Skip to content

Commit 0696d65

Browse files
authored
Update BlockingQueueDemo.java
1 parent 2a8acd9 commit 0696d65

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

BlockingQueueDemo.java

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
package main.concurrency;
2-
31
import java.util.concurrent.ArrayBlockingQueue;
42
import java.util.concurrent.BlockingQueue;
53

64
/**
75
* Simple sample of Producer/Consumer using ArrayBlockingQueue (ThreadSafe)
8-
* - One Producer adds items in a pipeline in a frequency of 100ms
6+
* - One Producer adds items in a pipeline in a frequency of 200ms
97
* - The pipeline has a limit of 5 items
10-
* - When the pipeline is full, the Producer will wait for 500ms
118
* - Two consumers take items from the pipeline and consumes it in 500ms
129
*/
1310
public class BlockingQueueDemo {
@@ -16,28 +13,23 @@ public class BlockingQueueDemo {
1613
private static final int CONSUMERS = 2;
1714
private static final int CAPACITY = 5;
1815

19-
static class Producer extends Thread {
20-
BlockingQueue pipeline;
16+
private static class Producer extends Thread {
17+
private final BlockingQueue<String> pipeline;
2118

22-
Producer(BlockingQueue pipeline) {
19+
Producer(BlockingQueue<String> pipeline) {
2320
this. pipeline = pipeline;
2421
}
2522

2623
public void run() {
2724
int nItems = 1;
2825
while (nItems <= 20) {
2926
try {
30-
if (pipeline.remainingCapacity() > 0) {
31-
String item = "item" + nItems;
32-
pipeline.add(item);
33-
String capacity = String.format(" [%d/%d]", pipeline.size(), CAPACITY);
34-
System.out.println("Producer is adding " + item + capacity );
35-
nItems++;
36-
Thread.sleep(100);
37-
} else {
38-
System.out.println("Producer queue is full");
39-
Thread.sleep(500);
40-
}
27+
String item = "item" + nItems;
28+
pipeline.offer(item);
29+
String capacity = String.format(" [%d/%d]", pipeline.size(), CAPACITY);
30+
System.out.println("Producer is adding " + item + capacity );
31+
nItems++;
32+
Thread.sleep(200);
4133
} catch (InterruptedException e) {
4234
e.printStackTrace();
4335
}
@@ -59,19 +51,19 @@ public void run() {
5951
}
6052
}
6153

62-
static class Consumer extends Thread {
63-
BlockingQueue pipeline;
64-
String name;
54+
private static class Consumer extends Thread {
55+
private final BlockingQueue<String> pipeline;
56+
private final String name;
6557

66-
Consumer(String name, BlockingQueue pipeline) {
58+
Consumer(String name, BlockingQueue<String> pipeline) {
6759
this.name = name;
6860
this. pipeline = pipeline;
6961
}
7062

7163
public void run() {
7264
while (true) {
7365
try {
74-
String item = (String)pipeline.take();
66+
String item = pipeline.take();
7567
if (item.equals(STOP)) {
7668
break;
7769
}
@@ -85,7 +77,7 @@ public void run() {
8577
}
8678

8779
public static void main(String[] args) throws InterruptedException {
88-
BlockingQueue pipeline = new ArrayBlockingQueue<String>(CAPACITY);
80+
BlockingQueue<String> pipeline = new ArrayBlockingQueue<>(CAPACITY);
8981
new Producer(pipeline).start();
9082
Thread.sleep(100);
9183
for (int i = 0; i < CONSUMERS; i++) {

0 commit comments

Comments
 (0)