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

前端 未结 7 574
被撕碎了的回忆
被撕碎了的回忆 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:38

    There's no way to tell directly - that is, you would have to add additional code to determine this. Often when you wait(), you're waiting for something to happen which changes the state of an object in some way - e.g. by setting a boolean variable, perhaps. If that's the case, then you may be able to simply check the state of that variable to see if the event occurred, or you merely timed out. Or you can look at the value of System.currentTimeMillis() to see i the elapsed time is greater than or equal to the timeout period - if it is, that would be a clue you have probably timed out (though it's not an absolute guarantee). Or if the elapsed time is less than the timeout period then you certainly have not timed out. Does that help?

提交回复
热议问题