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.
Thanx Alfred for a such a thorough n simple expalanation.This is really a verry appreciable effort. regds Arpana
posted 16 years ago
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.
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 ]