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;
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<String> outputScraper;
CountdownLatchResetExample(final List<String> 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());
}
}

View File

@ -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<String> outputScraper;
private final AtomicInteger updateCount;
CyclicBarrierCompletionMethodExample(final List<String> 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<String>(), 5, 20);
CyclicBarrierCompletionMethodExample ex = new CyclicBarrierCompletionMethodExample(5, 20);
System.out.println("Count : " + ex.countTrips());
}
}

View File

@ -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<String> outputScraper;
private final AtomicInteger updateCount;
CyclicBarrierResetExample(final List<String> 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<String>(), 7, 20);
CyclicBarrierResetExample ex = new CyclicBarrierResetExample(7, 20);
System.out.println("Count : " + ex.countWaits());
}
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}