-
Notifications
You must be signed in to change notification settings - Fork 0
/
SychronousQueueDemo.java
33 lines (29 loc) · 1.13 KB
/
SychronousQueueDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package edu.nwpu.zhao.Test;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;
public class SychronousQueueDemo {
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();
}
},"AAA").start();
new Thread(()->{
try {
TimeUnit.SECONDS.sleep(5);
System.out.println(Thread.currentThread().getName()+"\t get "+blockingQueue.take());
}catch (InterruptedException e){
e.printStackTrace();
}
},"BBB").start();
}
}