55import java .util .concurrent .Executors ;
66
77/**
8- * CountDownLatch Java class to synchronize your threads’ activities.
9- *
8+ * {@link java.util.concurrent. CountDownLatch} Java class to synchronize your threads’ activities.
9+ * <br><br>
1010 * Source:
11- * (http://stackoverflow.com/questions/17827022/what-is-countdown-latch-in-java-multithreading)
11+ * <em>http://stackoverflow.com/questions/17827022/what-is-countdown-latch-in-java-multithreading</em><br>
12+ *
1213 * Any thread, usually main thread of application, which calls
13- * CountDownLatch. await() will wait until count reaches zero or its interrupted
14+ * {@link java.util.concurrent.CountDownLatch# await()} will wait until count reaches zero or its interrupted
1415 * by another thread. All other thread are required to do count down by calling
15- * CountDownLatch. countDown() once they are completed or ready.
16- *
16+ * {@link java.util.concurrent.CountDownLatch# countDown()} once they are completed or ready.
17+ * <br>
1718 * As soon as count reaches zero, Thread awaiting starts running. One of the
18- * disadvantage of CountDownLatch is that its not reusable once count reaches to
19- * zero you can not use CountDownLatch any more.
20- *
21- * Use CountDownLatch when one thread like main thread, require to wait for one
22- * or more thread to complete, before it can start processing.
23- *
24- * Classical example of using CountDownLatch in Java is any server side core
25- * Java application which uses services architecture, where multiple services
19+ * disadvantage of {@link java.util.concurrent.CountDownLatch} is that it's
20+ * not reusable once the count reaches to
21+ * zero you can not use {@link java.util.concurrent.CountDownLatch} any more.
22+ * <br><br>
23+ * Use {@link java.util.concurrent.CountDownLatch} when one thread, like main
24+ * thread, require to wait for one or more threads to complete, before it can
25+ * start processing.
26+ * <br><br>
27+ * Classical example of using {@link java.util.concurrent.CountDownLatch} in
28+ * Java is any server side core Java application which uses services
29+ * architecture, where multiple services
2630 * are provided by multiple threads and application can not start processing
2731 * until all services have started successfully.
28- *
29- * Codes with minor comments are from http://www.caveofprogramming.com/youtube/
32+ * <br><br>
33+ * Codes with minor comments are from <em> http://www.caveofprogramming.com/youtube/</em><br>
3034 * also freely available at
31- * https://www.udemy.com/java-multithreading/?couponCode=FREE
35+ * <em> https://www.udemy.com/java-multithreading/?couponCode=FREE</em>
3236 *
3337 * @author Z.B. Celik <celik.berkay@gmail.com>
3438 */
@@ -45,10 +49,7 @@ public void run() {
4549
4650 try {
4751 Thread .sleep (3000 );
48- } catch (InterruptedException e ) {
49- // TODO Auto-generated catch block
50- e .printStackTrace ();
51- }
52+ } catch (InterruptedException ignored ) {}
5253 latch .countDown ();
5354 }
5455}
@@ -61,8 +62,10 @@ public static void main(String[] args) {
6162 for (int i = 0 ; i < 3 ; i ++) {
6263 executor .submit (new Processor (latch ));
6364 }
65+ executor .shutdown ();
66+
6467 try {
65- // Application’s main thread waits, till other service threads which are
68+ // Application’s main thread waits, till other service threads which are
6669 // as an example responsible for starting framework services have completed started all services.
6770 latch .await ();
6871 } catch (InterruptedException e ) {
0 commit comments