1class ThreadJoining extends Thread
2{
3 @Override
4 public void run()
5 {
6 for (int i = 0; i < 2; i++)
7 {
8 try
9 {
10 Thread.sleep(500);
11 System.out.println("Current Thread: "
12 + Thread.currentThread().getName());
13 }
14
15 catch(Exception ex)
16 {
17 System.out.println("Exception has" +
18 " been caught" + ex);
19 }
20 System.out.println(i);
21 }
22 }
23}
24
25class GFG
26{
27 public static void main (String[] args)
28 {
29 ThreadJoining t1 = new ThreadJoining();
30 ThreadJoining t2 = new ThreadJoining();
31 ThreadJoining t3 = new ThreadJoining();
32 t1.start();
33 try
34 {
35 System.out.println("Current Thread: "
36 + Thread.currentThread().getName());
37 t1.join();
38 }
39 catch(Exception ex)
40 {
41 System.out.println("Exception has " +
42 "been caught" + ex);
43 }
44 t2.start();
45 try
46 {
47 System.out.println("Current Thread: "
48 + Thread.currentThread().getName());
49 t2.join();
50 }
51 catch(Exception ex)
52 {
53 System.out.println("Exception has been" +
54 " caught" + ex);
55 }
56 t3.start();
57 }
58}