-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJoinDemo.java
More file actions
39 lines (32 loc) · 924 Bytes
/
Copy pathJoinDemo.java
File metadata and controls
39 lines (32 loc) · 924 Bytes
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
public class JoinDemo{
public static void main(String[] args){
Runnable r = new Runnable(){
@Override
public void run(){
System.out.println("Worker thread is simulating work by sleeping for 5 seconds.");
try{
Thread.sleep(5000);
}catch(InterruptedException ie){
ie.printStackTrace();
}
System.out.println("Worker thread is dying.");
}
};
Thread thd = new Thread(r);
thd.start();
System.out.println("Default main thread is doing work.");
try{
Thread.sleep(2000);
}catch(InterruptedException ie){
ie.printStackTrace();
}
System.out.println("Default main thread has finished its work.");
System.out.println("Default main thread is waiting for worker thread to die.");
try{
thd.join();
}catch(InterruptedException ie){
ie.printStackTrace();
}
System.out.println("Last statement in main method. Default main thread is dying.");
}
}