Skip to content

Commit 21649ab

Browse files
committed
BAEL-613 - Added workers which wait for all other workers to start
1 parent 65e7c7f commit 21649ab

2 files changed

Lines changed: 73 additions & 4 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.concurrent.countdownlatch;
2+
3+
import java.util.List;
4+
import java.util.concurrent.CountDownLatch;
5+
6+
public class WaitingWorker implements Runnable {
7+
8+
private final List<String> outputScraper;
9+
private final CountDownLatch readyThreadCounter;
10+
private final CountDownLatch callingThreadBlocker;
11+
private final CountDownLatch completedThreadCounter;
12+
13+
public WaitingWorker(final List<String> outputScraper,
14+
final CountDownLatch readyThreadCounter,
15+
final CountDownLatch callingThreadBlocker,
16+
CountDownLatch completedThreadCounter) {
17+
18+
this.outputScraper = outputScraper;
19+
this.readyThreadCounter = readyThreadCounter;
20+
this.callingThreadBlocker = callingThreadBlocker;
21+
this.completedThreadCounter = completedThreadCounter;
22+
}
23+
24+
@Override
25+
public void run() {
26+
// Mark this thread as read / started
27+
readyThreadCounter.countDown();
28+
try {
29+
callingThreadBlocker.await();
30+
outputScraper.add("Counted down");
31+
} catch (InterruptedException e) {
32+
e.printStackTrace();
33+
} finally {
34+
completedThreadCounter.countDown();
35+
}
36+
}
37+
}

core-java/src/test/java/com/baeldung/concurrent/countdownlatch/CountdownLatchExampleTest.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package com.baeldung.concurrent.countdownlatch;
22

3-
import org.assertj.core.api.Assertions;
43
import org.junit.Test;
54

65
import java.util.ArrayList;
76
import java.util.Collections;
87
import java.util.List;
98
import java.util.concurrent.CountDownLatch;
109
import java.util.concurrent.TimeUnit;
11-
import java.util.concurrent.TimeoutException;
1210
import java.util.stream.Stream;
1311

1412
import static java.util.stream.Collectors.toList;
@@ -17,7 +15,6 @@
1715
public class CountdownLatchExampleTest {
1816
@Test
1917
public void whenParallelProcessing_thenMainThreadWillBlockUntilCompletion() throws InterruptedException {
20-
2118
// Given
2219
List<String> outputScraper = Collections.synchronizedList(new ArrayList<>());
2320
CountDownLatch countDownLatch = new CountDownLatch(5);
@@ -59,6 +56,41 @@ public void whenFailingToParallelProcess_thenMainThreadShouldTimeout() throws In
5956
final boolean result = countDownLatch.await(3L, TimeUnit.SECONDS);
6057

6158
// Then
62-
assertThat(result).isTrue();
59+
assertThat(result).isFalse();
60+
}
61+
62+
@Test
63+
public void whenDoingLotsOfThreadsInParallel_thenStartThemAtTheSameTime() throws InterruptedException {
64+
// Given
65+
List<String> outputScraper = Collections.synchronizedList(new ArrayList<>());
66+
CountDownLatch readyThreadCounter = new CountDownLatch(5);
67+
CountDownLatch callingThreadBlocker = new CountDownLatch(1);
68+
CountDownLatch completedThreadCounter = new CountDownLatch(5);
69+
List<Thread> workers = Stream
70+
.generate(() -> new Thread(new WaitingWorker(outputScraper, readyThreadCounter, callingThreadBlocker, completedThreadCounter)))
71+
.limit(5)
72+
.collect(toList());
73+
74+
// When
75+
workers.forEach(Thread::start);
76+
readyThreadCounter.await(); // Block until workers start
77+
outputScraper.add("Workers ready");
78+
callingThreadBlocker.countDown(); // Start workers
79+
completedThreadCounter.await(); // Block until workers finish
80+
outputScraper.add("Workers complete");
81+
82+
// Then
83+
outputScraper.forEach(Object::toString);
84+
assertThat(outputScraper)
85+
.containsExactly(
86+
"Workers ready",
87+
"Counted down",
88+
"Counted down",
89+
"Counted down",
90+
"Counted down",
91+
"Counted down",
92+
"Workers complete"
93+
);
6394
}
95+
6496
}

0 commit comments

Comments
 (0)