File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused from K&B Book please explain?

 
Aaron Raja
Ranch Hand
Posts: 206
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain join method of Thread?
K&B Book say
"When one thread calls the join() method of another thread, the currently running thread will wait until the thread it joins with has completed. Think of the join() method as saying, �Hey thread, I want to join on to the end of you. Let me know when you�re done, so I can enter the Runnable state.�
Lets see there are three Thread t1, t2,and t3.
Thread t1 = new Thread();
Thread t2 = new Thread();
Thread t3 = new Thread();

t1.start();
t2.start();
t3.start();

t1.join();
t3.join();
t2.join();
So in this senerio t2 will run first then t3 then only t1 because K&B say"When one thread calls the join() method of another thread, the currently running thread will wait until the thread it joins with has completed"
[ June 17, 2007: Message edited by: Aaron Raja ]
 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

t1.join();
t3.join();
t2.join();
So in this senerio t2 will run first then t3 then only t1 because K&B say"When one thread calls the join() method of another thread, the currently running thread will wait until the thread it joins with has completed"


How did you draw this conclusion?

First, you need to realize that there are 4 threads in this scenario. The first three are the threads that were started, and the fourth is the thread that started the other three. (let's call this the main thread, assuming that this code is in the main method)

So the "t1.join()" command basically tells the main thread to wait until t1 finishes. Once that is done, then it can go on to wait for t3 to finish, and then for t2 to finish.

Also note, that when the main thread waits for t1, t2, or t3 to finish, it may already have been completed, in which case, it will retrun immediately.

You can't draw any conclusion from the code, which thread run first, nor which thread finished first. You only know that the main thread waited for the t1 thread first, t3 thread second, and t2 thread last -- and those threads may not have finished in that order.

Henry
 
chintan ramavat
Ranch Hand
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends !

Then what is guaranteed here, is that MAIN will wait or either way T1 gets chance ? still in discussion or join does not guarantee anything not even if it gets called ? please explain if anybody would

thank you
 
Manfred Klug
Ranch Hand
Posts: 377
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi chintan,

The only guarantee for Thread.join is that it waits until the thread terminates or there is an InterruptedException. You cannot make any assumptions about the execution of the threads.

If we assume that there is no InterruptedException you have the following guarantees:

t1.join() - After this instruction, t1 has finished. But t2 and t3 may have finished too.

t3.join() - After this instruction, t3 has finished. But t2 may have finished too.

t2.join() - After this instruction, t2 has finished.
 
chintan ramavat
Ranch Hand
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it !

- thanks
 
Vyas Sanzgiri
Ranch Hand
Posts: 686
Chrome Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic