wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes join() method of class Thread 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 "join() method of class Thread" Watch "join() method of class Thread" New topic
Author

join() method of class Thread

Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
hi:
I am a little confused about how join() works. Which thread will stop, and which one will continue.
Thread t = new Thread();
Thread x = new Thread();
t.start();
t.join();
x.start();
and also this case:
t.start();
join()
x.start();
join();
Thanks


SCJD 1.4<br />SCJP 1.4<br />-----------------------------------<br />"With regard to excellence, it is not enough to know, but we must try to have and use it.<br />" Aristotle
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
Hi Hanna,
here's my understanding of it, any corrections are welcome.
in the first example when you t.start(), t begins running, the next step in the current method is t.join(). t.join() won't return to the current method until t has died, the current method has to wait until t.join() returns to continue. When t dies, the current method can continue and call x.start() then exit.
The second case is the same, except now the current method waits for t to die, then x, then exits.
Dhanashree Mankar
Ranch Hand

Joined: Aug 25, 2003
Posts: 123
Originally posted by Hanna Habashy:
hi:
I am a little confused about how join() works. Which thread will stop, and which one will continue.
Thread t = new Thread();
Thread x = new Thread();
t.start();
t.join();
x.start();
and also this case:
t.start();
join()
x.start();
join();
Thanks

1>
in this case t.start will start a new thread.it will call run() method.
and then call to join(). means main threads wait till new thread completes it's work or till new thread dies.Then new object of thread class "x" starts new thread.
2>
second case is also same except join is not called with object It is not necessary also since join is instance method of thread class.also here x starts new thread and then wait for it to complete by calling join
Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
Thank you all. It is now clearer than before.
But, I have another quistion. In the case of using join(), one can guarnatee the order in which threads are excuted? I know it is up to the schadualer, but if I can can force main thread to wait before it runs another thread, then I am telling to the schadualer which thread to run first.
Am I correct?
Hanna
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: join() method of class Thread
 
Similar Threads
anonymous inner class output
Question on therad k&b 739 q.14
Thread class
Query about start() and join()
join( )