Skip to content

Commit 3b4bc80

Browse files
committed
updated README.MD
1 parent 2ca371c commit 3b4bc80

File tree

4 files changed

+64
-46
lines changed

4 files changed

+64
-46
lines changed

join/README.md

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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

join/src/main/java/com/iluwatar/join/DemoThread.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,14 @@
3232
@Slf4j
3333
public class DemoThread implements Runnable {
3434

35-
private static int[] executionOrder;
3635
private static int[] actualExecutionOrder;
3736
private static int index = 0;
3837
private static JoinPattern pattern;
39-
private int id;
40-
private Thread previous;
38+
private final int id;
39+
private final Thread previous;
4140

4241
/**
43-
* Initalise a demo thread object with id and previous thread .
42+
* Initialise a demo thread object with id and previous thread .
4443
*/
4544
public DemoThread(int id, Thread previous) {
4645
this.id = id;
@@ -55,26 +54,31 @@ public static int[] getActualExecutionOrder() {
5554
* set custom execution order of threads .
5655
*/
5756
public static void setExecutionOrder(int[] executionOrder, JoinPattern pattern) {
58-
DemoThread.executionOrder = executionOrder;
5957
DemoThread.pattern = pattern;
6058
actualExecutionOrder = new int[executionOrder.length];
6159
}
6260
/**
6361
* use to run demo thread.
62+
* every newly created thread waits for
63+
* the completion of previous thread
64+
* by previous.join() .
6465
*/
66+
@Override
6567
public void run() {
6668
if (previous != null) {
6769
try {
6870
previous.join();
6971
} catch (InterruptedException e) {
70-
e.printStackTrace();
72+
Thread.currentThread().interrupt();
73+
LOGGER.error("Interrupted exception : ", e);
7174
}
7275
}
7376
LOGGER.info("Thread " + id + " starts");
7477
try {
75-
Thread.sleep(id * 250);
78+
Thread.sleep(id * (long) 250);
7679
} catch (InterruptedException e) {
77-
e.printStackTrace();
80+
Thread.currentThread().interrupt();
81+
LOGGER.error("Interrupted exception : ", e);
7882
} finally {
7983
LOGGER.info("Thread " + id + " ends");
8084
actualExecutionOrder[index++] = id;

join/src/main/java/com/iluwatar/join/DependentThread.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
*/
2525
package com.iluwatar.join;
2626

27-
import lombok.Getter;
28-
import lombok.RequiredArgsConstructor;
2927
import lombok.extern.slf4j.Slf4j;
3028

3129
/**
@@ -34,21 +32,22 @@
3432
@Slf4j
3533
public class DependentThread implements Runnable {
3634

37-
38-
private int id;
35+
private final int id;
3936
DependentThread(int id) {
4037
this.id = id;
4138
}
4239
/**
4340
* dependent threads run .
4441
*/
42+
@Override
4543
public void run() {
4644

4745
LOGGER.info(" Dependent Thread " + id + " starts ");
4846
try {
49-
Thread.sleep(id * 200);
47+
Thread.sleep(id * (long) 200);
5048
} catch (InterruptedException e) {
51-
e.printStackTrace();
49+
Thread.currentThread().interrupt();
50+
LOGGER.error("Interrupted exception : ", e);
5251
} finally {
5352
LOGGER.info("Dependent Thread " + id + " completed ");
5453
}

join/src/main/java/com/iluwatar/join/JoinPatternDemo.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@
3030
* main thread will continue when CountDownLatch count becomes 0
3131
* CountDownLatch will start with count 4 and 4 demo threads will decrease it by 1
3232
* everytime when they will finish .
33+
* DemoThreads are implemented in join pattern such that every newly created thread
34+
* waits for the completion of previous thread by previous.join() . Hence maintaining
35+
* execution order of demo threads .
36+
* JoinPattern object ensures that dependent threads execute only after completion
37+
* demo threads by pattern.await() . This method keep the main thread in waiting state
38+
* until countdown latch becomes 0 . CountdownLatch will become 0 as all demo threads
39+
* will be completed as each of them have decreased it by 1 and its initial count was set to noOfDemoThreads.
40+
* Hence this pattern ensures dependent threads will start only after completion of demo threads .
3341
*/
3442

3543
@Slf4j

0 commit comments

Comments
 (0)