1+ package com .baeldung .concurrent .countdownlatch ;
2+
3+ import org .junit .Test ;
4+
5+ import java .util .ArrayList ;
6+ import java .util .Collections ;
7+ import java .util .List ;
8+ import java .util .concurrent .CountDownLatch ;
9+ import java .util .concurrent .TimeUnit ;
10+ import java .util .stream .Stream ;
11+
12+ import static java .util .stream .Collectors .toList ;
13+ import static org .assertj .core .api .Assertions .assertThat ;
14+
15+ public class CountdownLatchExampleTest {
16+ @ Test
17+ public void whenParallelProcessing_thenMainThreadWillBlockUntilCompletion () throws InterruptedException {
18+ // Given
19+ List <String > outputScraper = Collections .synchronizedList (new ArrayList <>());
20+ CountDownLatch countDownLatch = new CountDownLatch (5 );
21+ List <Thread > workers = Stream
22+ .generate (() -> new Thread (new Worker (outputScraper , countDownLatch )))
23+ .limit (5 )
24+ .collect (toList ());
25+
26+ // When
27+ workers .forEach (Thread ::start );
28+ countDownLatch .await (); // Block until workers finish
29+ outputScraper .add ("Latch released" );
30+
31+ // Then
32+ outputScraper .forEach (Object ::toString );
33+ assertThat (outputScraper )
34+ .containsExactly (
35+ "Counted down" ,
36+ "Counted down" ,
37+ "Counted down" ,
38+ "Counted down" ,
39+ "Counted down" ,
40+ "Latch released"
41+ );
42+ }
43+
44+ @ Test
45+ public void whenFailingToParallelProcess_thenMainThreadShouldTimeout () throws InterruptedException {
46+ // Given
47+ List <String > outputScraper = Collections .synchronizedList (new ArrayList <>());
48+ CountDownLatch countDownLatch = new CountDownLatch (5 );
49+ List <Thread > workers = Stream
50+ .generate (() -> new Thread (new BrokenWorker (outputScraper , countDownLatch )))
51+ .limit (5 )
52+ .collect (toList ());
53+
54+ // When
55+ workers .forEach (Thread ::start );
56+ final boolean result = countDownLatch .await (3L , TimeUnit .SECONDS );
57+
58+ // Then
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+ );
94+ }
95+
96+ }
0 commit comments