Example to demonstrate differences between CyclicBarrier and CountDownLatch.

This commit is contained in:
RanjeetKaur17
2018-10-19 19:58:50 +04:00
parent 3a7c3af33a
commit e5f43622be
10 changed files with 280 additions and 0 deletions
@@ -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);
}
}