Skip to content

Commit 992d899

Browse files
committed
SonarQube fix try 2
1 parent 2109002 commit 992d899

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

queue-based-load-leveling/src/main/java/com/iluwatar/queue/load/leveling/MessageQueue.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
public class MessageQueue {
3737

3838
private final BlockingQueue<Message> blkQueue;
39-
public Object serviceExecutorWait =new Object();
4039

4140
// Default constructor when called creates Blocking Queue object.
4241
public MessageQueue() {
@@ -51,8 +50,8 @@ public void submitMsg(Message msg) {
5150
try {
5251
if (null != msg) {
5352
blkQueue.add(msg);
54-
synchronized (serviceExecutorWait) {
55-
serviceExecutorWait.notifyAll();
53+
synchronized (ServiceExecutor.serviceExecutorWait) {
54+
ServiceExecutor.serviceExecutorWait.notifyAll();
5655
}
5756
}
5857
} catch (Exception e) {

queue-based-load-leveling/src/main/java/com/iluwatar/queue/load/leveling/ServiceExecutor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
@Slf4j
3434
public class ServiceExecutor implements Runnable {
3535
private final MessageQueue msgQueue;
36-
36+
public static final Object serviceExecutorWait = new Object();
3737
public ServiceExecutor(MessageQueue msgQueue) {
3838
this.msgQueue = msgQueue;
3939
}
@@ -50,8 +50,8 @@ public void run() {
5050
LOGGER.info(msg + " is served.");
5151
} else {
5252
LOGGER.info("Service Executor: Waiting for Messages to serve .. ");
53-
synchronized (msgQueue.serviceExecutorWait) {
54-
msgQueue.serviceExecutorWait.wait();
53+
synchronized (serviceExecutorWait) {
54+
serviceExecutorWait.wait();
5555
}
5656
}
5757
}

queue-based-load-leveling/src/main/java/com/iluwatar/queue/load/leveling/TaskGenerator.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ public TaskGenerator(MessageQueue msgQueue, int msgCount) {
5050
*/
5151
public void submit(Message msg) {
5252
try {
53-
this.msgQueue.submitMsg(msg);
53+
synchronized (msg) {
54+
this.msgQueue.submitMsg(msg);
55+
}
5456
} catch (Exception e) {
5557
LOGGER.error(e.getMessage());
5658
}
@@ -66,9 +68,12 @@ public void run() {
6668
try {
6769
while (count > 0) {
6870
var statusMsg = "Message-" + count + " submitted by " + Thread.currentThread().getName();
69-
this.submit(new Message(statusMsg));
71+
Message msg = new Message(statusMsg);
72+
synchronized (msg) {
73+
this.submit(msg);
74+
LOGGER.info(statusMsg);
75+
}
7076

71-
LOGGER.info(statusMsg);
7277

7378
// reduce the message count.
7479
count--;

0 commit comments

Comments
 (0)