aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes notify() and notifyAll() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "notify() and notifyAll()" Watch "notify() and notifyAll()" New topic
Author

notify() and notifyAll()

Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200


When I run the code above it produces the follwing result:

Waiting for calculation...
Waiting for calculation...
Waiting for calculation...
Total is : 4950
Total is : 4950
Total is : 4950

Then I change the notifyAll() to just notify(), I get the ff result:

Waiting for calculation...
Waiting for calculation...
Waiting for calculation...
Total is : 4950
Total is : 4950
Total is : 4950

My question is why is it the same? I was expecting that only one thread will be notified so there will only be one "Total is : 4950" result.


Be Afraid...Be very Afraid...
Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200
anyone?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18977
    
  40

Paulo,

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.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200
Yes Henry, You're right! I was just wondering and for SCJP exam purposes, do I need to catch that kind of behavior?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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
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
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
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
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Jane W pemberley
Greenhorn

Joined: Apr 26, 2005
Posts: 12
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.

"while (<condition does not hold> ; )"

Thanks & Regards
Jane
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: notify() and notifyAll()