Java: How can a thread wait on multiple objects?

后端 未结 8 2172
有刺的猬
有刺的猬 2020-12-30 23:46

A thread can use Object.wait() to block until another thread calls notify() or notifyAll() on that object.

But what if a threa

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-31 00:37

    A thread cannot wait on more than one object at a time.

    The wait() and notify() methods are object-specific. The wait() method suspends the current thread of execution, and tells the object to keep track of the suspended thread. The notify() method tells the object to wake up the suspended threads that it is currently keeping track of.

    Useful link : Can a thread call wait() on two locks at once in Java (6) ?

提交回复
热议问题