Java Delay/Wait

后端 未结 4 945
悲&欢浪女
悲&欢浪女 2020-12-10 01:20

How do I delay a while loop to 1 second intervals without slowing down the entire code / computer it\'s running on to the one second delay (just the one little loop).

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-10 02:03

    It seems your loop runs on Main thread and if you do sleep on that thread it will pause the app (since there is only one thread which has been paused), to overcome this you can put this code in new Thread that runs parallely

    try{
    
      Thread.sleep(1000);
    }catch(InterruptedException ex){
      //do stuff
    }
    

提交回复
热议问题