Example to demonstrate differences between CyclicBarrier and CountDownLatch.
This commit is contained in:
+15
@@ -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);
|
||||
}
|
||||
}
|
||||
+17
@@ -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);
|
||||
}
|
||||
}
|
||||
+17
@@ -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);
|
||||
}
|
||||
}
|
||||
+15
@@ -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);
|
||||
}
|
||||
}
|
||||
+17
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user