Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java: wait(), notify() and synchronized blocks

I learned that calling an Object's wait() method will release the object monitor, if present.

But I have some questions regarding calling notify() on this object by another thread:

  1. (when) will the waiting thread wake up, if another (a 3rd) thread owns the object monitor in the meanwhile?

  2. will the waiting thread wake up, if a 3rd thread called wait() on this object?

  3. is it possible to determine if a thread is waiting for notifying a particular object (java 1.4/java 5)

  4. What's happening if wait() will be called in the finalize() method?

like image 964
MRalwasser Avatar asked Jul 06 '10 21:07

MRalwasser


People also ask

Why wait () notify () and notifyAll () method have to be called from the synchronized context?

If no threads are waiting in the waiting queue, then notify() and notifyAll() have no effect. Before calling the notify() or notifyAll() method of an object, a thread must own the lock of the object. Hence it must be in one of the object's synchronized methods or synchronized block.

Does wait () Release lock from synchronized block?

The wait function doesn't release "all locks", but it does release the lock associated with the object on which wait is invoked.

How do wait () notify () and notifyAll () work?

The wait() method causes the current thread to wait until another thread invokes the notify() or notifyAll() methods for that object. The notify() method wakes up a single thread that is waiting on that object's monitor. The notifyAll() method wakes up all threads that are waiting on that object's monitor.

Why wait () notify () and notifyAll are in object class?

If wait() and notify() were on the Thread instead then each thread would have to know the status of every other thread and there is no way to know thread1 that thread2 was waiting for any resource to access. Hence, notify, wait, notifyAll methods are defined in object class in Java.


2 Answers

When you call wait() from a thread, that thread stop executing and it's added to the waitset of the object. When you call notify() from another thread, a random thread from the waitset is waked up, if you call notifyAll() all would be ready to execute.

When you call notify(), the thread is ready to run but it doesnt mean it will be executed inmediately so be careful.

  1. It would wake up a thread from the waitset randomly.

  2. Youd don't know which one will be waked up first, it doesn't follow any order.

  3. Thread.getState()

  4. You would produce deadlock.

like image 124
sui Avatar answered Sep 20 '22 16:09

sui


  1. notify will wake one thread waiting on the monitor. Unless and until the monitor is unowned, no thread waiting can run; wait() must be called in a synchronized block and so the lock must be held to continue running that block.
  2. No guarantees. Call notifyAll to give all threads a chance to wake.
  3. Dunno. You could have the thread set a variable saying it's waiting before it goes to sleep...
  4. This is probably a bad idea. Can you come up with a situation where this is necessary?
like image 21
Borealid Avatar answered Sep 22 '22 16:09

Borealid