diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/countdownlatch/CountdownLatchCountExample.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/countdownlatch/CountdownLatchCountExample.java new file mode 100644 index 0000000000..08c7eeec03 --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/countdownlatch/CountdownLatchCountExample.java @@ -0,0 +1,33 @@ +package com.baeldung.concurrent.countdownlatch; + +import java.util.concurrent.CountDownLatch; + +public class CountdownLatchCountExample { + + private int count; + + public CountdownLatchCountExample(int count) { + this.count = count; + } + + public boolean callTwiceInSameThread() { + CountDownLatch countDownLatch = new CountDownLatch(count); + Thread t = new Thread(() -> { + countDownLatch.countDown(); + countDownLatch.countDown(); + }); + t.start(); + + try { + countDownLatch.await(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + return countDownLatch.getCount() == 0; + } + + public static void main(String[] args) { + CountdownLatchCountExample ex = new CountdownLatchCountExample(2); + System.out.println("Is CountDown Completed : " + ex.callTwiceInSameThread()); + } +} 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 new file mode 100644 index 0000000000..1828b7f91e --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/countdownlatch/CountdownLatchResetExample.java @@ -0,0 +1,41 @@ +package com.baeldung.concurrent.countdownlatch; + +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 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); + for (int i = 0; i < threadCount; i++) { + es.execute(() -> { + long prevValue = countDownLatch.getCount(); + countDownLatch.countDown(); + if (countDownLatch.getCount() != prevValue) { + updateCount.incrementAndGet(); + } + }); + } + + es.shutdown(); + return updateCount.get(); + } + + public static void main(String[] args) { + 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 new file mode 100644 index 0000000000..7c1299da62 --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierCompletionMethodExample.java @@ -0,0 +1,45 @@ +package com.baeldung.concurrent.cyclicbarrier; + +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 AtomicInteger updateCount; + + CyclicBarrierCompletionMethodExample(int count, int threadCount) { + updateCount = new AtomicInteger(0); + this.count = count; + this.threadCount = threadCount; + } + + public int countTrips() { + + CyclicBarrier cyclicBarrier = new CyclicBarrier(count, () -> { + updateCount.incrementAndGet(); + }); + + ExecutorService es = Executors.newFixedThreadPool(threadCount); + for (int i = 0; i < threadCount; i++) { + es.execute(() -> { + try { + cyclicBarrier.await(); + } catch (InterruptedException | BrokenBarrierException e) { + e.printStackTrace(); + } + }); + } + es.shutdown(); + return updateCount.get(); + } + + public static void main(String[] args) { + 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/CyclicBarrierCountExample.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierCountExample.java new file mode 100644 index 0000000000..9d637b428b --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierCountExample.java @@ -0,0 +1,32 @@ +package com.baeldung.concurrent.cyclicbarrier; + +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CyclicBarrier; + +public class CyclicBarrierCountExample { + + private int count; + + public CyclicBarrierCountExample(int count) { + this.count = count; + } + + public boolean callTwiceInSameThread() { + CyclicBarrier cyclicBarrier = new CyclicBarrier(count); + Thread t = new Thread(() -> { + try { + cyclicBarrier.await(); + cyclicBarrier.await(); + } catch (InterruptedException | BrokenBarrierException e) { + e.printStackTrace(); + } + }); + t.start(); + return cyclicBarrier.isBroken(); + } + + public static void main(String[] args) { + CyclicBarrierCountExample ex = new CyclicBarrierCountExample(7); + System.out.println("Count : " + ex.callTwiceInSameThread()); + } +} 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 new file mode 100644 index 0000000000..76b6198bc4 --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierResetExample.java @@ -0,0 +1,46 @@ +package com.baeldung.concurrent.cyclicbarrier; + +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 AtomicInteger updateCount; + + CyclicBarrierResetExample(int count, int threadCount) { + updateCount = new AtomicInteger(0); + this.count = count; + this.threadCount = threadCount; + } + + public int countWaits() { + + CyclicBarrier cyclicBarrier = new CyclicBarrier(count); + + ExecutorService es = Executors.newFixedThreadPool(threadCount); + for (int i = 0; i < threadCount; i++) { + es.execute(() -> { + try { + if (cyclicBarrier.getNumberWaiting() > 0) { + updateCount.incrementAndGet(); + } + cyclicBarrier.await(); + } catch (InterruptedException | BrokenBarrierException e) { + e.printStackTrace(); + } + }); + } + es.shutdown(); + return updateCount.get(); + } + + public static void main(String[] args) { + 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/CountdownLatchCountExampleUnitTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/countdownlatch/CountdownLatchCountExampleUnitTest.java new file mode 100644 index 0000000000..835efa53f2 --- /dev/null +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/countdownlatch/CountdownLatchCountExampleUnitTest.java @@ -0,0 +1,15 @@ +package com.baeldung.concurrent.countdownlatch; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class CountdownLatchCountExampleUnitTest { + + @Test + public void whenCountDownLatch_completed() { + CountdownLatchCountExample ex = new CountdownLatchCountExample(2); + boolean isCompleted = ex.callTwiceInSameThread(); + assertTrue(isCompleted); + } +} 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 new file mode 100644 index 0000000000..d2d43f6312 --- /dev/null +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/countdownlatch/CountdownLatchResetExampleUnitTest.java @@ -0,0 +1,15 @@ +package com.baeldung.concurrent.countdownlatch; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class CountdownLatchResetExampleUnitTest { + + @Test + public void whenCountDownLatch_noReset() { + CountdownLatchResetExample ex = new CountdownLatchResetExample(7,20); + int lineCount = ex.countWaits(); + assertTrue(lineCount <= 7); + } +} 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 new file mode 100644 index 0000000000..310063c86c --- /dev/null +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierCompletionMethodExampleUnitTest.java @@ -0,0 +1,15 @@ +package com.baeldung.concurrent.cyclicbarrier; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class CyclicBarrierCompletionMethodExampleUnitTest { + + @Test + public void whenCyclicBarrier_countTrips() { + CyclicBarrierCompletionMethodExample ex = new CyclicBarrierCompletionMethodExample(7,20); + int lineCount = ex.countTrips(); + assertEquals(2, lineCount); + } +} diff --git a/core-java-concurrency/src/test/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierCountExampleUnitTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierCountExampleUnitTest.java new file mode 100644 index 0000000000..9b7f3d9945 --- /dev/null +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierCountExampleUnitTest.java @@ -0,0 +1,15 @@ +package com.baeldung.concurrent.cyclicbarrier; + +import static org.junit.Assert.assertFalse; + +import org.junit.Test; + +public class CyclicBarrierCountExampleUnitTest { + + @Test + public void whenCyclicBarrier_notCompleted() { + CyclicBarrierCountExample ex = new CyclicBarrierCountExample(2); + boolean isCompleted = ex.callTwiceInSameThread(); + assertFalse(isCompleted); + } +} 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 new file mode 100644 index 0000000000..8d2b148f06 --- /dev/null +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierResetExampleUnitTest.java @@ -0,0 +1,15 @@ +package com.baeldung.concurrent.cyclicbarrier; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class CyclicBarrierResetExampleUnitTest { + + @Test + public void whenCyclicBarrier_reset() { + CyclicBarrierResetExample ex = new CyclicBarrierResetExample(7,20); + int lineCount = ex.countWaits(); + assertTrue(lineCount > 7); + } +}