File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Salient join() characteristics 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 "Salient join() characteristics" Watch "Salient join() characteristics" New topic
Author

Salient join() characteristics

Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
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.


Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
It made the join concepts clear... Thanks for the effort u took to make things clear


Sunita<br />SCJP 1.4
sun moon
Greenhorn

Joined: Oct 09, 2002
Posts: 28
hi,
i m preparing for scjp and ur topic helped me a lot by clearning doubts on join.

badri
Bharathi Balasubramanian
Greenhorn

Joined: Jul 12, 2000
Posts: 26
Nice example(s).. thanx


I got a flight to catch.
Arpana Rai
Ranch Hand

Joined: Nov 12, 2002
Posts: 93
Thanx Alfred for a such a thorough n simple expalanation.This is really a verry appreciable effort.
regds
Arpana


SCJP1.4(91%)
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
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

Joined: Aug 14, 2002
Posts: 279
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Salient join() characteristics
 
Similar Threads
A thread problem
Is this JWhiz thread question wrong?
Threads - join()
Threads and 1.4 exam
Child thread becoming a parent