Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

join() method

 
Divya Gehlot
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
chintan ramavat
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
chintan ramavat
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic