Forcing a spurious-wake up in Java

后端 未结 2 649
别跟我提以往
别跟我提以往 2021-02-11 19:03

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

2条回答
  •  梦毁少年i
    2021-02-11 19:52

    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

提交回复
热议问题