-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThread12.java
More file actions
42 lines (33 loc) · 1.31 KB
/
Thread12.java
File metadata and controls
42 lines (33 loc) · 1.31 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
class Child implements Runnable{
public void run(){
for(int i=0; i<50; i++){
System.out.println("child: "+ Thread.currentThread().getName() +" "+i);
try{
Thread.sleep(250);
} catch(InterruptedException e){
System.out.println("Interrupted Exception"+e);
}
}
}
}
class Thread12 {
public static void main(String[] args) throws InterruptedException {
Child thread2 = new Child();
Thread thread = new Thread(thread2);
thread.start();
try{
// main thread decides to stay untill child thread completes
//thread.join();
// main thread stays only 5 secs
thread.join(5000);
}
/* join() will put the current thread on wait until the thread on which it is called is dead. If thread is interrupted then it will throw InterruptedException. */
catch(InterruptedException e){
System.out.println("Interrupted Exception"+e);
}
for(int i=0; i<50; i++){
System.out.println("main: "+ Thread.currentThread().getName() +" "+i);
}
System.out.println("====================================================");
}
}