isAlive( ) and join( ) in Java

By: Ivan Lim Viewed: 169542 times  Printer Friendly Format    


The main thread must be the last thread to finish. Sometimes this is accomplished by calling sleep() within main( ), with a long enough delay to ensure that all child threads terminate prior to the main thread. However, this is hardly a satisfactory solution, and it also raises a larger question: How can one thread know when another thread has ended? Fortunately, Thread provides a means by which you can answer this question.

Two ways exist to determine whether a thread has finished. First, you can call isAlive( ) on the thread. This method is defined by Thread, and its general form is shown here:

final boolean isAlive( )

The isAlive( ) method returns true if the thread upon which it is called is still running. It returns false otherwise. While isAlive( ) is occasionally useful, the method that you will more commonly use to wait for a thread to finish is called join( ), shown here:

final void join( ) throws InterruptedException

This method waits until the thread on which it is called terminates. Its name comes from the concept of the calling thread waiting until the specified thread joins it. Additional forms of join( ) allow you to specify a maximum amount of time that you want to wait for the specified thread to terminate. Here is an improved version of the preceding example that uses join( ) to ensure that the main thread is the last to stop. It also demonstrates the isAlive( ) method.

// Using join() to wait for threads to finish.
class NewThread implements Runnable {
String name; // name of thread
Thread t;
NewThread(String threadname) {
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println(name + " interrupted.");}
System.out.println(name + " exiting.");
}
}

class DemoJoin {
public static void main(String args[]) {
NewThread ob1 = new NewThread("One");
NewThread ob2 = new NewThread("Two");
NewThread ob3 = new NewThread("Three");
System.out.println("Thread One is alive: "+ ob1.t.isAlive());
System.out.println("Thread Two is alive: "+ ob2.t.isAlive());
System.out.println("Thread Three is alive: "+ ob3.t.isAlive());
// wait for threads to finish
try {
System.out.println("Waiting for threads to finish.");
ob1.t.join();
ob2.t.join();
ob3.t.join();
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Thread One is alive: "+ ob1.t.isAlive());
System.out.println("Thread Two is alive: "+ ob2.t.isAlive());
System.out.println("Thread Three is alive: "+ ob3.t.isAlive());
System.out.println("Main thread exiting.");
}
}

Sample output from this program is shown here:

New thread: Thread[One,5,main]
New thread: Thread[Two,5,main]
New thread: Thread[Three,5,main]
Thread One is alive: true
Thread Two is alive: true
Thread Three is alive: true
Waiting for threads to finish.
One: 5
Two: 5
Three: 5
One: 4
Two: 4
Three: 4
One: 3
Two: 3
Three: 3
One: 2
Two: 2
Three: 2
One: 1
Two: 1
Three: 1
Two exiting.
Three exiting.
One exiting.
Thread One is alive: false
Thread Two is alive: false
Thread Three is alive: false
Main thread exiting.

As you can see, after the calls to join( ) return, the threads have stopped executing.



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. This Example program is very useful.
good w

View Tutorial          By: sathya at 2008-03-28 02:21:41

2. Nice work. Thanks
View Tutorial          By: Karthik at 2009-09-30 08:02:36

3. this example is the copy of herbert schildt add ne
View Tutorial          By: animesh singh at 2009-10-09 07:48:41

4. this example is the copy of herbert schildt add ne
View Tutorial          By: animesh singh at 2009-10-09 07:49:09

5. Cheated from Herbert Schildt.........and again no
View Tutorial          By: suraj gupta at 2010-02-26 02:40:12

6. Thanks!! This example was very helpful for me...
View Tutorial          By: Jonathas at 2010-03-11 07:47:51

7. How I will make main thread to wait, excluding Thr
View Tutorial          By: prashant shelke at 2010-06-08 08:12:47

8. Thanks, it was really helpful.
But wouldn't

View Tutorial          By: Ron at 2010-06-27 14:22:36

9. thanx it was really helpful
View Tutorial          By: Vidisha at 2010-07-09 06:21:32

10. thanx it was really helpful....
View Tutorial          By: Vidisha at 2010-07-09 06:27:34

11. its so nice
View Tutorial          By: senthilkumar at 2010-11-11 02:44:37

12. Thanks , really appreciate
View Tutorial          By: Marlen at 2010-11-23 10:50:29

13. nice intelligent work
View Tutorial          By: santosh at 2010-11-28 01:18:50

14. nice example and this is very use ful
View Tutorial          By: jyothi at 2011-02-18 22:24:51

15. as we write:
ob1.t.join();
Does t

View Tutorial          By: abc123 at 2011-04-03 13:33:33

16. Thank you, I need exactly this; cheers :)
View Tutorial          By: Lang at 2011-04-08 09:33:24

