Skip to content

Commit f2e8cb1

Browse files
author
Justin Lin
committed
更正程式碼
1 parent 32397ff commit f2e8cb1

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

docs/CH15.md

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,26 +1222,20 @@ public class ConsumerQueue implements Runnable {
12221222
```java
12231223
package onlyfun.caterpillar;
12241224

1225+
import java.util.concurrent.ArrayBlockingQueue;
12251226
import java.util.concurrent.BlockingQueue;
12261227

1227-
public class ConsumerQueue implements Runnable {
1228-
private BlockingQueue<Integer> queue;
1229-
1230-
public ConsumerQueue(BlockingQueue<Integer> queue) {
1231-
this.queue = queue;
1232-
}
1233-
1234-
public void run() {
1235-
for(int i = 1; i <= 10; i++) {
1236-
try {
1237-
// wait for a random time
1238-
Thread.sleep((int) (Math.random() * 3000));
1239-
queue.take();
1240-
}
1241-
catch(InterruptedException e) {
1242-
e.printStackTrace();
1243-
}
1244-
}
1228+
public class BlockingQueueDemo {
1229+
public static void main(String[] args) {
1230+
BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(1);
1231+
1232+
Thread producerThread = new Thread(
1233+
new ProducerQueue(queue));
1234+
Thread consumerThread = new Thread(
1235+
new ConsumerQueue(queue));
1236+
1237+
producerThread.start();
1238+
consumerThread.start();
12451239
}
12461240
}
12471241

0 commit comments

Comments
 (0)