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

wait() method

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the effect of issuing a wait() method on an object
A. If a notify() method has already been sent to that object then it has no effect
B. The object issuing the call to wait() will halt until another object sends a notify() or notifyAll() method
C. An exception will be raised
D. The object issuing the call to wait() will be automatically synchronized with any other objects using the receiving object
The answer given is b, but IMO answer should be b,c. bcoz wait() method gives InterruptedException.
Please give ur opinion..
--Shallender
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That question is badly stated. Only Threads execute calls to methods. A Thread "A" with a synchronized lock on an object can call wait() and enter a waiting condition.
Another Thread "B" with a synchronized lock on the SAME object can call notify() or notifyAll() and the waiting Thread will enter the runnable condition. When Thread B loses the synchronized lock on the object, Thread A gets a chance to run.
You get a RuntimeException (I forget the exact type) if your Thread calls wait or notify or notifyAll WITHOUT having a synchronized lock on the object.
Don't think of wait etc as affecting an object but as affecting a Thread executing a method.
Bill
 
Rong Chen
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wait(), notify() and notifyall() methods must be executed in synchronized code, otherwise the call will result in an IllegalMonitorStateException.
So an Exception isn't always raised after wait().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic