aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes To Notify or to not Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "To Notify or to not" Watch "To Notify or to not" New topic
Author

To Notify or to not

Chris Bicnal
Ranch Hand

Joined: Aug 17, 2005
Posts: 80
    
    1
Hi guys,

I'm in deepest darkest B&S hell at the moment and I'm trying to figure my locking strategy out.

I know what I want to lock and how I want to lock it but I'm having trouble with the notifyAll method.

I'm particularly confused by the unlock method, as required in the spec...

// Releases the lock on a record. Cookie must be the cookie
// returned when the record was locked; otherwise throws SecurityException.
public void unlock(long recNo, long cookie)
throws SecurityException;

My question is; after I have determined that the record is locked with the cookie passed in do I need to call notifyAll? In psuedo-code my unlock looks like this...

synchronise on the locked record map
is the record number passed in the locked record map
is the record number locked with the passed in cookie?
if not throw an exception *here*
if it is we remove the key/value pair from the locked record map
*here*

The two instances of *here* are where I'm having problems with calling notifyAll. Do I need to call notifyAll just before I throw the exception to wake up any waiting threads? Likewise, do I need to call it when I've finished processing, just before I leave the synchronised block?

Also, what would happen if I didn't? Would any thread that called wait() on the locked object just wait infinitely until a notifyAll was called? Does this mean I should always call notifyAll whenever I leave a synchronised block?

Thanks in advance!

Chris
Chris Bicnal
Ranch Hand

Joined: Aug 17, 2005
Posts: 80
    
    1
OK, so first of all thanks for all the help! ;-)

I think I've figured this out - notify/notifyAll is used to 'wake' a thread that's called wait on the lock object.

So, if I leave a synchronised block without calling notify/notifyAll any threads that have called wait wont change their status. The lock object will become available for other threads to synchronise on it, but the threads that have called wait will be blocked.

Therefore I only need to call notify/notifyAll at the end of the opposing synchronised block that called wait.

For example, if I call wait in a lock method I should call notify/notifyAll in the corresponding unlock method. There's not much point calling it elsewhere as, in this example, unless the unlock method has been called the thread waiting to lock something will re-enter the waiting state as soon as it's woken (as nothing has been unlocked since it's been waiting).

I think that makes sense....and talking to myself has helped my understanding!

Chris
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
Originally posted by Chris Bicnal:
[QB]OK, so first of all thanks for all the help! ;-)

I'm glad our strategy of "not answering you" worked! It was the master plan.


I think I've figured this out - notify/notifyAll is used to 'wake' a thread that's called wait on the lock object.

I agree..


So, if I leave a synchronised block without calling notify/notifyAll any threads that have called wait wont change their status. The lock object will become available for other threads to synchronise on it, but the threads that have called wait will be blocked.

I agree.


Therefore I only need to call notify/notifyAll at the end of the opposing synchronised block that called wait.

For example, if I call wait in a lock method I should call notify/notifyAll in the corresponding unlock method. There's not much point calling it elsewhere as, in this example, unless the unlock method has been called the thread waiting to lock something will re-enter the waiting state as soon as it's woken (as nothing has been unlocked since it's been waiting).

I agree!

But.. how about notify or notifyAll ? Which one will you use ?

Alex
 
Don't get me started about those stupid light bulbs.
 
subject: To Notify or to not