How to differentiate when wait(long timeout) exit for notify or timeout?

前端 未结 7 558
被撕碎了的回忆
被撕碎了的回忆 2020-12-15 02:58

Having this wait declaration:

public final native void wait(long timeout) throws InterruptedException;

It could exit by InterruptedExceptio

相关标签:
7条回答
  • 2020-12-15 03:50

    There is one more reason that notify can return: spurious wakeup. This is an unlikely but possible thing, because preventing spurious wakeups is very expensive on some hardware/OS combinations.

    Because of this you always have to call wait() in a loop and re-check the condition that you are waiting for. During this work it's easy to check for timeout at the same time.

    For details I recommend the book "Java Concurrency In Practice". And using higher level constructs that will get this all correct for you.

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