aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confused from K&B Book  please explain? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confused from K&B Book  please explain?" Watch "Confused from K&B Book  please explain?" New topic
Author

Confused from K&B Book please explain?

Aaron Raja
Ranch Hand

Joined: Jun 07, 2007
Posts: 206
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 ]

Thanks, AR
SCWCD 5, SCJP 1.4, OCA (PL/SQL)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18997
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
chintan ramavat
Ranch Hand

Joined: Sep 15, 2006
Posts: 134
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

Joined: Jun 04, 2007
Posts: 377
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

Joined: Sep 15, 2006
Posts: 134
got it !

- thanks
Vyas Sanzgiri
Ranch Hand

Joined: Jun 16, 2007
Posts: 686

Thank you


===Vyas Sanzgiri===
My Blog
 
Consider Paul's rocket mass heater.
 
subject: Confused from K&B Book please explain?