Skip to content
Prev Previous commit
Next Next commit
updated README.MD
  • Loading branch information
keshavMM004 committed Jan 16, 2025
commit 3b4bc80a4ef778ff6676b86e70ee5af073662bef
71 changes: 39 additions & 32 deletions join/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,15 @@ import lombok.extern.slf4j.Slf4j;
* main thread will continue when CountDownLatch count becomes 0
* CountDownLatch will start with count 4 and 4 demo threads will decrease it by 1
* everytime when they will finish .
* DemoThreads are implemented in join pattern such that every newly created thread
* waits for the completion of previous thread by previous.join() . Hence maintaining
* execution order of demo threads .
* JoinPattern object ensures that dependent threads execute only after completion of
* demo threads by pattern.await() . This method keep the main thread in waiting state
* until countdown latch becomes 0 . CountdownLatch will become 0 as all demo threads
* will be completed as each of them have decreased it by 1 and its initial count was set to noOfDemoThreads.
* Hence this pattern ensures dependent threads will start only after completion of demo threads .
*/
Comment on lines +42 to +58
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep the code example minimal. Leave out headers and imports and such. It doesn't have to compile, but the reader has to understand.


@Slf4j
public class JoinPatternDemo {

/**
* execution of demo and dependent threads.
*/
public static void main(String[] args) {

int[] executionOrder = {4, 2, 1, 3};
int noOfDemoThreads = 4;
int noOfDependentThreads = 2;
JoinPattern pattern = new JoinPattern(noOfDemoThreads, executionOrder);
Thread previous = null;

for (int i = 0; i < noOfDemoThreads; i++) {
previous = new Thread(new DemoThread(executionOrder[i], previous));
previous.start();
}
pattern.await();

//Dependent threads after execution of DemoThreads
Expand All @@ -75,25 +64,43 @@ public class JoinPatternDemo {
}
LOGGER.info("end of program ");

}
/**
* use to run demo thread.
* every newly created thread waits for
* the completion of previous thread
* by previous.join() .
*/
@Override
public void run() {
if (previous != null) {
try {
previous.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
LOGGER.error("Interrupted exception : ", e);
}
}

}

```

### Program Output:

```
Running com.iluwatar.join.JoinPatternTest
01:13:17.890 [Thread-2] INFO com.iluwatar.join.DemoThread -- Thread 1 starts
01:13:18.167 [Thread-2] INFO com.iluwatar.join.DemoThread -- Thread 1 ends
01:13:18.168 [Thread-3] INFO com.iluwatar.join.DemoThread -- Thread 4 starts
01:13:19.176 [Thread-3] INFO com.iluwatar.join.DemoThread -- Thread 4 ends
01:13:19.176 [Thread-4] INFO com.iluwatar.join.DemoThread -- Thread 3 starts
01:13:19.935 [Thread-4] INFO com.iluwatar.join.DemoThread -- Thread 3 ends
01:13:19.935 [Thread-5] INFO com.iluwatar.join.DemoThread -- Thread 2 starts
01:13:20.437 [Thread-5] INFO com.iluwatar.join.DemoThread -- Thread 2 ends
```
[INFO] Running com.iluwatar.join.JoinPatternTest
16:12:01.815 [Thread-2] INFO com.iluwatar.join.DemoThread -- Thread 1 starts
16:12:02.086 [Thread-2] INFO com.iluwatar.join.DemoThread -- Thread 1 ends
16:12:02.087 [Thread-3] INFO com.iluwatar.join.DemoThread -- Thread 4 starts
16:12:03.090 [Thread-3] INFO com.iluwatar.join.DemoThread -- Thread 4 ends
16:12:03.091 [Thread-4] INFO com.iluwatar.join.DemoThread -- Thread 3 starts
16:12:03.851 [Thread-4] INFO com.iluwatar.join.DemoThread -- Thread 3 ends
16:12:03.851 [Thread-5] INFO com.iluwatar.join.DemoThread -- Thread 2 starts
16:12:04.352 [Thread-5] INFO com.iluwatar.join.DemoThread -- Thread 2 ends
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.904 s -- in com.iluwatar.join.JoinPatternTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]

## When to Use the Join Pattern in Java

Expand Down
20 changes: 12 additions & 8 deletions join/src/main/java/com/iluwatar/join/DemoThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,14 @@
@Slf4j
public class DemoThread implements Runnable {

private static int[] executionOrder;
private static int[] actualExecutionOrder;
private static int index = 0;
private static JoinPattern pattern;
private int id;
private Thread previous;
private final int id;
private final Thread previous;

/**
* Initalise a demo thread object with id and previous thread .
* Initialise a demo thread object with id and previous thread .
*/
public DemoThread(int id, Thread previous) {
this.id = id;
Expand All @@ -55,26 +54,31 @@ public static int[] getActualExecutionOrder() {
* set custom execution order of threads .
*/
public static void setExecutionOrder(int[] executionOrder, JoinPattern pattern) {
DemoThread.executionOrder = executionOrder;
DemoThread.pattern = pattern;
actualExecutionOrder = new int[executionOrder.length];
}
/**
* use to run demo thread.
* every newly created thread waits for
* the completion of previous thread
* by previous.join() .
*/
@Override
public void run() {
if (previous != null) {
try {
previous.join();
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
LOGGER.error("Interrupted exception : ", e);
}
}
LOGGER.info("Thread " + id + " starts");
try {
Thread.sleep(id * 250);
Thread.sleep(id * (long) 250);
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
LOGGER.error("Interrupted exception : ", e);
} finally {
LOGGER.info("Thread " + id + " ends");
actualExecutionOrder[index++] = id;
Expand Down
11 changes: 5 additions & 6 deletions join/src/main/java/com/iluwatar/join/DependentThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
*/
package com.iluwatar.join;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
Expand All @@ -34,21 +32,22 @@
@Slf4j
public class DependentThread implements Runnable {


private int id;
private final int id;
DependentThread(int id) {
this.id = id;
}
/**
* dependent threads run .
*/
@Override
public void run() {

LOGGER.info(" Dependent Thread " + id + " starts ");
try {
Thread.sleep(id * 200);
Thread.sleep(id * (long) 200);
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
LOGGER.error("Interrupted exception : ", e);
} finally {
LOGGER.info("Dependent Thread " + id + " completed ");
}
Expand Down
8 changes: 8 additions & 0 deletions join/src/main/java/com/iluwatar/join/JoinPatternDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@
* main thread will continue when CountDownLatch count becomes 0
* CountDownLatch will start with count 4 and 4 demo threads will decrease it by 1
* everytime when they will finish .
* DemoThreads are implemented in join pattern such that every newly created thread
* waits for the completion of previous thread by previous.join() . Hence maintaining
* execution order of demo threads .
* JoinPattern object ensures that dependent threads execute only after completion
* demo threads by pattern.await() . This method keep the main thread in waiting state
* until countdown latch becomes 0 . CountdownLatch will become 0 as all demo threads
* will be completed as each of them have decreased it by 1 and its initial count was set to noOfDemoThreads.
* Hence this pattern ensures dependent threads will start only after completion of demo threads .
*/
Comment on lines 29 to 41
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explain the pattern briefly. After that explain how the code example implements it. Add comments to the code where necessary. Remember, this is material for studying design patterns.


@Slf4j
Expand Down