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
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();
}
}
`

浙公网安备 33010602011771号