Does notify/notifyall release the lock being held

前端 未结 8 1453
忘掉有多难
忘掉有多难 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:46

    No -- notify/notifyAll don't release locks like wait does. The awakened thread can't run until the code which called notify releases its lock.

    This is what the Javadoc says:

    The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

提交回复
热议问题