File tree Expand file tree Collapse file tree 1 file changed +12
-18
lines changed Expand file tree Collapse file tree 1 file changed +12
-18
lines changed Original file line number Diff line number Diff line change @@ -1222,26 +1222,20 @@ public class ConsumerQueue implements Runnable {
1222
1222
``` java
1223
1223
package onlyfun.caterpillar ;
1224
1224
1225
+ import java.util.concurrent.ArrayBlockingQueue ;
1225
1226
import java.util.concurrent.BlockingQueue ;
1226
1227
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();
1245
1239
}
1246
1240
}
1247
1241
You can’t perform that action at this time.
0 commit comments