aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes join() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "join() method" Watch "join() method" New topic
Author

join() method

Divya Gehlot
Ranch Hand

Joined: Sep 10, 2006
Posts: 243
Hi,
I am confused with the functionality of join() method what actually It is doing. when a thread call the join method what's happening(internally) . can any one explain me with example


SCJP1.5(81%), SCDJWS(94%), next mission SCEA (but need to wait or that)
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
I cannot tell you what exactly happens internally when you call join on a thread , but until the thread dies (the thread on which you have called the join) the control would not return.Check for some sample program to learn more about this.


Rahul Bhattacharjee
LinkedIn - Blog
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
t.join(); means you are joining currently executing thread at the end of t thread. Currently executing thread says join me at the end of t, so after t complete or become dead, I will start. Also read a good description is given by K & B book, page:699.


SCJP-1.5<br />SCWCD-1.4
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6657
    
    5

Originally posted by Divya Gehlot:
Hi,
I am confused with the functionality of join() method what actually It is doing. when a thread call the join method what's happening(internally) . can any one explain me with example


Use join() to join some other thread chain. Sometimes the main thread is made to join other threads so that it is the last one to finish. Have a look at the SCJP study guide for more information


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
chintan ramavat
Ranch Hand

Joined: Sep 15, 2006
Posts: 134
Thread flow is totally JVM and Platform Dependant, It's important to know what is guaranteed and what is not. JOIN() method is used for saying "I am DONE with my work" and you can take the control back.

so when you hit the join() method, t1.join(t2) so t2 is the definite to be executed once t1 completes the job. and if there's nothing as t2, then MAIN thread the service thread (MAIN METHOD) will continue with it's work.

so again as i mentioned , it's upto JVM and system to whom to give chance and select but join guarantees the other thread joined with get executed.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy, ranchers!

chintan ramavat posted yesterday

(...) so when you hit the join() method, t1.join(t2) so t2 is the (...)



t1.join(t2) ?
No, you can't do that.

You cannot specify a thread to join a certain thread. There is no method of join() that takes a thread / runnable as argument.
It is only possible to give join() a timeout as parameter.

The API about join() is indeed a bit laconic. It tells you only six words:
Waits for this thread to die.

Nothing else. But it explains everything.
When you see something like

aThread.join();

it means that the currentthread (the one who reads the line) will wait until aThread has finished its run method and dies thereafter.
And this is guaranteed, in threading a lot is platform dependent, but this is safe: the current thread will wait.


Demo:



From the output you see that the current thread (main in this case) waits, until thr2 is ready. You may also see things like this:
...
thr1: i=3
mainthr1: i=2
: i=2
...

because there is no synchronization, but that the main thread waits for thr2 in the second part is guaranteed.




Yours,
Bu.


all events occur in real time
chintan ramavat
Ranch Hand

Joined: Sep 15, 2006
Posts: 134
thank you burkhard for making it clear. JOIN() is not overloaded method, moreover over it guarantees wait for the death of the currently running thread.now i got it. i had misconception about the syntax of the yield. whatif we create thread with the highest priority , i guess still the selection of HIGHEST priority thread is JVM and Platform dependent so never know either it will be MAIN or the one we created with the highest priority, but its just that we can have something then nothing.
- thanks Burkhard one more time for clearing me up.
- chintan ramavat
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: join() method
 
Similar Threads
Thread signatures help please
problem to block a method until a line complete.
can i get an example which illustrate the use of join() method
SCJWSD online Group study
WA #1.....word association