Skip to content

Commit ad21e99

Browse files
committed
Merge branch 'tomekl007-BAEL-614'
2 parents 8d68b5b + d389301 commit ad21e99

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

‎core-java/src/main/java/com/baeldung/concurrent/blockingqueue/BlockingQueueUsage.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ public static void main(String[] args) {
88
int BOUND = 10;
99
int N_PRODUCERS = 4;
1010
int N_CONSUMERS = Runtime.getRuntime().availableProcessors();
11+
int poisonPill = Integer.MAX_VALUE;
12+
int poisonPillPerProducer = N_CONSUMERS / N_PRODUCERS;
1113

1214
BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(BOUND);
1315

1416
for (int i = 0; i < N_PRODUCERS; i++) {
15-
new Thread(new NumbersProducer(queue)).start();
17+
new Thread(new NumbersProducer(queue, poisonPill, poisonPillPerProducer)).start();
1618
}
1719

1820
for (int j = 0; j < N_CONSUMERS; j++) {
19-
new Thread(new NumbersConsumer(queue)).start();
21+
new Thread(new NumbersConsumer(queue, poisonPill)).start();
2022
}
2123
}
2224
}

‎core-java/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersConsumer.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@
44

55
public class NumbersConsumer implements Runnable {
66
private final BlockingQueue<Integer> queue;
7+
private final int poisonPill;
78

8-
public NumbersConsumer(BlockingQueue<Integer> queue) {
9+
public NumbersConsumer(BlockingQueue<Integer> queue, int poisonPill) {
910
this.queue = queue;
11+
this.poisonPill = poisonPill;
1012
}
1113

1214
public void run() {
1315
try {
1416
while (true) {
1517
Integer number = queue.take();
18+
if (number.equals(poisonPill)) {
19+
return;
20+
}
1621
String result = number.toString();
1722
System.out.println(Thread.currentThread().getName() + " result: " + result);
1823
}

‎core-java/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersProducer.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
public class NumbersProducer implements Runnable {
77

88
private final BlockingQueue<Integer> numbersQueue;
9+
private final int poisonPill;
10+
private final int poisonPillPerProducer;
911

10-
public NumbersProducer(BlockingQueue<Integer> numbersQueue) {
12+
public NumbersProducer(BlockingQueue<Integer> numbersQueue, int poisonPill, int poisonPillPerProducer) {
1113
this.numbersQueue = numbersQueue;
14+
this.poisonPill = poisonPill;
15+
this.poisonPillPerProducer = poisonPillPerProducer;
1216
}
1317

1418
public void run() {
@@ -23,5 +27,8 @@ private void generateNumbers() throws InterruptedException {
2327
for (int i = 0; i < 100; i++) {
2428
numbersQueue.put(ThreadLocalRandom.current().nextInt(100));
2529
}
30+
for (int j = 0; j < poisonPillPerProducer; j++) {
31+
numbersQueue.put(poisonPill);
32+
}
2633
}
2734
}

0 commit comments

Comments
 (0)