17. This article is a direct copy from Herbert Schildt
View Tutorial          By: Tushar at 2011-04-22 21:25:25

18. nice work.......
View Tutorial          By: sukirti at 2011-05-06 16:29:11

19. nice work.......
View Tutorial          By: sukirti at 2011-05-06 16:30:19

20. nice example 2 uderstand
View Tutorial          By: nilesh doshi at 2011-05-09 01:24:24

21. What gud?
its the copied example from Compl

View Tutorial          By: Ankur at 2011-09-30 06:31:46

22. This example shows that when the thread is suspend
View Tutorial          By: Sam at 2011-10-02 20:30:38

23. ha ha you have given as same in complete reference
View Tutorial          By: reddy at 2011-11-01 20:36:21

24. Thank you. It is very good.
View Tutorial          By: tapes pra at 2011-12-08 09:22:33

25. Thank you. It is very good.
View Tutorial          By: tapes pra at 2011-12-08 09:23:31

26. Nice example but does not work on me, after execut
View Tutorial          By: Junjie Zhang at 2012-01-10 03:26:12

27. I'm sorry I've misunderstood, join() guarantees th
View Tutorial          By: Junkie Zhang at 2012-01-10 06:02:00

28. too difficult example..
no good

View Tutorial          By: giorg at 2012-01-11 22:58:14

29. too difficult example..
no good

View Tutorial          By: giorg at 2012-01-11 23:11:16

30. It's a good eg to learn join and isAlive method.Bu
View Tutorial          By: Nithin at 2012-02-02 08:43:28

31. I'd like to Thanks first bcoz your program is real
View Tutorial          By: Pawan at 2012-04-24 19:46:00

32. The example given here are simple and easy to run
View Tutorial          By: Kranti at 2012-04-26 10:18:34

33. copied from Herb's complete reference on java
View Tutorial          By: Cj at 2012-06-14 06:48:32

34. this is copy paste from complete reference book of
View Tutorial          By: Bad ass at 2012-07-30 17:25:40

35. please explain thread t=new thread(this,name). Wha
View Tutorial          By: Bhavin at 2012-09-28 12:26:55

36. All of this has been copy pasted from Hebert Schil
View Tutorial          By: Cazorla at 2012-11-15 19:32:49

37. After seeing this explaination and example i wante
View Tutorial          By: Sumit Vishwakarma at 2012-11-29 21:55:36

38. New thread: Thread[One,5,main]
How is the s

View Tutorial          By: Venkatesh at 2013-04-21 07:06:15

39. KennethBew
View Tutorial          By: KennethBew at 2015-07-06 06:51:33

40. you seriously took every word from book
JA

View Tutorial          By: keshav sharma at 2016-06-25 00:28:55

41. ArtemSeoKrporma
View Tutorial          By: ArtemSeoKrporma at 2017-01-04 03:26:16

42. ArtemSeoKrporma
View Tutorial          By: ArtemSeoKrporma at 2017-01-08 15:22:46

43. ArtemSeoKrporma
View Tutorial          By: ArtemSeoKrporma at 2017-01-16 14:23:34

44. Krasnov85porma
View Tutorial          By: Krasnov85porma at 2017-02-23 01:42:45

45. Dorothynob
View Tutorial          By: Dorothynob at 2017-02-24 07:22:02

46. ValeriaGoodloacy
View Tutorial          By: ValeriaGoodloacy at 2017-03-23 09:28:23

47. I Sortons un moment de l'art serieux pour donner q
View Tutorial          By: soins massage lyon at 2017-03-31 02:26:12

48. JasonForma
View Tutorial          By: JasonForma at 2017-09-20 10:40:46