-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBlockingQueueDemo.java
More file actions
130 lines (117 loc) · 4.17 KB
/
BlockingQueueDemo.java
File metadata and controls
130 lines (117 loc) · 4.17 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.bywlstudio.queue;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 阻塞队列实现生产者消费者
*/
public class BlockingQueueDemo {
public static void main(String[] args) throws InterruptedException {
test();
}
private static void testBlock(){
BlockingQueue<Integer> blockingQueue = new SynchronousQueue<>();
new Thread(()->{
try {
blockingQueue.take();
System.out.println("执行了");
} catch (InterruptedException e) {
e.printStackTrace();
}
},"A").start();
new Thread(()->{
try {
try{
TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}
blockingQueue.put(2);
System.out.println("执行了==");
} catch (InterruptedException e) {
e.printStackTrace();
}
},"B").start();
}
private static void testSychron() {
BlockingQueue<Integer> blockingQueue = new SynchronousQueue<>();
try {
blockingQueue.put(2);
System.out.println("11111");
try{TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {e.printStackTrace();}
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
try {
blockingQueue.put(1);
System.out.println("执行了");
} catch (InterruptedException e) {
e.printStackTrace();
}
},"A").start();
new Thread(()->{
try {
try{
TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}
blockingQueue.take();
System.out.println("执行了==");
} catch (InterruptedException e) {
e.printStackTrace();
}
},"B").start();
}
private static void test() {
MyData myData = new MyData(new ArrayBlockingQueue(10));
new Thread(()->{
for (int j = 0; j < 5; j++) {
myData.increment();
}
},"线程一").start();
try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}
new Thread(()->{
for (int j = 0; j < 5; j++) {
myData.decrement();
}
},"线程二").start();
try{TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {e.printStackTrace();}
myData.stop();
}
}
class MyData{
private volatile boolean flag = true ;
private AtomicInteger atomicInteger = new AtomicInteger();
private BlockingQueue<Integer> blockingQueue ;
public MyData(BlockingQueue blockingQueue) {
this.blockingQueue = blockingQueue;
}
public void increment(){
while(flag){
try {
if(blockingQueue.offer(atomicInteger.getAndIncrement(),2,TimeUnit.SECONDS)){
System.out.println(Thread.currentThread().getName()+"\t 插入成功\t"+atomicInteger.get());
}else{
System.out.println(Thread.currentThread().getName()+"\t 插入失败");
}
try{TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void decrement(){
while (flag){
try {
Integer poll = blockingQueue.poll(2, TimeUnit.SECONDS);
if(poll == null ){
flag = false ;
System.out.println(Thread.currentThread().getName()+"\t不再等待");
return ;
}
System.out.println(Thread.currentThread().getName()+"\t 获取成功\t"+ atomicInteger.decrementAndGet());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void stop(){
flag = false ;
System.out.println("主线程停止");
}
}