Java - alternative to thread.sleep

前端 未结 8 674
礼貌的吻别
礼貌的吻别 2020-11-30 10:00

I have a requirement to pause a while loop for a specific number of milliseconds. I have tried using Thread.sleep(duration) but it is not accurate, especially in a looping s

8条回答
  •  旧巷少年郎
    2020-11-30 10:24

    You could implement wait/notify mechanism and delegate to another thread the responsibility of notify the other thread in wait state that the amount of time is passed and that it can go ahead ...

    For example when the threadA need to wait for a certain amount of time you can put the thread in wait state and start a timer task that after a certain amount of time (interval ) call notify and wake up the ThreadA that go ahead, this could be an alternative .

提交回复
热议问题