This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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: 9291
    
  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: 3573
    
  14

http://www.coderanch.com/t/524526/java/java/Why-wait-method-define-synchronized
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what wait() should be called from Synchrnoized block
 
Similar Threads
Question about wait() method
Doubt on mock exam
wait method calls
illegalMonitorStateException
wait()