File tree Expand file tree Collapse file tree
main/java/cn/byhieg/threadtutorial/concurrent/blocking
test/java/cn/byhieg/threadtutorialtest/concurrenttest/blockingtest Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments