BAEL-6501 Update article "The producer-consumer problem example in Java" (#13973)

* BAEL-6501 Changed the synchronization block to use the DataQueue

* BAEL-6501 Fixed the platform agnostic new lines

* BAEL-6501 Moved ID increment to a static method

* BAEL-6501 Added a logger
This commit is contained in:
Eugene Kovko 2023-05-08 23:04:21 +02:00 committed by GitHub
parent 579ba7726f
commit 4cc0380249
3 changed files with 23 additions and 9 deletions

View File

@ -1,6 +1,9 @@
package com.baeldung.producerconsumer;
import java.util.logging.Logger;
public class Consumer implements Runnable {
private static final Logger log = Logger.getLogger(Consumer.class.getCanonicalName());
private final DataQueue dataQueue;
public Consumer(DataQueue dataQueue) {
@ -14,7 +17,7 @@ public class Consumer implements Runnable {
public void consume() {
while (dataQueue.runFlag) {
synchronized (this) {
synchronized (dataQueue) {
while (dataQueue.isEmpty() && dataQueue.runFlag) {
try {
dataQueue.waitOnEmpty();
@ -31,12 +34,13 @@ public class Consumer implements Runnable {
useMessage(message);
}
}
System.out.println("Consumer Stopped");
log.info("Consumer Stopped");
}
private void useMessage(Message message) {
if (message != null) {
System.out.printf("[%s] Consuming Message. Id: %d, Data: %f\n", Thread.currentThread().getName(), message.getId(), message.getData());
log.info(String.format("[%s] Consuming Message. Id: %d, Data: %f%n",
Thread.currentThread().getName(), message.getId(), message.getData()));
//Sleeping on random time to make it realistic
ThreadUtil.sleep((long) (message.getData() * 100));

View File

@ -1,6 +1,9 @@
package com.baeldung.producerconsumer;
import java.util.logging.Logger;
public class Producer implements Runnable {
private static final Logger log = Logger.getLogger(Producer.class.getCanonicalName());
private final DataQueue dataQueue;
private static int idSequence = 0;
@ -16,7 +19,7 @@ public class Producer implements Runnable {
public void produce() {
while (dataQueue.runFlag) {
synchronized (this) {
synchronized (dataQueue) {
while (dataQueue.isFull() && dataQueue.runFlag) {
try {
dataQueue.waitOnFull();
@ -33,12 +36,13 @@ public class Producer implements Runnable {
dataQueue.notifyAllForEmpty();
}
}
System.out.println("Producer Stopped");
log.info("Producer Stopped");
}
private Message generateMessage() {
Message message = new Message(++idSequence, Math.random());
System.out.printf("[%s] Generated Message. Id: %d, Data: %f\n", Thread.currentThread().getName(), message.getId(), message.getData());
Message message = new Message(incrementAndGetId(), Math.random());
log.info(String.format("[%s] Generated Message. Id: %d, Data: %f%n",
Thread.currentThread().getName(), message.getId(), message.getData()));
//Sleeping on random time to make it realistic
ThreadUtil.sleep((long) (message.getData() * 100));
@ -46,6 +50,10 @@ public class Producer implements Runnable {
return message;
}
private static int incrementAndGetId() {
return ++idSequence;
}
public void stop() {
dataQueue.runFlag = false;
dataQueue.notifyAllForFull();

View File

@ -2,10 +2,12 @@ package com.baeldung.producerconsumer;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.logging.Logger;
import static com.baeldung.producerconsumer.ThreadUtil.sleep;
public class SimpleProducerConsumerDemonstrator {
private static final Logger log = Logger.getLogger(SimpleProducerConsumerDemonstrator.class.getCanonicalName());
BlockingQueue<Double> blockingQueue = new LinkedBlockingDeque<>(5);
private void produce() {
@ -17,7 +19,7 @@ public class SimpleProducerConsumerDemonstrator {
e.printStackTrace();
break;
}
System.out.printf("[%s] Value produced: %f\n", Thread.currentThread().getName(), value);
log.info(String.format("[%s] Value produced: %f%n", Thread.currentThread().getName(), value));
}
}
@ -31,7 +33,7 @@ public class SimpleProducerConsumerDemonstrator {
break;
}
// Consume value
System.out.printf("[%s] Value consumed: %f\n", Thread.currentThread().getName(), value);
log.info(String.format("[%s] Value consumed: %f%n", Thread.currentThread().getName(), value));
}
}