Example to demonstrate differences between CyclicBarrier and CountDownLatch.
This commit is contained in:
		
							parent
							
								
									3a7c3af33a
								
							
						
					
					
						commit
						e5f43622be
					
				| @ -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()); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,41 @@ | ||||
| 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; | ||||
| 
 | ||||
| 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; | ||||
|         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(() -> { | ||||
|                 if (countDownLatch.getCount() > 0) { | ||||
|                     outputScraper.add("Count Left : " + countDownLatch.getCount()); | ||||
|                 } | ||||
|                 countDownLatch.countDown(); | ||||
|             }); | ||||
|         } | ||||
|          | ||||
|         es.shutdown(); | ||||
|         return outputScraper.size(); | ||||
|     } | ||||
| 
 | ||||
|     public static void main(String[] args) { | ||||
|         CountdownLatchResetExample ex = new CountdownLatchResetExample(new ArrayList<>(),5,20); | ||||
|         System.out.println("Count : " + ex.countWaits()); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,46 @@ | ||||
| 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; | ||||
| 
 | ||||
| public class CyclicBarrierCompletionMethodExample { | ||||
| 
 | ||||
|     private int count; | ||||
|     private int threadCount; | ||||
|     private final List<String> outputScraper; | ||||
|      | ||||
|     CyclicBarrierCompletionMethodExample(final List<String> outputScraper, int count, int threadCount) { | ||||
|         this.outputScraper = outputScraper; | ||||
|         this.count = count; | ||||
|         this.threadCount = threadCount; | ||||
|     } | ||||
| 
 | ||||
|     public int countTrips() { | ||||
| 
 | ||||
|         CyclicBarrier cyclicBarrier = new CyclicBarrier(count, () -> { | ||||
|             outputScraper.add("Barrier is Tripped"); | ||||
|         }); | ||||
| 
 | ||||
|         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 outputScraper.size(); | ||||
|     } | ||||
| 
 | ||||
|     public static void main(String[] args) { | ||||
|         CyclicBarrierCompletionMethodExample ex = new CyclicBarrierCompletionMethodExample(new ArrayList<String>(), 5, 20); | ||||
|         System.out.println("Count : " + ex.countTrips()); | ||||
|     } | ||||
| } | ||||
| @ -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(1); | ||||
|         System.out.println("Count : " + ex.callTwiceInSameThread()); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,47 @@ | ||||
| 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; | ||||
| 
 | ||||
| public class CyclicBarrierResetExample { | ||||
| 
 | ||||
|     private int count; | ||||
|     private int threadCount; | ||||
|     private final List<String> outputScraper; | ||||
| 
 | ||||
|     CyclicBarrierResetExample(final List<String> outputScraper, int count, int threadCount) { | ||||
|         this.outputScraper = outputScraper; | ||||
|         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) { | ||||
|                         outputScraper.add("Waiting Count : " + cyclicBarrier.getNumberWaiting()); | ||||
|                     } | ||||
|                     cyclicBarrier.await(); | ||||
|                 } catch (InterruptedException | BrokenBarrierException e) { | ||||
|                     e.printStackTrace(); | ||||
|                 } | ||||
|             }); | ||||
|         } | ||||
|         es.shutdown(); | ||||
|         return outputScraper.size(); | ||||
|     } | ||||
| 
 | ||||
|     public static void main(String[] args) { | ||||
|         CyclicBarrierResetExample ex = new CyclicBarrierResetExample(new ArrayList<String>(), 5, 20); | ||||
|         System.out.println("Count : " + ex.countWaits()); | ||||
|     } | ||||
| } | ||||
| @ -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); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,17 @@ | ||||
| package com.baeldung.concurrent.countdownlatch; | ||||
| 
 | ||||
| import static org.junit.Assert.assertEquals; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| 
 | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| public class CountdownLatchResetExampleUnitTest { | ||||
|      | ||||
|     @Test | ||||
|     public void whenCountDownLatch_noReset() { | ||||
|         CountdownLatchResetExample ex = new CountdownLatchResetExample(new ArrayList<>(),5,20); | ||||
|         int lineCount = ex.countWaits(); | ||||
|         assertEquals(5, lineCount); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,17 @@ | ||||
| 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); | ||||
|         int lineCount = ex.countTrips(); | ||||
|         assertEquals(4, lineCount); | ||||
|     } | ||||
| } | ||||
| @ -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); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,17 @@ | ||||
| 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); | ||||
|         int lineCount = ex.countWaits(); | ||||
|         assertEquals(16, lineCount); | ||||
|     } | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user