Skip to content

Commit 49f9e64

Browse files
committed
Spotbugs fix: Ensure bytesUsed is atomic
1 parent 972c0a4 commit 49f9e64

File tree

1 file changed

+9
-6
lines changed
  • commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress

1 file changed

+9
-6
lines changed

commons-rng-examples/examples-stress/src/main/java/org/apache/commons/rng/examples/stress/StressTestCommand.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.util.concurrent.Executors;
4949
import java.util.concurrent.Future;
5050
import java.util.concurrent.TimeUnit;
51+
import java.util.concurrent.atomic.AtomicLong;
5152
import java.util.concurrent.locks.ReentrantLock;
5253

5354
/**
@@ -940,7 +941,7 @@ private static class StressTestTask implements Runnable {
940941
private final ProgressTracker progressTracker;
941942

942943
/** The count of bytes used by the sub-process. */
943-
private long bytesUsed;
944+
private AtomicLong bytesUsed = new AtomicLong();
944945

945946
/**
946947
* Creates the task.
@@ -1015,17 +1016,18 @@ private Integer runSubProcess() throws IOException {
10151016
final Process testingProcess = builder.start();
10161017

10171018
// Use a custom data output to write the RNG.
1019+
long writeCount = 0;
10181020
try (RngDataOutput sink = RNGUtils.createDataOutput(rng, cmd.source64,
10191021
testingProcess.getOutputStream(), cmd.bufferSize, cmd.byteOrder)) {
10201022
for (;;) {
10211023
sink.write(rng);
1022-
bytesUsed++;
1024+
writeCount++;
10231025
}
10241026
} catch (final IOException ignored) {
10251027
// Hopefully getting here when the analyzing software terminates.
10261028
}
10271029

1028-
bytesUsed *= cmd.bufferSize;
1030+
bytesUsed.set(writeCount * cmd.bufferSize);
10291031

10301032
// Get the exit value.
10311033
// Wait for up to 60 seconds.
@@ -1094,10 +1096,11 @@ private void printFooter(long millis,
10941096

10951097
appendDate(sb, "End").append(C).append(N);
10961098

1099+
final long bytes = bytesUsed.get();
10971100
sb.append(C).append("Exit value: ").append(exitValue).append(N)
1098-
.append(C).append("Bytes used: ").append(bytesUsed)
1099-
.append(" >= 2^").append(log2(bytesUsed))
1100-
.append(" (").append(bytesToString(bytesUsed)).append(')').append(N)
1101+
.append(C).append("Bytes used: ").append(bytes)
1102+
.append(" >= 2^").append(log2(bytes))
1103+
.append(" (").append(bytesToString(bytes)).append(')').append(N)
11011104
.append(C).append(N);
11021105

11031106
final double duration = millis * 1e-3 / 60;

0 commit comments

Comments
 (0)