aspose file tools*
The moose likes Beginning Java and the fly likes Must wait() and notify() be written in a synchronized block? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Must wait() and notify() be written in a synchronized block?" Watch "Must wait() and notify() be written in a synchronized block?" New topic
Author

Must wait() and notify() be written in a synchronized block?

Fox Hu
Ranch Hand

Joined: Jan 23, 2003
Posts: 49
Must wait() and notify() be written in a synchronized block?
Jon Strayer
Ranch Hand

Joined: Dec 04, 2002
Posts: 133
Originally posted by Neal:
Must wait() and notify() be written in a synchronized block?

Yes.


Jon
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
There must be some sort of synchronization. Both these method will throw an IllegalMonitorStateException if called from code that doesn't already hold the lock for whatever object is being used to invoke wait() or notify(). Which meanse that in order to have previously acquired this lock, you must be inside a synchronized block or synchronized method. Or, inside a method that was called from within another method that had already acquired the lock, using a synchronized method or block, etc. It's possible to create examples where the synchronization might be many levels removed from the method that calls wait() or notify(), possibly even in another class. Though that's probably a bad idea for readability...


"I'm not back." - Bill Harding, Twister
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Neal,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Must wait() and notify() be written in a synchronized block?