JUC_01

ArrayBlockingQueue的四组api

方式 抛出异常 有返回值,不抛出异常 阻塞,等待 超时等待
添加 add() offer() put() put(,,)
移除 remove() poll() take() take(,)
检测队首元素 element peek - -

SyschronousQueue同步队列 只有一个元素

`package org.li;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;

public class SynchronousQueueDemo {
public static void main(String[] args) {
BlockingQueue bq= new SynchronousQueue<>();
new Thread(()->{
try {
System.out.println(Thread.currentThread().getName()+"->putabc");
bq.put("abc");
System.out.println(Thread.currentThread().getName()+"->putABC");
bq.put("ABC");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
},"t1").start();
new Thread(()->{
try {
System.out.println(Thread.currentThread().getName()+"->take:abc");
bq.take();
System.out.println(Thread.currentThread().getName()+"->take:ABC");
bq.take();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
},"t2").start();
}
}
`

posted @ 2025-04-17 12:09  黑影五  阅读(8)  评论(0)    收藏  举报