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 11package cn .byhieg .threadtutorial .concurrent .blocking ;
22
3- import java .util .concurrent .ArrayBlockingQueue ;
4- import java .util .concurrent .BlockingQueue ;
3+ import com .sun .scenario .effect .impl .sw .sse .SSEBlend_SRC_OUTPeer ;
4+
5+ import javax .sound .midi .SoundbankResource ;
6+ import java .lang .management .LockInfo ;
7+ import java .util .concurrent .*;
58
69/**
710 * Created by byhieg on 17/5/3.
@@ -11,8 +14,18 @@ public class ArrayBlock {
1114
1215 private BlockingQueue <String > blockingQueue ;
1316
14- public ArrayBlock (){
15- blockingQueue = new ArrayBlockingQueue <String >(1024 );
17+ public ArrayBlock (int index ) {
18+ switch (index ) {
19+ case 0 :
20+ blockingQueue = new ArrayBlockingQueue <String >(3 );
21+ break ;
22+ case 1 :
23+ blockingQueue = new LinkedBlockingQueue <>();
24+ break ;
25+ case 2 :
26+ blockingQueue = new SynchronousQueue <>();
27+ break ;
28+ }
1629 }
1730
1831 public BlockingQueue <String > getBlockingQueue () {
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ public void run() {
2020 super .run ();
2121 while (true ) {
2222 try {
23- Thread .sleep (2000 );
23+ Thread .sleep (5000 );
2424 String str = blockingQueue .take ();
2525 System .out .println (getName () + " 取出数据 " + str );
2626 } catch (InterruptedException e ) {
Original file line number Diff line number Diff line change @@ -14,9 +14,8 @@ public void run() {
1414 super .run ();
1515 for (int i = 0 ; i < 5 ;i ++) {
1616 try {
17- System .out .println (getName () + " 生产数据" );
1817 blockingQueue .put (i + "" );
19- Thread . sleep ( 1000 );
18+ System . out . println ( getName () + " 生产数据" );
2019 } catch (InterruptedException e ) {
2120 e .printStackTrace ();
2221 }
Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ public class ArrayBlockTest extends TestCase {
1515 ArrayBlock block ;
1616 public void setUp () throws Exception {
1717 super .setUp ();
18- block = new ArrayBlock ();
18+ block = new ArrayBlock (2 );
1919 }
2020
2121 public void tearDown () throws Exception {
You can’t perform that action at this time.
0 commit comments