forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJoinEx.java
More file actions
56 lines (40 loc) · 1.16 KB
/
JoinEx.java
File metadata and controls
56 lines (40 loc) · 1.16 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
49
50
51
52
53
54
55
56
package com.zetcode;
class Worker extends Thread {
private int delay;
private String msg;
public Worker(int delay, String msg) {
this.delay = delay;
this.msg = msg;
}
public void run() {
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(msg);
}
}
public class JoinEx {
public static void main(String[] args) {
var w1 = new Worker(2000, "Hello there");
var w2 = new Worker(1000, "New mail received");
var w3 = new Worker(500, "Notes taken");
// start three threads
w1.start();
w2.start();
w3.start();
// wait for threads to end
try {
// join is a blocker method which waits for a thread to complete.
// the w1.join() causes the current (main) thread to pause execution
// until w1's thread terminates.
w1.join();
w2.join();
w3.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("finished tasks");
}
}