Java - alternative to thread.sleep

前端 未结 8 683
礼貌的吻别
礼貌的吻别 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

    The solution is to use a handler with a runnable and use of the method 'postDelayed'. Example:

    new Handler().postDelayed(new Runnable() {
    public void run () {
        // Do delayed stuff!
    }
    }, 5000L); //5 seconds delay 
    

    https://stackoverflow.com/a/21680858

提交回复
热议问题