Using AtomicInteger to avoid Race conditions

This commit is contained in:
RanjeetKaur17 2018-10-26 20:50:07 +04:00
parent a559137c8c
commit 541fa8d5be
6 changed files with 26 additions and 33 deletions

View File

@ -1,23 +1,22 @@
package com.baeldung.concurrent.countdownlatch; package com.baeldung.concurrent.countdownlatch;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
public class CountdownLatchResetExample { public class CountdownLatchResetExample {
private int count; private int count;
private int threadCount; private int threadCount;
private final List<String> outputScraper; private final AtomicInteger updateCount;
CountdownLatchResetExample(final List<String> outputScraper, int count, int threadCount) { CountdownLatchResetExample(int count, int threadCount) {
this.outputScraper = outputScraper; updateCount = new AtomicInteger(0);
this.count = count; this.count = count;
this.threadCount = threadCount; this.threadCount = threadCount;
} }
public int countWaits() { public int countWaits() {
CountDownLatch countDownLatch = new CountDownLatch(count); CountDownLatch countDownLatch = new CountDownLatch(count);
ExecutorService es = Executors.newFixedThreadPool(threadCount); ExecutorService es = Executors.newFixedThreadPool(threadCount);
@ -26,17 +25,17 @@ public class CountdownLatchResetExample {
long prevValue = countDownLatch.getCount(); long prevValue = countDownLatch.getCount();
countDownLatch.countDown(); countDownLatch.countDown();
if (countDownLatch.getCount() != prevValue) { if (countDownLatch.getCount() != prevValue) {
outputScraper.add("Count Updated"); updateCount.incrementAndGet();
} }
}); });
} }
es.shutdown(); es.shutdown();
return outputScraper.size(); return updateCount.get();
} }
public static void main(String[] args) { 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()); System.out.println("Count : " + ex.countWaits());
} }
} }

View File

@ -1,20 +1,19 @@
package com.baeldung.concurrent.cyclicbarrier; package com.baeldung.concurrent.cyclicbarrier;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier; import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
public class CyclicBarrierCompletionMethodExample { public class CyclicBarrierCompletionMethodExample {
private int count; private int count;
private int threadCount; private int threadCount;
private final List<String> outputScraper; private final AtomicInteger updateCount;
CyclicBarrierCompletionMethodExample(final List<String> outputScraper, int count, int threadCount) { CyclicBarrierCompletionMethodExample(int count, int threadCount) {
this.outputScraper = outputScraper; updateCount = new AtomicInteger(0);
this.count = count; this.count = count;
this.threadCount = threadCount; this.threadCount = threadCount;
} }
@ -22,7 +21,7 @@ public class CyclicBarrierCompletionMethodExample {
public int countTrips() { public int countTrips() {
CyclicBarrier cyclicBarrier = new CyclicBarrier(count, () -> { CyclicBarrier cyclicBarrier = new CyclicBarrier(count, () -> {
outputScraper.add("Barrier is Tripped"); updateCount.incrementAndGet();
}); });
ExecutorService es = Executors.newFixedThreadPool(threadCount); ExecutorService es = Executors.newFixedThreadPool(threadCount);
@ -36,11 +35,11 @@ public class CyclicBarrierCompletionMethodExample {
}); });
} }
es.shutdown(); es.shutdown();
return outputScraper.size(); return updateCount.get();
} }
public static void main(String[] args) { public static void main(String[] args) {
CyclicBarrierCompletionMethodExample ex = new CyclicBarrierCompletionMethodExample(new ArrayList<String>(), 5, 20); CyclicBarrierCompletionMethodExample ex = new CyclicBarrierCompletionMethodExample(5, 20);
System.out.println("Count : " + ex.countTrips()); System.out.println("Count : " + ex.countTrips());
} }
} }

View File

@ -1,20 +1,19 @@
package com.baeldung.concurrent.cyclicbarrier; package com.baeldung.concurrent.cyclicbarrier;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier; import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
public class CyclicBarrierResetExample { public class CyclicBarrierResetExample {
private int count; private int count;
private int threadCount; private int threadCount;
private final List<String> outputScraper; private final AtomicInteger updateCount;
CyclicBarrierResetExample(final List<String> outputScraper, int count, int threadCount) { CyclicBarrierResetExample(int count, int threadCount) {
this.outputScraper = outputScraper; updateCount = new AtomicInteger(0);
this.count = count; this.count = count;
this.threadCount = threadCount; this.threadCount = threadCount;
} }
@ -28,7 +27,7 @@ public class CyclicBarrierResetExample {
es.execute(() -> { es.execute(() -> {
try { try {
if (cyclicBarrier.getNumberWaiting() > 0) { if (cyclicBarrier.getNumberWaiting() > 0) {
outputScraper.add("Count Updated"); updateCount.incrementAndGet();
} }
cyclicBarrier.await(); cyclicBarrier.await();
} catch (InterruptedException | BrokenBarrierException e) { } catch (InterruptedException | BrokenBarrierException e) {
@ -37,11 +36,11 @@ public class CyclicBarrierResetExample {
}); });
} }
es.shutdown(); es.shutdown();
return outputScraper.size(); return updateCount.get();
} }
public static void main(String[] args) { public static void main(String[] args) {
CyclicBarrierResetExample ex = new CyclicBarrierResetExample(new ArrayList<String>(), 7, 20); CyclicBarrierResetExample ex = new CyclicBarrierResetExample(7, 20);
System.out.println("Count : " + ex.countWaits()); System.out.println("Count : " + ex.countWaits());
} }
} }

View File

@ -10,7 +10,7 @@ public class CountdownLatchResetExampleUnitTest {
@Test @Test
public void whenCountDownLatch_noReset() { public void whenCountDownLatch_noReset() {
CountdownLatchResetExample ex = new CountdownLatchResetExample(new ArrayList<>(),5,20); CountdownLatchResetExample ex = new CountdownLatchResetExample(5,20);
int lineCount = ex.countWaits(); int lineCount = ex.countWaits();
assertEquals(5, lineCount); assertEquals(5, lineCount);
} }

View File

@ -2,15 +2,13 @@ package com.baeldung.concurrent.cyclicbarrier;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import org.junit.Test; import org.junit.Test;
public class CyclicBarrierCompletionMethodExampleUnitTest { public class CyclicBarrierCompletionMethodExampleUnitTest {
@Test @Test
public void whenCyclicBarrier_countTrips() { public void whenCyclicBarrier_countTrips() {
CyclicBarrierCompletionMethodExample ex = new CyclicBarrierCompletionMethodExample(new ArrayList<>(),5,20); CyclicBarrierCompletionMethodExample ex = new CyclicBarrierCompletionMethodExample(5,20);
int lineCount = ex.countTrips(); int lineCount = ex.countTrips();
assertEquals(4, lineCount); assertEquals(4, lineCount);
} }

View File

@ -2,15 +2,13 @@ package com.baeldung.concurrent.cyclicbarrier;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import org.junit.Test; import org.junit.Test;
public class CyclicBarrierResetExampleUnitTest { public class CyclicBarrierResetExampleUnitTest {
@Test @Test
public void whenCyclicBarrier_reset() { public void whenCyclicBarrier_reset() {
CyclicBarrierResetExample ex = new CyclicBarrierResetExample(new ArrayList<>(),5,20); CyclicBarrierResetExample ex = new CyclicBarrierResetExample(5,20);
int lineCount = ex.countWaits(); int lineCount = ex.countWaits();
assertEquals(16, lineCount); assertEquals(16, lineCount);
} }