Skip to content

Commit ede471b

Browse files
committed
Added Join Design Pattern Issue iluwatar#70
1 parent adbddcb commit ede471b

File tree

10 files changed

+495
-0
lines changed

10 files changed

+495
-0
lines changed

Diff for: join/README.md

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
---
2+
title: "Join Pattern in Java: Synchronizing Concurrent Tasks"
3+
shortTitle: Join
4+
description: "Learn the Join Design Pattern in Java with detailed examples and explanations. Understand how to synchronize concurrent tasks and manage execution flow using the Join Pattern. Ideal for developers looking to improve their multithreading and synchronization skills."
5+
category: Behavioral
6+
language: en
7+
issue: #70
8+
tag:
9+
- Concurrency
10+
- Synchronization
11+
- Threads
12+
- Multithreading
13+
- Parallel Execution
14+
---
15+
16+
## Intent of Join Design Pattern
17+
18+
The **Join Design Pattern** in Java is used to synchronize multiple concurrent processes or threads so that they must all complete before any subsequent tasks can proceed. This pattern is essential when tasks are executed in parallel, but the subsequent tasks need to wait until all parallel tasks are finished. It allows threads to "join" at a synchronization point and ensures correct execution order and timing.
19+
20+
## Detailed Explanation of Join Pattern with Real-World Examples
21+
22+
#### Real-World Example
23+
24+
Imagine a **construction project** where multiple contractors are working on different aspects of the building simultaneously. The project manager doesn't want to proceed with the final inspection of the building until all the contractors have finished their respective tasks. Using the **Join Design Pattern**, the manager waits for all contractors (threads) to complete their work before proceeding with the inspection (subsequent task).
25+
26+
This pattern allows the project manager to synchronize all contractors' tasks to ensure that the inspection is only performed once all work is completed.
27+
28+
#### Wikipedia Definition:
29+
30+
> "Join is a synchronization technique that allows multiple concurrent threads or processes to synchronize and wait for the completion of other threads before proceeding to subsequent tasks."
31+
32+
## Programmatic Example of Join Pattern in Java
33+
34+
In this example, we simulate a scenario where four demo tasks run concurrently, and the main thread waits for their completion before proceeding. This is achieved using the **Thread#join()** method, which ensures that the main thread waits for all demo tasks to finish before continuing.
35+
36+
### DemoThreadClass
37+
38+
```java
39+
/*
40+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
41+
*
42+
* The MIT License
43+
* Copyright © 2014-2022 Ilkka Seppälä
44+
*
45+
* Permission is hereby granted, free of charge, to any person obtaining a copy
46+
* of this software and associated documentation files (the "Software"), to deal
47+
* in the Software without restriction, including without limitation the rights
48+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
49+
* copies of the Software, and to permit persons to whom the Software is
50+
* furnished to do so, subject to the following conditions:
51+
*
52+
* The above copyright notice and this permission notice shall be included in
53+
* all copies or substantial portions of the Software.
54+
*
55+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
56+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
57+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
58+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
59+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
60+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
61+
* THE SOFTWARE.
62+
*/
63+
package com.iluwatar.join;
64+
65+
import lombok.extern.slf4j.Slf4j;
66+
67+
/*
68+
* DemoThreads implementing Runnable
69+
*/
70+
@Slf4j
71+
public class DemoThread implements Runnable {
72+
73+
private static int[] executionOrder;
74+
private static int[] actualExecutionOrder;
75+
private static int index = 0;
76+
private static JoinPattern pattern;
77+
private int id;
78+
private Thread previous;
79+
80+
public DemoThread(int id, Thread previous) {
81+
this.id = id;
82+
this.previous = previous;
83+
84+
}
85+
86+
public static int[] getActualExecutionOrder() {
87+
return actualExecutionOrder;
88+
}
89+
90+
public static void setExecutionOrder(int[] executionOrder, JoinPattern pattern) {
91+
DemoThread.executionOrder = executionOrder;
92+
DemoThread.pattern = pattern;
93+
actualExecutionOrder = new int[executionOrder.length];
94+
}
95+
96+
public void run() {
97+
if (previous != null) {
98+
try {
99+
previous.join();
100+
} catch (InterruptedException e) {
101+
e.printStackTrace();
102+
}
103+
}
104+
Logger.info("Thread " + id + " starts");
105+
try {
106+
Thread.sleep(id * 250);
107+
108+
} catch (InterruptedException e) {
109+
e.printStackTrace();
110+
} finally {
111+
Logger.info("Thread " + id + " ends");
112+
actualExecutionOrder[index++] = id;
113+
pattern.countdown();
114+
115+
}
116+
}
117+
118+
}
119+

Diff for: join/etc/JoinPatternFlowDiagram.png

37.3 KB
Loading

Diff for: join/etc/java-join-method.png

11.5 KB
Loading

Diff for: join/pom.xml

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
5+
6+
The MIT License
7+
Copyright © 2014-2022 Ilkka Seppälä
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in
17+
all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
27+
-->
28+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
29+
<modelVersion>4.0.0</modelVersion>
30+
<parent>
31+
<groupId>com.iluwatar</groupId>
32+
<artifactId>java-design-patterns</artifactId>
33+
<version>1.26.0-SNAPSHOT</version>
34+
</parent>
35+
<artifactId>join</artifactId>
36+
<dependencies>
37+
<dependency>
38+
<groupId>org.junit.jupiter</groupId>
39+
<artifactId>junit-jupiter-engine</artifactId>
40+
<scope>test</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.junit.jupiter</groupId>
44+
<artifactId>junit-jupiter-params</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.mockito</groupId>
49+
<artifactId>mockito-core</artifactId>
50+
<scope>test</scope>
51+
</dependency>
52+
</dependencies>
53+
<build>
54+
<plugins>
55+
<plugin>
56+
<groupId>org.apache.maven.plugins</groupId>
57+
<artifactId>maven-assembly-plugin</artifactId>
58+
<executions>
59+
<execution>
60+
<configuration>
61+
<archive>
62+
<manifest>
63+
<mainClass>com.iluwatar.join.JoinPatternDemo</mainClass>
64+
</manifest>
65+
</archive>
66+
</configuration>
67+
</execution>
68+
</executions>
69+
</plugin>
70+
</plugins>
71+
</build>
72+
</project>

Diff for: join/src/main/java/com/iluwatar/join/DemoThread.java

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package com.iluwatar.join;
26+
27+
import lombok.extern.slf4j.Slf4j;
28+
29+
/*
30+
* DemoThreads implementing Runnable
31+
*/
32+
@Slf4j
33+
public class DemoThread implements Runnable {
34+
35+
private static int[] executionOrder;
36+
private static int[] actualExecutionOrder;
37+
private static int index = 0;
38+
private static JoinPattern pattern;
39+
private int id;
40+
private Thread previous;
41+
42+
public DemoThread(int id, Thread previous) {
43+
this.id = id;
44+
this.previous = previous;
45+
46+
}
47+
48+
public static int[] getActualExecutionOrder() {
49+
return actualExecutionOrder;
50+
}
51+
52+
public static void setExecutionOrder(int[] executionOrder, JoinPattern pattern) {
53+
DemoThread.executionOrder = executionOrder;
54+
DemoThread.pattern = pattern;
55+
actualExecutionOrder = new int[executionOrder.length];
56+
}
57+
58+
public void run() {
59+
if (previous != null) {
60+
try {
61+
previous.join();
62+
} catch (InterruptedException e) {
63+
e.printStackTrace();
64+
}
65+
}
66+
Logger.info("Thread " + id + " starts");
67+
try {
68+
Thread.sleep(id * 250);
69+
70+
} catch (InterruptedException e) {
71+
e.printStackTrace();
72+
} finally {
73+
Logger.info("Thread " + id + " ends");
74+
actualExecutionOrder[index++] = id;
75+
pattern.countdown();
76+
77+
}
78+
}
79+
80+
}
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package com.iluwatar.join;
26+
27+
import lombok.Getter;
28+
import lombok.RequiredArgsConstructor;
29+
import lombok.extern.slf4j.Slf4j;
30+
31+
/*
32+
* Dependent threads will execute only after completion of all demothreads
33+
*/
34+
@Slf4j
35+
public class DependentThread {
36+
37+
private int id;
38+
39+
DependentThread(int id) {
40+
this.id = id;
41+
}
42+
43+
public void run() {
44+
45+
Logger.info(" Dependent Thread " + id + " starts ");
46+
try {
47+
Thread.sleep(id * 200);
48+
} catch (InterruptedException e) {
49+
e.printStackTrace();
50+
} finally {
51+
Logger.info("Dependent Thread " + id + " completed ");
52+
}
53+
54+
}
55+
}
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package com.iluwatar.join;
26+
27+
import java.util.concurrent.CountDownLatch;
28+
29+
/**
30+
* The Join design pattern allows multiple concurrent processes or threads to be
31+
* synchronized such that they all must complete before any subsequent tasks can
32+
* proceed. This pattern is particularly useful in scenarios where tasks can be
33+
* executed in parallel but the subsequent tasks must wait for the completion of
34+
* these parallel tasks.
35+
*/
36+
public class JoinPattern {
37+
38+
int noOfDemoThreads;
39+
private CountDownLatch latch;
40+
int[] executionOrder;
41+
42+
public JoinPattern(int noOfDemoThreads, int[] executionOrder) {
43+
latch = new CountDownLatch(noOfDemoThreads);
44+
this.executionOrder = executionOrder;
45+
}
46+
47+
public void countdown() {
48+
latch.countDown();
49+
}
50+
51+
public void await() throws InterruptedException {
52+
latch.await();
53+
}
54+
55+
}

0 commit comments

Comments
 (0)