commit
e4636b90b0
|
@ -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.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());
|
||||||
|
}
|
||||||
|
}
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
|
@ -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,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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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,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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue