@@ -47,26 +47,15 @@ import lombok.extern.slf4j.Slf4j;
4747 * main thread will continue when CountDownLatch count becomes 0
4848 * CountDownLatch will start with count 4 and 4 demo threads will decrease it by 1
4949 * everytime when they will finish .
50+ * DemoThreads are implemented in join pattern such that every newly created thread
51+ * waits for the completion of previous thread by previous.join() . Hence maintaining
52+ * execution order of demo threads .
53+ * JoinPattern object ensures that dependent threads execute only after completion of
54+ * demo threads by pattern.await() . This method keep the main thread in waiting state
55+ * until countdown latch becomes 0 . CountdownLatch will become 0 as all demo threads
56+ * will be completed as each of them have decreased it by 1 and its initial count was set to noOfDemoThreads.
57+ * Hence this pattern ensures dependent threads will start only after completion of demo threads .
5058 */
51-
52- @Slf4j
53- public class JoinPatternDemo {
54-
55- /**
56- * execution of demo and dependent threads.
57- */
58- public static void main (String [] args ) {
59-
60- int [] executionOrder = {4 , 2 , 1 , 3 };
61- int noOfDemoThreads = 4 ;
62- int noOfDependentThreads = 2 ;
63- JoinPattern pattern = new JoinPattern (noOfDemoThreads, executionOrder);
64- Thread previous = null ;
65-
66- for (int i = 0 ; i < noOfDemoThreads; i++ ) {
67- previous = new Thread (new DemoThread (executionOrder[i], previous));
68- previous. start();
69- }
7059 pattern. await();
7160
7261 // Dependent threads after execution of DemoThreads
@@ -75,25 +64,43 @@ public class JoinPatternDemo {
7564 }
7665 LOGGER . info(" end of program " );
7766
78- }
67+ /**
68+ * use to run demo thread.
69+ * every newly created thread waits for
70+ * the completion of previous thread
71+ * by previous.join() .
72+ */
73+ @Override
74+ public void run() {
75+ if (previous != null ) {
76+ try {
77+ previous. join();
78+ } catch (InterruptedException e) {
79+ Thread . currentThread(). interrupt();
80+ LOGGER . error(" Interrupted exception : " , e);
81+ }
82+ }
7983
80- }
8184
8285```
8386
8487### Program Output :
8588
86- ```
87- Running com.iluwatar.join.JoinPatternTest
88- 01:13:17.890 [Thread-2] INFO com.iluwatar.join.DemoThread -- Thread 1 starts
89- 01:13:18.167 [Thread-2] INFO com.iluwatar.join.DemoThread -- Thread 1 ends
90- 01:13:18.168 [Thread-3] INFO com.iluwatar.join.DemoThread -- Thread 4 starts
91- 01:13:19.176 [Thread-3] INFO com.iluwatar.join.DemoThread -- Thread 4 ends
92- 01:13:19.176 [Thread-4] INFO com.iluwatar.join.DemoThread -- Thread 3 starts
93- 01:13:19.935 [Thread-4] INFO com.iluwatar.join.DemoThread -- Thread 3 ends
94- 01:13:19.935 [Thread-5] INFO com.iluwatar.join.DemoThread -- Thread 2 starts
95- 01:13:20.437 [Thread-5] INFO com.iluwatar.join.DemoThread -- Thread 2 ends
96- ```
89+ [INFO ] Running com.iluwatar.join. JoinPatternTest
90+ 16 : 12 : 01 . 815 [Thread - 2 ] INFO com.iluwatar.join. DemoThread -- Thread 1 starts
91+ 16 : 12 : 02 . 086 [Thread - 2 ] INFO com.iluwatar.join. DemoThread -- Thread 1 ends
92+ 16 : 12 : 02 . 087 [Thread - 3 ] INFO com.iluwatar.join. DemoThread -- Thread 4 starts
93+ 16 : 12 : 03 . 090 [Thread - 3 ] INFO com.iluwatar.join. DemoThread -- Thread 4 ends
94+ 16 : 12 : 03 . 091 [Thread - 4 ] INFO com.iluwatar.join. DemoThread -- Thread 3 starts
95+ 16 : 12 : 03 . 851 [Thread - 4 ] INFO com.iluwatar.join. DemoThread -- Thread 3 ends
96+ 16 : 12 : 03 . 851 [Thread - 5 ] INFO com.iluwatar.join. DemoThread -- Thread 2 starts
97+ 16 : 12 : 04 . 352 [Thread - 5 ] INFO com.iluwatar.join. DemoThread -- Thread 2 ends
98+ [INFO ] Tests run: 1 , Failures : 0 , Errors : 0 , Skipped : 0 , Time elapsed: 2.904 s -- in com.iluwatar.join. JoinPatternTest
99+ [INFO ]
100+ [INFO ] Results :
101+ [INFO ]
102+ [INFO ] Tests run: 1 , Failures : 0 , Errors : 0 , Skipped : 0
103+ [INFO ]
97104
98105## When to Use the Join Pattern in Java
99106
0 commit comments