如何学习一门知识 Posted on 2021-02-13 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253public class BlockingQueueTest { public static void main(String[] args) { BlockingQueue<String> blockingQueue = new SynchronousQueue<>(); new Thread(() -> { try { System.out.println(Thread.currentThread().getName() + "\t put A "); blockingQueue.put("A"); System.out.println(Thread.currentThread().getName() + "\t put B "); blockingQueue.put("B"); System.out.println(Thread.currentThread().getName() + "\t put C "); blockingQueue.put("C"); } catch (InterruptedException e) { e.printStackTrace(); } }, "t1").start(); new Thread(() -> { try { try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); } blockingQueue.take(); System.out.println(Thread.currentThread().getName() + "\t take A "); try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); } blockingQueue.take(); System.out.println(Thread.currentThread().getName() + "\t take B "); try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); } blockingQueue.take(); System.out.println(Thread.currentThread().getName() + "\t take C "); } catch (InterruptedException e) { e.printStackTrace(); } }, "t2").start(); }} Donate comment here Donate WeChat Pay