From 541fa8d5be14ed66591c1201ae4e010c870693d1 Mon Sep 17 00:00:00 2001 From: RanjeetKaur17 Date: Fri, 26 Oct 2018 20:50:07 +0400 Subject: [PATCH] Using AtomicInteger to avoid Race conditions --- .../CountdownLatchResetExample.java | 19 +++++++++---------- .../CyclicBarrierCompletionMethodExample.java | 15 +++++++-------- .../CyclicBarrierResetExample.java | 15 +++++++-------- .../CountdownLatchResetExampleUnitTest.java | 2 +- ...arrierCompletionMethodExampleUnitTest.java | 4 +--- .../CyclicBarrierResetExampleUnitTest.java | 4 +--- 6 files changed, 26 insertions(+), 33 deletions(-) diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/countdownlatch/CountdownLatchResetExample.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/countdownlatch/CountdownLatchResetExample.java index 55e509e374..1828b7f91e 100644 --- a/core-java-concurrency/src/main/java/com/baeldung/concurrent/countdownlatch/CountdownLatchResetExample.java +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/countdownlatch/CountdownLatchResetExample.java @@ -1,23 +1,22 @@ package com.baeldung.concurrent.countdownlatch; -import java.util.ArrayList; -import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicInteger; public class CountdownLatchResetExample { private int count; private int threadCount; - private final List outputScraper; - - CountdownLatchResetExample(final List outputScraper, int count, int threadCount) { - this.outputScraper = outputScraper; + private final AtomicInteger updateCount; + + CountdownLatchResetExample(int count, int threadCount) { + updateCount = new AtomicInteger(0); this.count = count; this.threadCount = threadCount; } - + public int countWaits() { CountDownLatch countDownLatch = new CountDownLatch(count); ExecutorService es = Executors.newFixedThreadPool(threadCount); @@ -26,17 +25,17 @@ public class CountdownLatchResetExample { long prevValue = countDownLatch.getCount(); countDownLatch.countDown(); if (countDownLatch.getCount() != prevValue) { - outputScraper.add("Count Updated"); + updateCount.incrementAndGet(); } }); } es.shutdown(); - return outputScraper.size(); + return updateCount.get(); } public static void main(String[] args) { - CountdownLatchResetExample ex = new CountdownLatchResetExample(new ArrayList<>(),5,20); + CountdownLatchResetExample ex = new CountdownLatchResetExample(5, 20); System.out.println("Count : " + ex.countWaits()); } } diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierCompletionMethodExample.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierCompletionMethodExample.java index b7a32e2c8d..7c1299da62 100644 --- a/core-java-concurrency/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierCompletionMethodExample.java +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierCompletionMethodExample.java @@ -1,20 +1,19 @@ package com.baeldung.concurrent.cyclicbarrier; -import java.util.ArrayList; -import java.util.List; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicInteger; public class CyclicBarrierCompletionMethodExample { private int count; private int threadCount; - private final List outputScraper; + private final AtomicInteger updateCount; - CyclicBarrierCompletionMethodExample(final List outputScraper, int count, int threadCount) { - this.outputScraper = outputScraper; + CyclicBarrierCompletionMethodExample(int count, int threadCount) { + updateCount = new AtomicInteger(0); this.count = count; this.threadCount = threadCount; } @@ -22,7 +21,7 @@ public class CyclicBarrierCompletionMethodExample { public int countTrips() { CyclicBarrier cyclicBarrier = new CyclicBarrier(count, () -> { - outputScraper.add("Barrier is Tripped"); + updateCount.incrementAndGet(); }); ExecutorService es = Executors.newFixedThreadPool(threadCount); @@ -36,11 +35,11 @@ public class CyclicBarrierCompletionMethodExample { }); } es.shutdown(); - return outputScraper.size(); + return updateCount.get(); } public static void main(String[] args) { - CyclicBarrierCompletionMethodExample ex = new CyclicBarrierCompletionMethodExample(new ArrayList(), 5, 20); + CyclicBarrierCompletionMethodExample ex = new CyclicBarrierCompletionMethodExample(5, 20); System.out.println("Count : " + ex.countTrips()); } } diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierResetExample.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierResetExample.java index febeb0978e..76b6198bc4 100644 --- a/core-java-concurrency/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierResetExample.java +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierResetExample.java @@ -1,20 +1,19 @@ package com.baeldung.concurrent.cyclicbarrier; -import java.util.ArrayList; -import java.util.List; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicInteger; public class CyclicBarrierResetExample { private int count; private int threadCount; - private final List outputScraper; + private final AtomicInteger updateCount; - CyclicBarrierResetExample(final List outputScraper, int count, int threadCount) { - this.outputScraper = outputScraper; + CyclicBarrierResetExample(int count, int threadCount) { + updateCount = new AtomicInteger(0); this.count = count; this.threadCount = threadCount; } @@ -28,7 +27,7 @@ public class CyclicBarrierResetExample { es.execute(() -> { try { if (cyclicBarrier.getNumberWaiting() > 0) { - outputScraper.add("Count Updated"); + updateCount.incrementAndGet(); } cyclicBarrier.await(); } catch (InterruptedException | BrokenBarrierException e) { @@ -37,11 +36,11 @@ public class CyclicBarrierResetExample { }); } es.shutdown(); - return outputScraper.size(); + return updateCount.get(); } public static void main(String[] args) { - CyclicBarrierResetExample ex = new CyclicBarrierResetExample(new ArrayList(), 7, 20); + CyclicBarrierResetExample ex = new CyclicBarrierResetExample(7, 20); System.out.println("Count : " + ex.countWaits()); } } diff --git a/core-java-concurrency/src/test/java/com/baeldung/concurrent/countdownlatch/CountdownLatchResetExampleUnitTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/countdownlatch/CountdownLatchResetExampleUnitTest.java index 2018c9168d..658ab5301a 100644 --- a/core-java-concurrency/src/test/java/com/baeldung/concurrent/countdownlatch/CountdownLatchResetExampleUnitTest.java +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/countdownlatch/CountdownLatchResetExampleUnitTest.java @@ -10,7 +10,7 @@ public class CountdownLatchResetExampleUnitTest { @Test public void whenCountDownLatch_noReset() { - CountdownLatchResetExample ex = new CountdownLatchResetExample(new ArrayList<>(),5,20); + CountdownLatchResetExample ex = new CountdownLatchResetExample(5,20); int lineCount = ex.countWaits(); assertEquals(5, lineCount); } diff --git a/core-java-concurrency/src/test/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierCompletionMethodExampleUnitTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierCompletionMethodExampleUnitTest.java index e8db84935a..f76b51c100 100644 --- a/core-java-concurrency/src/test/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierCompletionMethodExampleUnitTest.java +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierCompletionMethodExampleUnitTest.java @@ -2,15 +2,13 @@ package com.baeldung.concurrent.cyclicbarrier; import static org.junit.Assert.assertEquals; -import java.util.ArrayList; - import org.junit.Test; public class CyclicBarrierCompletionMethodExampleUnitTest { @Test public void whenCyclicBarrier_countTrips() { - CyclicBarrierCompletionMethodExample ex = new CyclicBarrierCompletionMethodExample(new ArrayList<>(),5,20); + CyclicBarrierCompletionMethodExample ex = new CyclicBarrierCompletionMethodExample(5,20); int lineCount = ex.countTrips(); assertEquals(4, lineCount); } diff --git a/core-java-concurrency/src/test/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierResetExampleUnitTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierResetExampleUnitTest.java index 2871d16348..413a17e9ff 100644 --- a/core-java-concurrency/src/test/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierResetExampleUnitTest.java +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierResetExampleUnitTest.java @@ -2,15 +2,13 @@ package com.baeldung.concurrent.cyclicbarrier; import static org.junit.Assert.assertEquals; -import java.util.ArrayList; - import org.junit.Test; public class CyclicBarrierResetExampleUnitTest { @Test public void whenCyclicBarrier_reset() { - CyclicBarrierResetExample ex = new CyclicBarrierResetExample(new ArrayList<>(),5,20); + CyclicBarrierResetExample ex = new CyclicBarrierResetExample(5,20); int lineCount = ex.countWaits(); assertEquals(16, lineCount); }