@@ -17,17 +17,20 @@ public class OrderPrintNum {
1717 private static final LinkedList <Integer > QUEUE = new LinkedList ();
1818 private static final ExecutorService POOL_SERVICE = Executors .newFixedThreadPool (3 );
1919
20- public static void main (String [] args ) {
20+ public static void main (String [] args ) throws InterruptedException {
21+
22+ // runPrint2();
23+
2124 initData ();
2225
23- POOL_SERVICE .execute (new PrintThread (QUEUE , 0 ));
24- POOL_SERVICE .execute (new PrintThread (QUEUE , 1 ));
25- POOL_SERVICE .execute (new PrintThread (QUEUE , 2 ));
26+ POOL_SERVICE .execute (new PrintThread1 (QUEUE , 2 ));
27+ POOL_SERVICE .execute (new PrintThread1 (QUEUE , 1 ));
28+ POOL_SERVICE .execute (new PrintThread1 (QUEUE , 0 ));
2629
2730 POOL_SERVICE .shutdown ();
2831
29- while (true ){
30- if (POOL_SERVICE .isTerminated ()){
32+ while (true ) {
33+ if (POOL_SERVICE .isTerminated ()) {
3134 System .out .println ("finished!!" );
3235 break ;
3336 }
@@ -40,28 +43,82 @@ private static void initData() {
4043 }
4144 }
4245
46+
47+ // 方案二
48+ private static void runPrint2 () {
49+ POOL_SERVICE .execute (new PrintThread2 (0 ));
50+ POOL_SERVICE .execute (new PrintThread2 (1 ));
51+ POOL_SERVICE .execute (new PrintThread2 (2 ));
52+
53+ POOL_SERVICE .shutdown ();
54+
55+ while (true ) {
56+ if (POOL_SERVICE .isTerminated ()) {
57+ System .out .println ("finished!!" );
58+ break ;
59+ }
60+ }
61+ }
4362}
4463
45- class PrintThread implements Runnable {
46- private static final ReentrantLock lock = new ReentrantLock ();
47- private static final Condition c = lock .newCondition ();
64+ class PrintThread1 implements Runnable {
65+ private static final Object LOCK = new Object ();
4866
49- private static int count = 0 ;
67+ private static int count = 0 ; // 计数,同时确定线程是否要加入等待队列,还是可以直接去资源队列里面去获取数据进行打印
5068 private LinkedList <Integer > queue ;
5169 private Integer threadNo ;
5270
53- public PrintThread (LinkedList <Integer > queue , Integer threadNo ) {
71+ public PrintThread1 (LinkedList <Integer > queue , Integer threadNo ) {
5472 this .queue = queue ;
5573 this .threadNo = threadNo ;
5674 }
5775
76+ @ Override
77+ public void run () {
78+ while (true ) {
79+ synchronized (LOCK ) {
80+ while (count % 3 != this .threadNo ) {
81+ if (count >= 101 ) {
82+ break ;
83+ }
84+ try {
85+ LOCK .wait ();
86+ } catch (InterruptedException e ) {
87+ e .printStackTrace ();
88+ }
89+ }
90+
91+ if (count >= 101 ) {
92+ break ;
93+ }
94+
95+ Integer val = this .queue .poll ();
96+ System .out .println ("thread-" + this .threadNo + ":" + val );
97+ count ++;
98+
99+ LOCK .notifyAll ();
100+ }
101+ }
102+ }
103+ }
104+
105+ class PrintThread2 implements Runnable {
106+ private static final ReentrantLock lock = new ReentrantLock ();
107+ private static final Condition c = lock .newCondition ();
108+
109+ private static int count = 0 ; //作为计数,同时也作为资源;因为这道题目是自然数作为资源,所以正好可以公用;
110+ private Integer threadNo ;
111+
112+ public PrintThread2 (Integer threadNo ) {
113+ this .threadNo = threadNo ;
114+ }
115+
58116 @ Override
59117 public void run () {
60118 while (true ) {
61119 try {
62120 lock .lock ();
63- int mod = count % 3 ;
64- if (mod != this .threadNo ) {
121+ while (count % 3 != this .threadNo ) {
65122 if (count >= 101 ) {
66123 break ;
67124 }
@@ -75,11 +132,10 @@ public void run() {
75132 if (count >= 101 ) {
76133 break ;
77134 }
78- Integer val = this .queue .poll ();
79- System .out .println ("thread-" + this .threadNo + ":" + val );
135+ System .out .println ("thread-" + this .threadNo + ":" + count );
80136 count ++;
81137
82- c .signal ();
138+ c .signalAll ();
83139
84140 } finally {
85141 lock .unlock ();
0 commit comments