Skip to content

Commit f68983d

Browse files
committed
concurrency bug fix: synchronize on read (as well as write)
1 parent 05b2961 commit f68983d

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

test/src/com/rabbitmq/examples/MulticastMain.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,6 @@ public void handleAck(long seqNo, boolean multiple) {
311311
}
312312
}
313313

314-
public synchronized void resetCounts() {
315-
msgCount = 0;
316-
returnCount = 0;
317-
confirmCount = 0;
318-
}
319-
320314
public void run() {
321315

322316
long now;
@@ -377,21 +371,23 @@ private void delay(long now)
377371
Thread.sleep(pause);
378372
}
379373
if (elapsed > interval) {
380-
System.out.print("sending rate: " +
381-
(msgCount * 1000L / elapsed) +
382-
" msg/s");
374+
long sendRate, returnRate, confirmRate;
375+
synchronized(this) {
376+
sendRate = msgCount * 1000L / elapsed;
377+
returnRate = returnCount * 1000L / elapsed;
378+
confirmRate = confirmCount * 1000L / elapsed;
379+
msgCount = 0;
380+
returnCount = 0;
381+
confirmCount = 0;
382+
}
383+
System.out.print("sending rate: " + sendRate + " msg/s");
383384
if (mandatory || immediate) {
384-
System.out.print(", returns: " +
385-
(returnCount * 1000L / elapsed) +
386-
" ret/s");
385+
System.out.print(", returns: " + returnRate + " ret/s");
387386
}
388387
if (confirm) {
389-
System.out.print(", confirms: " +
390-
(confirmCount * 1000L / elapsed) +
391-
" c/s");
388+
System.out.print(", confirms: " + confirmRate + " c/s");
392389
}
393390
System.out.println();
394-
resetCounts();
395391
lastStatsTime = now;
396392
}
397393
}

0 commit comments

Comments
 (0)