Skip to content

Commit 3e23c5e

Browse files
committed
resolved checkstyle violations
1 parent cf93fc8 commit 3e23c5e

File tree

4 files changed

+101
-91
lines changed

4 files changed

+101
-91
lines changed

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

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,55 +26,60 @@
2626

2727
import lombok.extern.slf4j.Slf4j;
2828

29-
/*
30-
* DemoThreads implementing Runnable
29+
/**
30+
* demo threads implementing Runnable .
3131
*/
3232
@Slf4j
3333
public class DemoThread implements Runnable {
3434

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-
}
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+
/**
43+
* Initalise a demo thread object with id and previous thread .
44+
*/
45+
public DemoThread(int id, Thread previous) {
46+
this.id = id;
47+
this.previous = previous;
4748

48-
public static int[] getActualExecutionOrder() {
49-
return actualExecutionOrder;
50-
}
49+
}
5150

52-
public static void setExecutionOrder(int[] executionOrder, JoinPattern pattern) {
53-
DemoThread.executionOrder = executionOrder;
54-
DemoThread.pattern = pattern;
55-
actualExecutionOrder = new int[executionOrder.length];
51+
public static int[] getActualExecutionOrder() {
52+
return actualExecutionOrder;
53+
}
54+
/**
55+
* set custom execution order of threads .
56+
*/
57+
public static void setExecutionOrder(int[] executionOrder, JoinPattern pattern) {
58+
DemoThread.executionOrder = executionOrder;
59+
DemoThread.pattern = pattern;
60+
actualExecutionOrder = new int[executionOrder.length];
61+
}
62+
/**
63+
* use to run demo thread.
64+
*/
65+
public void run() {
66+
if (previous != null) {
67+
try {
68+
previous.join();
69+
} catch (InterruptedException e) {
70+
e.printStackTrace();
71+
}
5672
}
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-
}
73+
Logger.info("Thread " + id + " starts");
74+
try {
75+
Thread.sleep(id * 250);
76+
} catch (InterruptedException e) {
77+
e.printStackTrace();
78+
} finally {
79+
Logger.info("Thread " + id + " ends");
80+
actualExecutionOrder[index++] = id;
81+
pattern.countdown();
7882
}
83+
}
7984

8085
}

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

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,30 @@
2828
import lombok.RequiredArgsConstructor;
2929
import lombok.extern.slf4j.Slf4j;
3030

31-
/*
32-
* Dependent threads will execute only after completion of all demothreads
31+
/**
32+
* Dependent threads will execute only after completion of all demothreads.
3333
*/
3434
@Slf4j
3535
public class DependentThread {
3636

37-
private int id;
37+
38+
private int id;
39+
DependentThread(int id) {
40+
this.id = id;
41+
}
42+
/**
43+
* dependent threads run .
44+
*/
45+
public void run() {
3846

39-
DependentThread(int id) {
40-
this.id = id;
47+
Logger.info(" Dependent Thread " + id + " starts ");
48+
try {
49+
Thread.sleep(id * 200);
50+
} catch (InterruptedException e) {
51+
e.printStackTrace();
52+
} finally {
53+
Logger.info("Dependent Thread " + id + " completed ");
4154
}
4255

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-
}
56+
}
5557
}

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,21 @@
3535
*/
3636
public class JoinPattern {
3737

38-
int noOfDemoThreads;
39-
private CountDownLatch latch;
40-
int[] executionOrder;
38+
int noOfDemoThreads;
39+
private CountDownLatch latch;
40+
int[] executionOrder;
4141

42-
public JoinPattern(int noOfDemoThreads, int[] executionOrder) {
43-
latch = new CountDownLatch(noOfDemoThreads);
44-
this.executionOrder = executionOrder;
45-
}
42+
public JoinPattern(int noOfDemoThreads, int[] executionOrder) {
43+
latch = new CountDownLatch(noOfDemoThreads);
44+
this.executionOrder = executionOrder;
45+
}
4646

47-
public void countdown() {
48-
latch.countDown();
49-
}
47+
public void countdown() {
48+
latch.countDown();
49+
}
5050

51-
public void await() throws InterruptedException {
52-
latch.await();
53-
}
51+
public void await() throws InterruptedException {
52+
latch.await();
53+
}
5454

5555
}

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

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,38 @@
2626

2727
import lombok.extern.slf4j.Slf4j;
2828

29-
/* Here main thread will execute after completion of 4 demo threads
29+
/** Here main thread will execute after completion of 4 demo threads
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 .
3333
*/
34+
3435
@Slf4j
3536
public class JoinPatternDemo {
3637

37-
public static void main(String[] args) {
38-
39-
int[] executionOrder = {4, 2, 1, 3};
40-
int noOfDemoThreads = 4;
41-
int noOfDependentThreads = 2;
42-
JoinPattern pattern = new JoinPattern(noOfDemoThreads, executionOrder);
43-
Thread previous = null;
44-
45-
for (int i = 0; i < noOfDemoThreads; i++) {
46-
previous = new Thread(new DemoThread(executionOrder[i], previous));
47-
previous.start();
38+
/**
39+
* execution of demo and dependent threads.
40+
*/
41+
public static void main(String[] args) {
4842

49-
}
50-
pattern.await();
43+
int[] executionOrder = {4, 2, 1, 3};
44+
int noOfDemoThreads = 4;
45+
int noOfDependentThreads = 2;
46+
JoinPattern pattern = new JoinPattern(noOfDemoThreads, executionOrder);
47+
Thread previous = null;
5148

52-
//Dependent threads after execution of DemoThreads
53-
for (int i = 0; i < noOfDependentThreads; i++) {
54-
new DependentThread(i + 1).start();
55-
}
56-
Logger.info("end of program ");
49+
for (int i = 0; i < noOfDemoThreads; i++) {
50+
previous = new Thread(new DemoThread(executionOrder[i], previous));
51+
previous.start();
52+
}
53+
pattern.await();
5754

55+
//Dependent threads after execution of DemoThreads
56+
for (int i = 0; i < noOfDependentThreads; i++) {
57+
new DependentThread(i + 1).start();
5858
}
59+
Logger.info("end of program ");
60+
61+
}
5962

6063
}

0 commit comments

Comments
 (0)