Skip to content

Commit e4636b9

Browse files
authored
Merge pull request eugenp#5492 from RanjeetKaur17/BAEL-2186
Bael 2186
2 parents 26a6cd6 + 39a828f commit e4636b9

10 files changed

Lines changed: 272 additions & 0 deletions
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.concurrent.countdownlatch;
2+
3+
import java.util.concurrent.CountDownLatch;
4+
5+
public class CountdownLatchCountExample {
6+
7+
private int count;
8+
9+
public CountdownLatchCountExample(int count) {
10+
this.count = count;
11+
}
12+
13+
public boolean callTwiceInSameThread() {
14+
CountDownLatch countDownLatch = new CountDownLatch(count);
15+
Thread t = new Thread(() -> {
16+
countDownLatch.countDown();
17+
countDownLatch.countDown();
18+
});
19+
t.start();
20+
21+
try {
22+
countDownLatch.await();
23+
} catch (InterruptedException e) {
24+
e.printStackTrace();
25+
}
26+
return countDownLatch.getCount() == 0;
27+
}
28+
29+
public static void main(String[] args) {
30+
CountdownLatchCountExample ex = new CountdownLatchCountExample(2);
31+
System.out.println("Is CountDown Completed : " + ex.callTwiceInSameThread());
32+
}
33+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.concurrent.countdownlatch;
2+
3+
import java.util.concurrent.CountDownLatch;
4+
import java.util.concurrent.ExecutorService;
5+
import java.util.concurrent.Executors;
6+
import java.util.concurrent.atomic.AtomicInteger;
7+
8+
public class CountdownLatchResetExample {
9+
10+
private int count;
11+
private int threadCount;
12+
private final AtomicInteger updateCount;
13+
14+
CountdownLatchResetExample(int count, int threadCount) {
15+
updateCount = new AtomicInteger(0);
16+
this.count = count;
17+
this.threadCount = threadCount;
18+
}
19+
20+
public int countWaits() {
21+
CountDownLatch countDownLatch = new CountDownLatch(count);
22+
ExecutorService es = Executors.newFixedThreadPool(threadCount);
23+
for (int i = 0; i < threadCount; i++) {
24+
es.execute(() -> {
25+
long prevValue = countDownLatch.getCount();
26+
countDownLatch.countDown();
27+
if (countDownLatch.getCount() != prevValue) {
28+
updateCount.incrementAndGet();
29+
}
30+
});
31+
}
32+
33+
es.shutdown();
34+
return updateCount.get();
35+
}
36+
37+
public static void main(String[] args) {
38+
CountdownLatchResetExample ex = new CountdownLatchResetExample(5, 20);
39+
System.out.println("Count : " + ex.countWaits());
40+
}
41+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.concurrent.cyclicbarrier;
2+
3+
import java.util.concurrent.BrokenBarrierException;
4+
import java.util.concurrent.CyclicBarrier;
5+
import java.util.concurrent.ExecutorService;
6+
import java.util.concurrent.Executors;
7+
import java.util.concurrent.atomic.AtomicInteger;
8+
9+
public class CyclicBarrierCompletionMethodExample {
10+
11+
private int count;
12+
private int threadCount;
13+
private final AtomicInteger updateCount;
14+
15+
CyclicBarrierCompletionMethodExample(int count, int threadCount) {
16+
updateCount = new AtomicInteger(0);
17+
this.count = count;
18+
this.threadCount = threadCount;
19+
}
20+
21+
public int countTrips() {
22+
23+
CyclicBarrier cyclicBarrier = new CyclicBarrier(count, () -> {
24+
updateCount.incrementAndGet();
25+
});
26+
27+
ExecutorService es = Executors.newFixedThreadPool(threadCount);
28+
for (int i = 0; i < threadCount; i++) {
29+
es.execute(() -> {
30+
try {
31+
cyclicBarrier.await();
32+
} catch (InterruptedException | BrokenBarrierException e) {
33+
e.printStackTrace();
34+
}
35+
});
36+
}
37+
es.shutdown();
38+
return updateCount.get();
39+
}
40+
41+
public static void main(String[] args) {
42+
CyclicBarrierCompletionMethodExample ex = new CyclicBarrierCompletionMethodExample(5, 20);
43+
System.out.println("Count : " + ex.countTrips());
44+
}
45+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.concurrent.cyclicbarrier;
2+
3+
import java.util.concurrent.BrokenBarrierException;
4+
import java.util.concurrent.CyclicBarrier;
5+
6+
public class CyclicBarrierCountExample {
7+
8+
private int count;
9+
10+
public CyclicBarrierCountExample(int count) {
11+
this.count = count;
12+
}
13+
14+
public boolean callTwiceInSameThread() {
15+
CyclicBarrier cyclicBarrier = new CyclicBarrier(count);
16+
Thread t = new Thread(() -> {
17+
try {
18+
cyclicBarrier.await();
19+
cyclicBarrier.await();
20+
} catch (InterruptedException | BrokenBarrierException e) {
21+
e.printStackTrace();
22+
}
23+
});
24+
t.start();
25+
return cyclicBarrier.isBroken();
26+
}
27+
28+
public static void main(String[] args) {
29+
CyclicBarrierCountExample ex = new CyclicBarrierCountExample(7);
30+
System.out.println("Count : " + ex.callTwiceInSameThread());
31+
}
32+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.concurrent.cyclicbarrier;
2+
3+
import java.util.concurrent.BrokenBarrierException;
4+
import java.util.concurrent.CyclicBarrier;
5+
import java.util.concurrent.ExecutorService;
6+
import java.util.concurrent.Executors;
7+
import java.util.concurrent.atomic.AtomicInteger;
8+
9+
public class CyclicBarrierResetExample {
10+
11+
private int count;
12+
private int threadCount;
13+
private final AtomicInteger updateCount;
14+
15+
CyclicBarrierResetExample(int count, int threadCount) {
16+
updateCount = new AtomicInteger(0);
17+
this.count = count;
18+
this.threadCount = threadCount;
19+
}
20+
21+
public int countWaits() {
22+
23+
CyclicBarrier cyclicBarrier = new CyclicBarrier(count);
24+
25+
ExecutorService es = Executors.newFixedThreadPool(threadCount);
26+
for (int i = 0; i < threadCount; i++) {
27+
es.execute(() -> {
28+
try {
29+
if (cyclicBarrier.getNumberWaiting() > 0) {
30+
updateCount.incrementAndGet();
31+
}
32+
cyclicBarrier.await();
33+
} catch (InterruptedException | BrokenBarrierException e) {
34+
e.printStackTrace();
35+
}
36+
});
37+
}
38+
es.shutdown();
39+
return updateCount.get();
40+
}
41+
42+
public static void main(String[] args) {
43+
CyclicBarrierResetExample ex = new CyclicBarrierResetExample(7, 20);
44+
System.out.println("Count : " + ex.countWaits());
45+
}
46+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.concurrent.countdownlatch;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import org.junit.Test;
6+
7+
public class CountdownLatchCountExampleUnitTest {
8+
9+
@Test
10+
public void whenCountDownLatch_completed() {
11+
CountdownLatchCountExample ex = new CountdownLatchCountExample(2);
12+
boolean isCompleted = ex.callTwiceInSameThread();
13+
assertTrue(isCompleted);
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.concurrent.countdownlatch;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import org.junit.Test;
6+
7+
public class CountdownLatchResetExampleUnitTest {
8+
9+
@Test
10+
public void whenCountDownLatch_noReset() {
11+
CountdownLatchResetExample ex = new CountdownLatchResetExample(7,20);
12+
int lineCount = ex.countWaits();
13+
assertTrue(lineCount <= 7);
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.concurrent.cyclicbarrier;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
public class CyclicBarrierCompletionMethodExampleUnitTest {
8+
9+
@Test
10+
public void whenCyclicBarrier_countTrips() {
11+
CyclicBarrierCompletionMethodExample ex = new CyclicBarrierCompletionMethodExample(7,20);
12+
int lineCount = ex.countTrips();
13+
assertEquals(2, lineCount);
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.concurrent.cyclicbarrier;
2+
3+
import static org.junit.Assert.assertFalse;
4+
5+
import org.junit.Test;
6+
7+
public class CyclicBarrierCountExampleUnitTest {
8+
9+
@Test
10+
public void whenCyclicBarrier_notCompleted() {
11+
CyclicBarrierCountExample ex = new CyclicBarrierCountExample(2);
12+
boolean isCompleted = ex.callTwiceInSameThread();
13+
assertFalse(isCompleted);
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.concurrent.cyclicbarrier;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import org.junit.Test;
6+
7+
public class CyclicBarrierResetExampleUnitTest {
8+
9+
@Test
10+
public void whenCyclicBarrier_reset() {
11+
CyclicBarrierResetExample ex = new CyclicBarrierResetExample(7,20);
12+
int lineCount = ex.countWaits();
13+
assertTrue(lineCount > 7);
14+
}
15+
}

0 commit comments

Comments
 (0)