Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ExecutorService executor = Executors.newFixedThreadPool(5);
- double[] arr = new double[5];
- for (int i=0; i<5; ++i) {
- arr[i] = 1 + Math.random();
- }
- for (int i=0; i<5; ++i) {
- final int j=i;
- executor.submit(() -> System.out.println(String.format("arr[%s]=%s", j, arr[j])));
- }
- for (int i=0; i<5; ++i) {
- arr[i] = 1 + Math.random();
- }
- final double[] arr2 = arr; //<---- safe publication?
- for (int i=0; i<5; ++i) {
- final int j=i;
- executor.submit(() -> System.out.println(String.format("arr[%s]=%s", j, arr2[j])));
- }
- CountDownLatch latch = new CountDownLatch(1); //1 = the number of writing threads
- for (int i=0; i<5; ++i) {
- arr[i] = Math.random();
- }
- latch.countDown(); //<- writing is done
- for (int i=0; i<5; ++i) {
- final int j=i;
- executor.submit(() -> {
- try {latch.await();} catch (InterruptedException e) {...} //happens-before(writings, reading) guarantee?
- System.out.println(String.format("arr[%s]=%s", j, arr[j]));
- });
- }
Add Comment
Please, Sign In to add comment