This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Salient join() characteristics  RSS feed

 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I searched the forum for anything about join() and what I found was not sufficient, same like the API documentation wasn�t, so I said, well I have to try it myself.
The following 3 pieces of code exhibits 3 salient characteristics of join().
Please reply if anything is not clear, and if this post was helpful for you.
1-join() is a public, final, non-static/instance method of class Thread. It can be invoked with (), (long), (long, int) parameters.
2-If a thread (main) invokes the join() method on another thread (J) then the thread (main) leaves the running state and waits until (J) finish executing � J�s run method returns � then (main) try to go back to the running state again. BUT hay, read the next two well.
3-If join() is invoked with no arguments or an argument of value 0, then (main) waits forever till (J) finishes.

Output is:
main begin
J goes
J ends
main ends
4-If join() is invoked with an argument of (x) milliseconds � or of course (x) milli & (y) nano � then (main) waits till either (x) milli elapses OR (J) finishes, whichever happens first. The following code exhibits that:

Output is:
main begin
J goes
main ends
J ends
5-Although (main) relinquish the processor after calling join() on (J), it does NOT relinquish the locks it holds for objects.

Output is:
main begin
J Thread starts
Me main is back running before J or aMethod ends
cause: Me str is LOCKED by main
Damn, couldn't access str before cause it was locked
J Thread Ends
------------------------
So was that helpful at all? It better be cause I spent some time on that
Please ranchers correct me if anything I wrote is wrong, and tell me if it was of any help.
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It made the join concepts clear... Thanks for the effort u took to make things clear
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i m preparing for scjp and ur topic helped me a lot by clearning doubts on join.

badri
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice example(s).. thanx
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Alfred for a such a thorough n simple expalanation.This is really a verry appreciable effort.
regds
Arpana
 
Alfred Kemety
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, no if you really understood these concepts, What will happen, if in the last code example we change J.join(2000); into J.join();
Don't run and see the result, just guess it, and don't post the answer let others think a bit and I'll post it in a couple of days.
 
Alfred Kemety
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well if we change J.join(2000) to J.join() then the main thread will wait for ever... since J can't get hold of the monitor on the object (str) as the main thread still keeps all its locks - join() doesn't make the thread lose its locks -, they - both threads - won't continue running....
So the program will print:
main begin
J Thread starts
and holds and never stop running...
[ November 16, 2002: Message edited by: Alfred Kemety ]
 
I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!