Created
December 19, 2018 12:36
-
-
Save franz1981/b81193705bafdc22875d5612ff9c48fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) { | |
final ExecutorService executorService = Executors.newFixedThreadPool(2); | |
final int tests = 5; | |
final int iterations = 10_000_000; | |
final int length = 64; | |
final AtomicBoolean releaser = new AtomicBoolean(); | |
final double[] values = new double[length]; | |
final AtomicBoolean finished = new AtomicBoolean(false); | |
Arrays.fill(values, Double.MAX_VALUE); | |
executorService.submit(() -> { | |
for (int t = 0; t < tests; t++) { | |
for (int i = 0; i < iterations; i++) { | |
final boolean reverse = i % 2 == 0; | |
for (int v = 0; v < length; v++) { | |
values[v] = reverse ? Double.MIN_VALUE : Double.MAX_VALUE; | |
} | |
releaser.lazySet(true); | |
} | |
} | |
finished.lazySet(true); | |
}); | |
executorService.submit(() -> { | |
while (!finished.get()) { | |
if (releaser.get()) { | |
for (int v = 0; v < length; v++) { | |
final double value = values[v]; | |
if (value != Double.MAX_VALUE && value != Double.MIN_VALUE) { | |
System.err.println("Non atomic write of a double: " + Long.toBinaryString(Double.doubleToRawLongBits(value))); | |
} | |
} | |
} | |
} | |
}); | |
executorService.shutdown(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment