wait method wakes up without calling notify(NOT SPURIOUS WAKEUP)

后端 未结 1 1142
感动是毒
感动是毒 2021-01-19 18:49

In the statement below, the wait() method is executed even though no notify is called but the statement below wait() are executing only after

相关标签:
1条回答
  • 2021-01-19 19:16

    You don't need to postulate a spurious wakeup to explain what's going on here. When laurel terminates it sends a notifyAll to the threads that are waiting on it. (This is how Thread.join works.)

    See the api doc for Thread#join:

    This implementation uses a loop of this.wait calls conditioned on this.isAlive. As a thread terminates the this.notifyAll method is invoked. It is recommended that applications not use wait, notify, or notifyAll on Thread instances.

    Also, always wait in a loop, using a condition; see the Oracle concurrency tutorial, especially the Guarded Blocks page. (From the description you can see join waits in a loop where the tested condition is isAlive on the thread joined to, so it is s good example. You can find the join method in the jdk source for the Thread class.)

    0 讨论(0)
提交回复
热议问题