-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadjoin.java
More file actions
59 lines (58 loc) · 988 Bytes
/
Threadjoin.java
File metadata and controls
59 lines (58 loc) · 988 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class MyRunnable implements Runnable
{
public void run()
{
System.out.println("Thread started" + Thread.currentThread().getName());
try
{
Thread.sleep(4000);
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("Thread ended" + Thread.currentThread().getName());
}
}
class Threadjoin
{
public static void main(String args[])
{
Thread t1=new Thread(new MyRunnable(),"t1");
Thread t2=new Thread(new MyRunnable(),"t2");
Thread t3=new Thread(new MyRunnable(),"t3");
t1.start();
//start second thread after waiting for 2 seconds or its dead
try
{
t1.join(2000);
}
catch(Exception e)
{
System.out.println(e);
}
t2.start();
//start third thread after waiting for 2 seconds or its dead
try
{
t1.join();
}
catch(Exception e)
{
System.out.println(e);
}
t3.start();
//let all thread finish execution before finishing main thread
try
{
t1.join();
t2.join();
t3.join();
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("all thread are dead,existing main Thread");
}
}