isAlive( ) and join( ) in Java

By: Ivan Lim  

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.




Archived Comments

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

2. I Sortons un moment de l'art serieux pour donner quelques heures d'attention a l'art du badinage; c'
View Tutorial          By: soins massage lyon at 2017-03-31 02:26:12

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

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

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

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

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

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

9. you seriously took every word from book
JAVA : THE COMPLETE REFERENCE
and pasted it

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

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

11. New thread: Thread[One,5,main]
How is the second value 5 got?

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

12. After seeing this explaination and example i wanted to search on the web and arrived here. You have
View Tutorial          By: Sumit Vishwakarma at 2012-11-29 21:55:36

13. All of this has been copy pasted from Hebert Schildt's AThe complete Reference......
View Tutorial          By: Cazorla at 2012-11-15 19:32:49

14. please explain thread t=new thread(this,name). What will be the values in this keyword and what Syst
View Tutorial          By: Bhavin at 2012-09-28 12:26:55

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

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

17. The example given here are simple and easy to run and understand the basics of java.
Thanks
View Tutorial          By: Kranti at 2012-04-26 10:18:34

18. I'd like to Thanks first bcoz your program is really a good and easy to understand...
It help

View Tutorial          By: Pawan at 2012-04-24 19:46:00

19. It's a good eg to learn join and isAlive method.But have a doubt
after the join method the i

View Tutorial          By: Nithin at 2012-02-02 08:43:28

20. too difficult example..
no good

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

21. too difficult example..
no good

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

22. I'm sorry I've misunderstood, join() guarantees that the parent thread won't terminate until it's ch
View Tutorial          By: Junkie Zhang at 2012-01-10 06:02:00

23. Nice example but does not work on me, after executing the three join() statements, the console shows
View Tutorial          By: Junjie Zhang at 2012-01-10 03:26:12

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

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

26. ha ha you have given as same in complete reference 7th edition book. give some simple examples, dont
View Tutorial          By: reddy at 2011-11-01 20:36:21

27. This example shows that when the thread is suspended for 1000 ms then other thread takes control and
View Tutorial          By: Sam at 2011-10-02 20:30:38

28. What gud?
its the copied example from Complete Reference.....
:@

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

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

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

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

32. This article is a direct copy from Herbert Schildt's The Complete Reference : Java2.
View Tutorial          By: Tushar at 2011-04-22 21:25:25

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

34. as we write:
ob1.t.join();
Does t refer to main thread?
or if we write:

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

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

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

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

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

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

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

41. Thanks, it was really helpful.
But wouldn't the InterruptedException be caught each time slee

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

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

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

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

45. this example is the copy of herbert schildt add new example.avoid to copy thanks
View Tutorial          By: animesh singh at 2009-10-09 07:49:09

46. this example is the copy of herbert schildt add new example.avoid to copy thanks
View Tutorial          By: animesh singh at 2009-10-09 07:48:41

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

48. This Example program is very useful.
good work. Thank You.

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


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial