99import java .util .concurrent .*;
1010
1111import static junit .framework .TestCase .assertTrue ;
12+ import static org .junit .Assert .fail ;
1213
1314public class WaitingForThreadsToFinishTest {
1415
1516 private static final Logger LOG = LoggerFactory .getLogger (WaitingForThreadsToFinishTest .class );
1617 private final static ExecutorService WORKER_THREAD_POOL = Executors .newFixedThreadPool (10 );
1718
19+ public void awaitTerminationAfterShutdown (ExecutorService threadPool ) {
20+ threadPool .shutdown ();
21+ try {
22+ if (!threadPool .awaitTermination (60 , TimeUnit .SECONDS )) {
23+ threadPool .shutdownNow ();
24+ }
25+ } catch (InterruptedException ex ) {
26+ threadPool .shutdownNow ();
27+ Thread .currentThread ().interrupt ();
28+ }
29+ }
30+
31+ @ Test
32+ public void givenMultipleThreads_whenUsingCountDownLatch_thenMainShoudWaitForAllToFinish () {
33+
34+ ExecutorService WORKER_THREAD_POOL = Executors .newFixedThreadPool (10 );
35+
36+ try {
37+ long startTime = System .currentTimeMillis ();
38+
39+ // create a CountDownLatch that waits for the 2 threads to finish
40+ CountDownLatch latch = new CountDownLatch (2 );
41+
42+ for (int i = 0 ; i < 2 ; i ++) {
43+ WORKER_THREAD_POOL .submit (new Runnable () {
44+ @ Override
45+ public void run () {
46+ try {
47+ Thread .sleep (1000 );
48+ latch .countDown ();
49+ } catch (InterruptedException e ) {
50+ e .printStackTrace ();
51+ Thread .currentThread ().interrupt ();
52+ }
53+ }
54+ });
55+ }
56+
57+ // wait for the latch to be decremented by the two threads
58+ latch .await ();
59+
60+ long processingTime = System .currentTimeMillis () - startTime ;
61+ assertTrue (processingTime >= 1000 );
62+
63+ } catch (InterruptedException e ) {
64+ e .printStackTrace ();
65+ }
66+
67+ awaitTerminationAfterShutdown (WORKER_THREAD_POOL );
68+ }
69+
1870 @ Test
1971 public void givenMultipleThreads_whenInvokeAll_thenMainThreadShouldWaitForAllToFinish () {
2072
2173 ExecutorService WORKER_THREAD_POOL = Executors .newFixedThreadPool (10 );
2274
23- List <Callable <String >> callables = Arrays .asList (new DelayedCallable ("fast thread" , 100 ), new DelayedCallable ("slow thread" , 3000 ));
24-
75+ List <Callable <String >> callables = Arrays .asList (
76+ new DelayedCallable ("fast thread" , 100 ),
77+ new DelayedCallable ("slow thread" , 3000 ));
78+
2579 try {
2680 long startProcessingTime = System .currentTimeMillis ();
2781 List <Future <String >> futures = WORKER_THREAD_POOL .invokeAll (callables );
82+
83+ awaitTerminationAfterShutdown (WORKER_THREAD_POOL );
84+
85+ try {
86+ WORKER_THREAD_POOL .submit (new Callable <String >() {
87+ @ Override
88+ public String call () throws Exception {
89+ fail ("This thread should have been rejected !" );
90+ Thread .sleep (1000000 );
91+ return null ;
92+ }
93+ });
94+ } catch (RejectedExecutionException ex ) {
95+ //
96+ }
2897
2998 long totalProcessingTime = System .currentTimeMillis () - startProcessingTime ;
3099 assertTrue (totalProcessingTime >= 3000 );
@@ -39,24 +108,22 @@ public void givenMultipleThreads_whenInvokeAll_thenMainThreadShouldWaitForAllToF
39108
40109 } catch (ExecutionException | InterruptedException ex ) {
41110 ex .printStackTrace ();
42- }
43-
44- WORKER_THREAD_POOL .shutdown ();
111+ }
45112 }
46113
47114 @ Test
48115 public void givenMultipleThreads_whenUsingCompletionService_thenMainThreadShouldWaitForAllToFinish () {
49116
50117 CompletionService <String > service = new ExecutorCompletionService <>(WORKER_THREAD_POOL );
51118
52- List <Callable <String >> callables = Arrays .asList (new DelayedCallable ("fast thread" , 100 ), new DelayedCallable ("slow thread" , 3000 ));
119+ List <Callable <String >> callables = Arrays .asList (
120+ new DelayedCallable ("fast thread" , 100 ),
121+ new DelayedCallable ("slow thread" , 3000 ));
53122
54123 for (Callable <String > callable : callables ) {
55124 service .submit (callable );
56125 }
57126
58- WORKER_THREAD_POOL .shutdown ();
59-
60127 try {
61128
62129 long startProcessingTime = System .currentTimeMillis ();
@@ -79,8 +146,9 @@ public void givenMultipleThreads_whenUsingCompletionService_thenMainThreadShould
79146
80147 } catch (ExecutionException | InterruptedException ex ) {
81148 ex .printStackTrace ();
149+ } finally {
150+ awaitTerminationAfterShutdown (WORKER_THREAD_POOL );
82151 }
83-
84152 }
85153
86154 @ Test
@@ -142,6 +210,6 @@ public void givenMultipleThreads_whenUsingCompletableFutures_thenMainThreadShoul
142210 e .printStackTrace ();
143211 }
144212
145- WORKER_THREAD_POOL . shutdown ( );
213+ awaitTerminationAfterShutdown ( WORKER_THREAD_POOL );
146214 }
147215}
0 commit comments