File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Fox Hu
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Must wait() and notify() be written in a synchronized block?
 
Jon Strayer
Ranch Hand
Posts: 133
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Neal:
Must wait() and notify() be written in a synchronized block?

Yes.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic