Skip to content

Commit

Permalink
resolved checkstyle violations
Browse files Browse the repository at this point in the history
  • Loading branch information
keshavMM004 committed Jan 7, 2025
1 parent cf93fc8 commit 3e23c5e
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 91 deletions.
89 changes: 47 additions & 42 deletions join/src/main/java/com/iluwatar/join/DemoThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,55 +26,60 @@

import lombok.extern.slf4j.Slf4j;

/*
* DemoThreads implementing Runnable
/**
* demo threads implementing Runnable .
*/
@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;

public DemoThread(int id, Thread previous) {
this.id = id;
this.previous = previous;

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

/**
* Initalise a demo thread object with id and previous thread .
*/
public DemoThread(int id, Thread previous) {
this.id = id;
this.previous = previous;

public static int[] getActualExecutionOrder() {
return actualExecutionOrder;
}
}

public static void setExecutionOrder(int[] executionOrder, JoinPattern pattern) {
DemoThread.executionOrder = executionOrder;
DemoThread.pattern = pattern;
actualExecutionOrder = new int[executionOrder.length];
public static int[] getActualExecutionOrder() {
return actualExecutionOrder;
}
/**
* 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.
*/
public void run() {
if (previous != null) {
try {
previous.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

public void run() {
if (previous != null) {
try {
previous.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Logger.info("Thread " + id + " starts");
try {
Thread.sleep(id * 250);

} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Logger.info("Thread " + id + " ends");
actualExecutionOrder[index++] = id;
pattern.countdown();

}
Logger.info("Thread " + id + " starts");
try {
Thread.sleep(id * 250);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Logger.info("Thread " + id + " ends");
actualExecutionOrder[index++] = id;
pattern.countdown();
}
}

}
36 changes: 19 additions & 17 deletions join/src/main/java/com/iluwatar/join/DependentThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,30 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/*
* Dependent threads will execute only after completion of all demothreads
/**
* Dependent threads will execute only after completion of all demothreads.
*/
@Slf4j
public class DependentThread {

private int id;

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

DependentThread(int id) {
this.id = id;
Logger.info(" Dependent Thread " + id + " starts ");
try {
Thread.sleep(id * 200);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Logger.info("Dependent Thread " + id + " completed ");
}

public void run() {

Logger.info(" Dependent Thread " + id + " starts ");
try {
Thread.sleep(id * 200);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Logger.info("Dependent Thread " + id + " completed ");
}

}
}
}
26 changes: 13 additions & 13 deletions join/src/main/java/com/iluwatar/join/JoinPattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@
*/
public class JoinPattern {

int noOfDemoThreads;
private CountDownLatch latch;
int[] executionOrder;
int noOfDemoThreads;
private CountDownLatch latch;
int[] executionOrder;

public JoinPattern(int noOfDemoThreads, int[] executionOrder) {
latch = new CountDownLatch(noOfDemoThreads);
this.executionOrder = executionOrder;
}
public JoinPattern(int noOfDemoThreads, int[] executionOrder) {
latch = new CountDownLatch(noOfDemoThreads);
this.executionOrder = executionOrder;
}

public void countdown() {
latch.countDown();
}
public void countdown() {
latch.countDown();
}

public void await() throws InterruptedException {
latch.await();
}
public void await() throws InterruptedException {
latch.await();
}

}
41 changes: 22 additions & 19 deletions join/src/main/java/com/iluwatar/join/JoinPatternDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,38 @@

import lombok.extern.slf4j.Slf4j;

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

@Slf4j
public class JoinPatternDemo {

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();
/**
* execution of demo and dependent threads.
*/
public static void main(String[] args) {

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

//Dependent threads after execution of DemoThreads
for (int i = 0; i < noOfDependentThreads; i++) {
new DependentThread(i + 1).start();
}
Logger.info("end of program ");
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 DependentThread(i + 1).start();
}
Logger.info("end of program ");

}

}

0 comments on commit 3e23c5e

Please sign in to comment.