JAVA-21602 | fixing sync code block (#14318)
This commit is contained in:
parent
44179d2187
commit
f372563ca2
|
@ -17,22 +17,21 @@ public class Consumer implements Runnable {
|
|||
|
||||
public void consume() {
|
||||
while (dataQueue.runFlag) {
|
||||
synchronized (dataQueue) {
|
||||
while (dataQueue.isEmpty() && dataQueue.runFlag) {
|
||||
try {
|
||||
dataQueue.waitOnEmpty();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!dataQueue.runFlag) {
|
||||
while (dataQueue.isEmpty() && dataQueue.runFlag) {
|
||||
try {
|
||||
dataQueue.waitOnEmpty();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
break;
|
||||
}
|
||||
Message message = dataQueue.remove();
|
||||
dataQueue.notifyAllForFull();
|
||||
useMessage(message);
|
||||
}
|
||||
if (!dataQueue.runFlag) {
|
||||
break;
|
||||
}
|
||||
Message message = dataQueue.remove();
|
||||
dataQueue.notifyAllForFull();
|
||||
useMessage(message);
|
||||
|
||||
}
|
||||
log.info("Consumer Stopped");
|
||||
}
|
||||
|
@ -40,7 +39,7 @@ public class Consumer implements Runnable {
|
|||
private void useMessage(Message message) {
|
||||
if (message != null) {
|
||||
log.info(String.format("[%s] Consuming Message. Id: %d, Data: %f%n",
|
||||
Thread.currentThread().getName(), message.getId(), message.getData()));
|
||||
Thread.currentThread().getName(), message.getId(), message.getData()));
|
||||
|
||||
//Sleeping on random time to make it realistic
|
||||
ThreadUtil.sleep((long) (message.getData() * 100));
|
||||
|
|
|
@ -19,22 +19,21 @@ public class Producer implements Runnable {
|
|||
|
||||
public void produce() {
|
||||
while (dataQueue.runFlag) {
|
||||
synchronized (dataQueue) {
|
||||
while (dataQueue.isFull() && dataQueue.runFlag) {
|
||||
try {
|
||||
dataQueue.waitOnFull();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!dataQueue.runFlag) {
|
||||
while (dataQueue.isFull() && dataQueue.runFlag) {
|
||||
try {
|
||||
dataQueue.waitOnFull();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
break;
|
||||
}
|
||||
Message message = generateMessage();
|
||||
dataQueue.add(message);
|
||||
dataQueue.notifyAllForEmpty();
|
||||
}
|
||||
if (!dataQueue.runFlag) {
|
||||
break;
|
||||
}
|
||||
Message message = generateMessage();
|
||||
dataQueue.add(message);
|
||||
dataQueue.notifyAllForEmpty();
|
||||
|
||||
}
|
||||
log.info("Producer Stopped");
|
||||
}
|
||||
|
@ -42,7 +41,7 @@ public class Producer implements Runnable {
|
|||
private Message generateMessage() {
|
||||
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()));
|
||||
Thread.currentThread().getName(), message.getId(), message.getData()));
|
||||
|
||||
//Sleeping on random time to make it realistic
|
||||
ThreadUtil.sleep((long) (message.getData() * 100));
|
||||
|
|
Loading…
Reference in New Issue