Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Join Pattern #3172

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open

Added Join Pattern #3172

wants to merge 10 commits into from

Conversation

keshavMM004
Copy link

Implemented the join design pattern that allows multiple threads to be synchronized such that all DemoThreads must complete before any DependentThreads can proceed .

Close #70

Copy link

github-actions bot commented Jan 12, 2025

PR Summary

This PR implements the Join design pattern, enabling synchronization of multiple threads. All DemoThreads must complete before DependentThreads can start. It includes a README, diagrams, and unit tests.

Changes

File Summary
join/README.md This file provides a comprehensive guide to the Java Join Pattern, including its intent, real-world examples, programmatic implementation, usage scenarios, benefits, trade-offs, and related patterns. It also includes a detailed explanation with code examples and program output.
join/etc/JoinPatternFlowDiagram.png New file: Flow diagram illustrating the Join Pattern.
join/etc/java-join-method.png New file: Image illustrating the Java join method.
join/etc/join.urm.puml New file: UML diagram depicting the classes and relationships in the Join Pattern implementation.
join/pom.xml This file configures the Maven project, including dependencies for testing and the main class for execution.
join/src/main/java/com/iluwatar/join/DemoThread.java Implements DemoThread class which runs and waits for the previous thread to complete before starting. It logs thread start and end times and updates the execution order.
join/src/main/java/com/iluwatar/join/DependentThread.java Implements DependentThread, which starts after all DemoThreads finish. It logs thread start and end times.
join/src/main/java/com/iluwatar/join/JoinPattern.java Implements the JoinPattern class using a CountDownLatch to synchronize DemoThreads and DependentThreads. It manages the execution order and ensures that dependent threads only start after all demo threads have completed.
join/src/main/java/com/iluwatar/join/JoinPatternDemo.java This is the main class that demonstrates the Join Pattern. It creates and starts DemoThreads and DependentThreads, ensuring proper synchronization using JoinPattern.
join/src/test/java/com/iluwatar/join/JoinPatternTest.java This file contains a JUnit test case to verify that the demo threads execute in the specified order using assertArrayEquals.
pom.xml The join module was added to the project's module list.

autogenerated by presubmit.ai

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

LGTM!

Review Summary

Commits Considered (7)
Files Processed (11)
  • join/README.md (1 hunk)
  • join/etc/JoinPatternFlowDiagram.png (0 hunks)
  • join/etc/java-join-method.png (0 hunks)
  • join/etc/join.urm.puml (1 hunk)
  • join/pom.xml (1 hunk)
  • join/src/main/java/com/iluwatar/join/DemoThread.java (1 hunk)
  • join/src/main/java/com/iluwatar/join/DependentThread.java (1 hunk)
  • join/src/main/java/com/iluwatar/join/JoinPattern.java (1 hunk)
  • join/src/main/java/com/iluwatar/join/JoinPatternDemo.java (1 hunk)
  • join/src/test/java/com/iluwatar/join/JoinPatternTest.java (1 hunk)
  • pom.xml (1 hunk)
Actionable Comments (0)
Skipped Comments (0)

join/README.md Outdated Show resolved Hide resolved
try {
previous.join();
} catch (InterruptedException e) {
e.printStackTrace();
Copy link
Owner

Choose a reason for hiding this comment

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

Log to Slf4j log, not to console, e.g.

log.error("An error occurred: {}", e.getMessage(), e);

Comment on lines 29 to 33
/** Here main thread will execute after completion of 4 demo threads
* 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 .
*/
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.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

LGTM!

Review Summary

Commits Considered (3)
Files Processed (7)
  • join/README.md (1 hunk)
  • join/etc/join.urm.puml (1 hunk)
  • join/src/main/java/com/iluwatar/join/DemoThread.java (1 hunk)
  • join/src/main/java/com/iluwatar/join/DependentThread.java (1 hunk)
  • join/src/main/java/com/iluwatar/join/JoinPattern.java (1 hunk)
  • join/src/main/java/com/iluwatar/join/JoinPatternDemo.java (1 hunk)
  • pom.xml (1 hunk)
Actionable Comments (0)
Skipped Comments (0)

Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
45.5% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@keshavMM004 keshavMM004 requested a review from iluwatar January 16, 2025 14:37
Comment on lines +42 to +58
package com.iluwatar.join;

import lombok.extern.slf4j.Slf4j;

/** Here main thread will execute after completion of 4 demo threads
* 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 .
*/
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.

Comment on lines +32 to +52
@Test
public void test() throws InterruptedException {

int noOfDemoThreads = 4;
int[] executionOrder = {1, 4, 3, 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();

int[] actualExecutionOrder = DemoThread.getActualExecutionOrder();

assertArrayEquals(executionOrder, actualExecutionOrder, "Demo threads did not execute in given order ");

}
Copy link
Owner

Choose a reason for hiding this comment

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

We need more test coverage. Use Sonar analysis to identify untested code.

Comment on lines +51 to +67
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
for (int i = 0; i < noOfDependentThreads; i++) {
new Thread(new DependentThread(i + 1)).start();
}
LOGGER.info("end of program ");
Copy link
Owner

Choose a reason for hiding this comment

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

Add clear comments explaining what is happening in each step

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Join pattern
2 participants