This week's giveaways are in the MongoDB and Jobs Discussion forums. We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line! See this thread and this one for details.
Try it again. This time, don't call either notify() or notifyAll(). I think you will see the same result.
I think what you are seeing is an effect of the Calculator thread exiting. I am guessing somewhere in the bowels of the JVM, one of the cleanup procedures for a terminating thread generates notifications on the thread object.
Originally posted by Henry Wong: ...I am guessing somewhere in the bowels of the JVM, one of the cleanup procedures for a terminating thread generates notifications on the thread object...
I noticed that behavior as well -- that the result was the same without any "notify" call -- but I couldn't figure out why. I suspected this might be some sort of "cleanup," but I didn't know whether that made sense. So thanks for posting!
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Jane W pemberley
Greenhorn
Joined: Apr 26, 2005
Posts: 12
posted
0
Bonjour! M.Wong, I tried the same without any notify or notifyAll calls ... For the first time, I got this output...
D:\>java SyncNotifyAll Waiting for calculation... Waiting for calculation... I am notified Thread[Thread-2,5,main] Total is : 4950 I am notified Thread[Thread-1,5,main] Total is : 4950 Waiting for calculation...
and the system stood waiting .. I had to press ctrl+c to come out ..
and in later executions I didn't get this kind of behaviour .. all threads are nicely getting finished . If one of the clean up procedures of JVM are giving notification on the thread object .... how come this kind of behaviour is possible ...
and also ... objects would never become eligible for garbage collection as long as there are refs from any live thread ... then .. what abt this clean up procedure ... I am not getting the point .. will U please make it little clear . Thanks & Regards Jane.
Animesh Shrivastava
Ranch Hand
Joined: Jul 19, 2004
Posts: 298
posted
0
My observations on this are: If u dont give any notify or notifyAll method, as soon as the calculator thread returns back from execution of its run method, all the threads waiting for the acquisition of the Calculator object lock, gets released.
In ur case Jane, what i guess is that the execution of Calculator thread gets completed before the last thread(SyncNotifyAll) runs. So the last thread goes into waiting and no one is there to notify. In ur code just put a print statement in the run method of Calculator. U can find this happening.
But i also dont know why this behaviour is happening?. If i put a notify statement, since notify method can only notify a single thread and not all of those waiting, only one thread amongst waiting is notified. But the rest two i dont understand how they come out of the blocked state.
Correct me if i am wrong anywhere
Jane W pemberley
Greenhorn
Joined: Apr 26, 2005
Posts: 12
posted
0
Hi Srivatsava, Here I am presenting 3 runs which are executed on HP-UX unix machines..with slight code changes
This is without any code changes .. ie without any notify methods hpmdd137::/kbase/java/practice >> java SyncNotifyAll Waiting for calculation... I am calculator thread I am notified Thread[Thread-3,5,main] Total is : 4950 Waiting for calculation... Waiting for calculation... hpmdd137::/kbase/java/practice >> (Notice the output and particularly last two lines)
This with a join() method in Calculator's run method hpmdd137::/kbase/java/practice >> java SyncNotifyAll Waiting for calculation... Waiting for calculation... Waiting for calculation... I am calculator thread hpmdd137::/kbase/java/practice >>
This is without join() and with a statement at the end of Calculator's run method .. just before the thread is going to die ( ie it finishes the execution of run method) hpmdd137::/kbase/java/practice >> java SyncNotifyAll Waiting for calculation... Waiting for calculation... I am calculator thread I am going to die I am notified Thread[Thread-3,5,main] Total is : 4950 Waiting for calculation... I am notified Thread[Thread-2,5,main] Total is : 4950 (Notice here the last thread is waiting).
U said ... "the execution of Calculator thread gets completed before the last thread(SyncNotifyAll) runs. So the last thread goes into waiting and no one is there to notify".What if the Calculator thread is waiting for SyncNotifyAll threads to get completed using a call to join.In that case please refer the 2nd run .. all the 3 SyncNotifyAll threads remained waiting ... ( What do U say?)
Totally .. I don't understand why these threads are coming out of wait state in few runs and not at other times .....
I noticed it on both windows and HP-UX m/cs...(I thought ..as Threads in java are implementation dependent at least to a little extent .. I could find any diff in the way threads are notified).
But .. I couldn't deduce anything now.. why this is happening ...ouf! Regards Jane
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
posted
0
This is from the Java API for the Object class' wait method:
A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied. In other words, waits should always occur in loops, like this one:
synchronized (obj) { while (<condition does not hold> ; ) obj.wait(timeout); ... // Perform action appropriate to condition }
You are most likely to be seeing a case of this in your example. [ April 26, 2005: Message edited by: Barry Gaunt ]
Hi, Thank you Barry for letting me know about this spurious wakeup.Got the point now.Will u please give me an example of a condition to be checked in this loop construct.