-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJoinDemo.java
More file actions
48 lines (42 loc) · 1.34 KB
/
Copy pathJoinDemo.java
File metadata and controls
48 lines (42 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package inverview.threadjoin;
public class JoinDemo {
private static class ParallelTask implements Runnable {
private Thread predecessor;
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " Started");
if (predecessor != null) {
try {
predecessor.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + " Finished");
}
public void setPredecessor(Thread t) {
this.predecessor = t;
}
}
public static void main(String[] args) {
// we have three threads and we need to run in the
// order T1, T2 and T3 i.e. T1 should start first
// and T3 should start last.
// You can enforce this ordering using join() method
// but join method must be called from run() method
// because the thread which will execute run() method
// will wait for thread on which join is called.
ParallelTask task1 = new ParallelTask();
ParallelTask task2 = new ParallelTask();
ParallelTask task3 = new ParallelTask();
final Thread t1 = new Thread(task1, "Thread - 1");
final Thread t2 = new Thread(task2, "Thread - 2");
final Thread t3 = new Thread(task3, "Thread - 3");
task2.setPredecessor(t1);
task3.setPredecessor(t2);
// now, let's start all three threads
t3.start();
t2.start();
t1.start();
}
}