aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what wait() should be called from Synchrnoized block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what wait() should be called from Synchrnoized block" Watch "what wait() should be called from Synchrnoized block" New topic
Author

what wait() should be called from Synchrnoized block

Sony Agrawal
Ranch Hand

Joined: Oct 04, 2009
Posts: 143
Could you please tell me why wait() method should be called from a sync block/method.
Why cant it be just be called like join() i.e from a non synch block/method
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

wait and join are two completely different things. join can only be called on a Thread object, but wait can be called on any object. To call wait on an object, you need to hold lock to that object. The idea behind it is that if some code has lock on an object (we don't want multiple threads to access this object simultaneously) and for some reason it can't use the object (lets say the code is waiting for user's input), then to allow other threads to use the object, the code will call wait on the object. That way it will release the lock on that object allowing other threads to use the object...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

http://www.coderanch.com/t/524526/java/java/Why-wait-method-define-synchronized
 
Don't get me started about those stupid light bulbs.
 
subject: what wait() should be called from Synchrnoized block