File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes notify and notifyAll 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 » Developer Certification (SCJD/OCMJD)
Bookmark "notify and notifyAll" Watch "notify and notifyAll" New topic
Author

notify and notifyAll

Jonny Butamer
Greenhorn

Joined: Oct 27, 2004
Posts: 15
Hi

I� am a bit confused about when notify or notifyAll should be call and am hoping some body might be able to explain a few things for me.

Should notify or notifyAll be called at the end of every synchronized block or will notify be called automatically when the synchronized block is exited?

If a synchronized block is interrupted for some reason eg an Exception is caught should notify or notifyAll all be called?

If a method is synchronized should notify or notifyAll be called before that method is returned?

If no for all of the above when should notify or notifyAll be called?

Thanks in advance for any replys
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
Hi jonny,

I was actually confused about this myself, until a fellow rancher showed me the errors of my ways.

When a thread tries to enter a synchronized block, it needs to get the exclusive lock on whatever object is being used for the locking. If another thread has the lock, it goes into a blocked state. Whenever the thread that held the lock exits the synchronized block, it gives up the lock, and a blocked thread will automatically grab it and become active. You don't have to call anything special in the code.

notify and notifyAll are used to wake up waiting threads, or threads that have called the wait() method. When a waiting thread is notified, it tries to grab the exclusive lock, and if it can't, enters the blocked state. Hope that helped!


“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Kai Witte
Ranch Hand

Joined: Jul 17, 2004
Posts: 354
hello,

to avoid digging your own trap I recommend to use notifyAll only, not notify. Besides always call wait inside a loop, e. g.

I recommend Effective Java for best practices in this matter (and many other contexts). Might be good to wait for a 5.0-adjusted edition, though.

Conan


Kai Witte's business website Kai Witte's private homepage Mock exam / preparation kit reviews
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18113
    
  39

Originally posted by Conan Elvitaro:
to avoid digging your own trap I recommend to use notifyAll only, not notify.


Generally, notifyAll() is "preferred" over notify() because either (a) the wrong thread is awoken, or (b) notifications are lost. Neither of these reasons should be considered valid. You should not have threads share locks if they are waiting for different conditions/states. And you should set variables during the synchronization block to prevent notifications from being lost.

IMO, the only reasons for using notify all is when (a) a condition is changed that can satisfy more than one thread or (b) a condition has changed when all threads must respond to. You shouldn't be using notifyAll() just because you can't get notify() to work correctly.

Henry
[ March 24, 2005: Message edited by: Henry Wong ]

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

Joined: May 24, 2004
Posts: 783
Valid points, however, the general consensus of this forum has been to use notifyAll() in the unlock method. You will not lose any points on the assignment using notifyAll(), but there have been situations where people have lost points for only using notify().
(a) a condition is changed that can satisfy more than one thread
This is often the case with deleted records, so once again, I would recommend using notifyAll() for this assignment.
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
You should not have threads share locks if they are waiting for different conditions/states.
Also, in this assignment, many are following the example from Max's book and using a collection such as a HashMap to logically lock records. In this situation, all threads are using the HashMap as the lock object, but waiting on different conditions (whether or not a specific record number exists in the HashMap). Only using notify() in the unlock can potentionally cause deadlock.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11278
    
  59

Hi Jonny,

As previously requested, could you please change your display name to comply with The JavaRanch Naming Policy..

Thanks,

Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Jonny Butamer
Greenhorn

Joined: Oct 27, 2004
Posts: 15
Hi

Thanks for the replys. Andrew I have updated my profile but not sure if it changes my display name. How do i do that?

I'am still a bit confused about when i should call notify or notifyAll. I understand the difference between them and I know when not to call them. But am still a bit confused about where exactly i should call one of them.

If i have a wait() call inside a synchronized block should I call notifyAll/notify at the end of that synchronized block?
For example:



Is this correct use of notifyAll? If it is not, when for the above scenario should notifyAll/notify be called?
Thanks in advance.
Kai Witte
Ranch Hand

Joined: Jul 17, 2004
Posts: 354
hello,

I told you already to call wait only inside a loop like that:

The right moment to call notifyAll is when you have a good reason to believe that maybe (or for sure) the condition (a volatile boolean field named "finished" in this case, but it can also be an other condition) has changed.

Example:
inside lock:

inside unlock:


@Henry ack

Conan
Jonny Butamer
Greenhorn

Joined: Oct 27, 2004
Posts: 15
Thanks for that Conan

I actually have wait() inside a while loop but left it out of the snippet i put in my last post. Sorry.

I think I understand now. But just to be sure. If i have the following:
(lockedRecords is a HashMap)


Where ever I make a change to the lockedRecords I should call notify/notiyAll?

Thanks
Kai Witte
Ranch Hand

Joined: Jul 17, 2004
Posts: 354
hello,

the major difference between your case and my case is that in I synchronize and wait on an Integer that only represents one record.

The consequence is that you really HAVE TO use notifyAll whenever you remove anything from lockedRecords. I could as well use notify, I just don't because I follow other rules than Henry does. (He made a good point though, and I will reconsider it for the future.)

So yes, call notifyAll if you want to keep the code you showed us. Unlike me you have to rely on the while (condition) and on the notifyAll to make it work.

Conan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: notify and notifyAll
 
Similar Threads
wait() and notify() methods
Object Lock
two questiones about synchronized block.
Question about wait() method
From Velmurugan's Notes