SynchronusQueue

/**
 * 当向SynchronousQueue插入元素时,必须同时有个线程往外取
 * SynchronousQueue是没有容量的,这是与其他的阻塞队列不同的地方
 */
public class SynchronusQueueTest {

   static SynchronousQueue<String> queue = new SynchronousQueue<>();

    public static void main(String[] args) throws InterruptedException {

        Executors.newCachedThreadPool().submit(()->{
            try {
                queue.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        queue.put("hello");
        System.out.println("==========");
    }
}
posted @ 2019-12-16 21:02  踏月而来  阅读(503)  评论(0编辑  收藏  举报