Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what wait() should be called from Synchrnoized block

 
Sony Agrawal
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Stephan van Hulst
Bartender
Pie
Posts: 5590
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.coderanch.com/t/524526/java/java/Why-wait-method-define-synchronized
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic