Forcing a spurious-wake up in Java

后端 未结 1 1540
醉梦人生
醉梦人生 2021-02-11 19:32

This question is not about, whether spurious wakeups actually happen, because this was already discussed in full length here: Do spurious wakeups in Java actually happen? Theref

1条回答
  •  终归单人心
    2021-02-11 20:00

    You can't force a spurious wakeup, but to the running thread, a spurious wakeup is indistinguishable from a regular wakeup (the source of the event is different, but the event itself is the same)

    To simulate a spurious wakeup, simply call notify();

    Calling interrupt() isn't suitable, because doing so sets the interrupt flag, and after a spurious wakeup, the interrupt flag is not set

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