* 同步队列
* 和其他的BlockingQueue 不一样 SynchronousQueue 不存储元素
* put了一个元素,必须从里面先take取出来,否则不能再put进去值
1 public static void main(String[] args) {
2 BlockingQueue<String> blockingQueue = new SynchronousQueue<>();
3
4 new Thread(()->{
5 try {
6 System.out.println(Thread.currentThread().getName()+"put 1");
7 blockingQueue.put("1");
8 System.out.println(Thread.currentThread().getName()+"put 2");
9 blockingQueue.put("2");
10 System.out.println(Thread.currentThread().getName()+"put 3");
11 blockingQueue.put("3");
12 } catch (InterruptedException e) {
13 e.printStackTrace();
14 }
15 },"T1").start();
16 new Thread(()->{
17 try {
18 TimeUnit.SECONDS.sleep(3);//等待3秒
19 System.out.println(Thread.currentThread().getName()+"=>"+blockingQueue.take());
20 TimeUnit.SECONDS.sleep(3);//等待3秒
21 System.out.println(Thread.currentThread().getName()+"=>"+blockingQueue.take());
22 TimeUnit.SECONDS.sleep(3);//等待3秒
23 System.out.println(Thread.currentThread().getName()+"=>"+blockingQueue.take());
24
25 } catch (InterruptedException e) {
26 e.printStackTrace();
27 }
28 },"T2").start();
29
30 }
