From 5b8de98ac341b0763b20ba43e8a2753e3f572d1b Mon Sep 17 00:00:00 2001 From: Gaetano Piazzolla Date: Mon, 28 Aug 2023 19:38:00 +0200 Subject: [PATCH] JAVA-23377 | fixing sync code block (#14574) --- .../baeldung/producerconsumer/DataQueue.java | 4 ++ .../baeldung/producerconsumer/Producer.java | 46 ++++++++++++------- .../ProducerConsumerDemonstrator.java | 9 ++-- 3 files changed, 39 insertions(+), 20 deletions(-) diff --git a/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/producerconsumer/DataQueue.java b/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/producerconsumer/DataQueue.java index 8867ddeb63..8b6f02b529 100644 --- a/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/producerconsumer/DataQueue.java +++ b/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/producerconsumer/DataQueue.java @@ -58,4 +58,8 @@ public class DataQueue { return queue.poll(); } } + + public Integer getSize() { + return queue.size(); + } } diff --git a/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/producerconsumer/Producer.java b/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/producerconsumer/Producer.java index 5ca60a29e4..069476bbd1 100644 --- a/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/producerconsumer/Producer.java +++ b/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/producerconsumer/Producer.java @@ -1,12 +1,13 @@ package com.baeldung.producerconsumer; +import java.util.concurrent.locks.ReentrantLock; 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; + final ReentrantLock lock = new ReentrantLock(); public Producer(DataQueue dataQueue) { this.dataQueue = dataQueue; @@ -19,22 +20,38 @@ public class Producer implements Runnable { public void produce() { while (dataQueue.runFlag) { - while (dataQueue.isFull() && dataQueue.runFlag) { - try { - dataQueue.waitOnFull(); - } catch (InterruptedException e) { - e.printStackTrace(); + + try { + lock.lock(); + + while (dataQueue.isFull() && dataQueue.runFlag) { + try { + dataQueue.waitOnFull(); + } catch (InterruptedException e) { + e.printStackTrace(); + break; + } + } + + if (!dataQueue.runFlag) { break; } - } - if (!dataQueue.runFlag) { - break; - } - Message message = generateMessage(); - dataQueue.add(message); - dataQueue.notifyAllForEmpty(); + Message message = generateMessage(); + dataQueue.add(message); + dataQueue.notifyAllForEmpty(); + + log.info("Size of the queue is: " + dataQueue.getSize()); + + } + finally{ + lock.unlock(); + } + + //Sleeping on random time to make it realistic + ThreadUtil.sleep((long) (Math.random() * 100)); } + log.info("Producer Stopped"); } @@ -43,9 +60,6 @@ public class Producer implements Runnable { 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)); - return message; } diff --git a/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/producerconsumer/ProducerConsumerDemonstrator.java b/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/producerconsumer/ProducerConsumerDemonstrator.java index 96d7b9f865..eac026536d 100644 --- a/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/producerconsumer/ProducerConsumerDemonstrator.java +++ b/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/producerconsumer/ProducerConsumerDemonstrator.java @@ -36,8 +36,8 @@ public class ProducerConsumerDemonstrator { public static void demoMultipleProducersAndMultipleConsumers() { DataQueue dataQueue = new DataQueue(MAX_QUEUE_CAPACITY); - int producerCount = 3; - int consumerCount = 3; + int producerCount = 5; + int consumerCount = 5; List threads = new ArrayList<>(); Producer producer = new Producer(dataQueue); for(int i = 0; i < producerCount; i++) { @@ -45,6 +45,7 @@ public class ProducerConsumerDemonstrator { producerThread.start(); threads.add(producerThread); } + Consumer consumer = new Consumer(dataQueue); for(int i = 0; i < consumerCount; i++) { Thread consumerThread = new Thread(consumer); @@ -52,8 +53,8 @@ public class ProducerConsumerDemonstrator { threads.add(consumerThread); } - // let threads run for two seconds - sleep(2000); + // let threads run for ten seconds + sleep(10000); // Stop threads producer.stop();