是一個不存儲元素的阻塞隊列。每一個put操作必須等待一個take操作,否則不能繼續添加元素。本身不存儲元素,只是負責傳遞(但是使用起來就像是隻能夠存儲一個元素的隊列)
demo
public class Test extends Thread {
public static void main(String[] args) throws InterruptedException {
SynchronousQueue queue = new SynchronousQueue();
//生產者線程
new Thread(){
@Override
public void run() {
try {
queue.put(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
//消費者線程
new Thread(){
@Override
public void run() {
try {
System.out.println(queue.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
}
結果:
1