Skip to content

Commit d200fb2

Browse files
committed
添加阻塞队列
1 parent 8632db2 commit d200fb2

4 files changed

Lines changed: 116 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cn.byhieg.threadtutorial.concurrent.blocking;
2+
3+
import java.util.concurrent.ArrayBlockingQueue;
4+
import java.util.concurrent.BlockingQueue;
5+
6+
/**
7+
* Created by byhieg on 17/5/3.
8+
9+
*/
10+
public class ArrayBlock {
11+
12+
private BlockingQueue<String> blockingQueue;
13+
14+
public ArrayBlock(){
15+
blockingQueue = new ArrayBlockingQueue<String>(1024);
16+
}
17+
18+
public BlockingQueue<String> getBlockingQueue() {
19+
return blockingQueue;
20+
}
21+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cn.byhieg.threadtutorial.concurrent.blocking;
2+
3+
import java.util.concurrent.BlockingQueue;
4+
5+
/**
6+
* Created by byhieg on 17/5/3.
7+
8+
*/
9+
public class Costumer extends Thread{
10+
11+
private BlockingQueue<String> blockingQueue;
12+
13+
public Costumer(ArrayBlock arrayBlock) {
14+
blockingQueue = arrayBlock.getBlockingQueue();
15+
this.setName("Costumer");
16+
}
17+
18+
@Override
19+
public void run() {
20+
super.run();
21+
while (true) {
22+
try {
23+
Thread.sleep(2000);
24+
String str = blockingQueue.take();
25+
System.out.println(getName() + " 取出数据 " + str);
26+
} catch (InterruptedException e) {
27+
e.printStackTrace();
28+
}
29+
}
30+
}
31+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package cn.byhieg.threadtutorial.concurrent.blocking;
2+
3+
import java.util.concurrent.BlockingQueue;
4+
5+
/**
6+
* Created by byhieg on 17/5/3.
7+
8+
*/
9+
public class Producer extends Thread {
10+
11+
private BlockingQueue<String> blockingQueue;
12+
@Override
13+
public void run() {
14+
super.run();
15+
for (int i = 0 ; i < 5;i++) {
16+
try {
17+
System.out.println(getName() + " 生产数据");
18+
blockingQueue.put(i + "");
19+
Thread.sleep(1000);
20+
} catch (InterruptedException e) {
21+
e.printStackTrace();
22+
}
23+
}
24+
}
25+
26+
public Producer(ArrayBlock arrayBlock){
27+
this.setName("Producer");
28+
blockingQueue = arrayBlock.getBlockingQueue();
29+
}
30+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cn.byhieg.threadtutorialtest.concurrenttest.blockingtest;
2+
3+
import cn.byhieg.threadtutorial.concurrent.blocking.ArrayBlock;
4+
import cn.byhieg.threadtutorial.concurrent.blocking.Costumer;
5+
import cn.byhieg.threadtutorial.concurrent.blocking.Producer;
6+
import junit.framework.TestCase;
7+
8+
import javax.swing.*;
9+
10+
/**
11+
* Created by byhieg on 17/5/3.
12+
13+
*/
14+
public class ArrayBlockTest extends TestCase {
15+
ArrayBlock block;
16+
public void setUp() throws Exception {
17+
super.setUp();
18+
block = new ArrayBlock();
19+
}
20+
21+
public void tearDown() throws Exception {
22+
}
23+
24+
25+
public void testBlocking() throws Exception {
26+
Producer producer = new Producer(block);
27+
Costumer costumer = new Costumer(block);
28+
producer.start();
29+
costumer.start();
30+
producer.join();
31+
costumer.join();
32+
33+
}
34+
}

0 commit comments

Comments
 (0)