/** * 当向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("=========="); } }
原文:https://www.cnblogs.com/moris5013/p/12051339.html