Does notify/notifyall release the lock being held

前端 未结 8 1436
忘掉有多难
忘掉有多难 2020-12-09 02:41

I am confused a bit about wait and notify/notifyAll.

I know there is a lock for every java object. I know wait will release the lock for other thread. How about noti

8条回答
  •  一整个雨季
    2020-12-09 02:52

    wait(): Virtually every object in Java posses a monitor, to enter inside any synchronized block a thread has to first acquire this monitor and then only he can enter this synchronized block. As critical section of the code is executed by a single thread at a time so it has a great impact on overall performance of the application. So in place of holding the resource(monitor) threads can be asked to leave the critical section and wait for some time. To achieve this behaviour Java has provided a wait() api directly in Object class.

    So any time a thread encounter a wait() API, it drops the current monitor plus all other monitors it holds and go to waiting state linked the current object. Its important to understand that that went to the waiting state in the context of object for which thread acquired the monitor first.. In a conceptual way i explain, every object holds a container house where all waiting threads are held. There are multiple ways a Thread can come out of this container house of Object. Lets see..

    • When another thread comes and rings the bell once, In Java by calling notify() method on the same object.
    • When another thread comes and rings multiple time, one of the Thread gets a chance to come out of Object’s container house. In Java we can do this by calling notifyAll() on the same object.
    • If we have the reference of the Thread waiting in container house. Calling interrupt() on the Thread object pops it out of waiting state and brings it the exception block of the Object.
    • There are overloaded wait(long milliseconds) and wait(long millSec, int nanos) methods. As the time over the Thread is eligible to come out of waiting state and contest for Object monitor again. In case Thread fails to acquire the monitor after timeout also then it has to wait for notify() calls only.

    notify(): If the objects container house has multiple threads in waiting state then calling notify() on the this object given chance to one the thread to proceed. But after coming out of the waiting state the thread still has to contest for the object monitor and if it succeeds in getting the monitor it continue its execution otherwise thread will come back to waiting state. So notify() also has to be called from the synchronized block. If the notify() is not called from synchronized context then it thorws IllegalMonitorStateException.

    notifyAll(): Calling notifyAll() on the Object makes sure all the threads in Object container house are awakened but once awakened they have to compete with each other or any other thread wants to acquire the object monitor. Which ever thread succeeds continue its executions others have to go back to waiting state and settle in object container house. As notify(), notifyAll() should also be called in synchronized context.

    Explanation Taken from http://coder2design.com/thread-communication/

提交回复
热议问题