Waking up a sleeping thread - interrupt() versus “splitting” the sleep into multiple sleeps

前端 未结 4 657
温柔的废话
温柔的废话 2021-02-10 10:40

This requirement came up in my Android app, but it applies to Java in general. My app \"does something\" every few seconds. I have implemented this as follows (just relevant sni

4条回答
  •  一生所求
    2021-02-10 11:08

    IIRC, in Java you can object.wait() with a timeout. Is this not what you want? If you want to change the timeout from another thread, change some 'waitValue' variable and notify(). The thread will then 'immediately' run and then wait again with the new timeout value. No explicit sleep required.

提交回复
热议问题