Java Delay/Wait

108,829

Solution 1

Thread.sleep(1000); // do nothing for 1000 miliseconds (1 second)

Solution 2

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
}

Solution 3

My simple ways to delay a loop.

I already put the codes here after failing to follow the stackoverflow's standards.

//1st way: Thread.sleep : Less efficient compared to 2nd
try {
  while (true) {//Or any Loops
   //Do Something
   Thread.sleep(sleeptime);//Sample: Thread.sleep(1000); 1 second sleep
  }
 } catch (InterruptedException ex) {
   //SomeFishCatching
 }
//================================== Thread.sleep


//2nd way: Object lock waiting = Most efficient due to Object level Sync.
Object obj = new Object();
 try {
  synchronized (obj) {
   while (true) {//Or any Loops
   //Do Something
   obj.wait(sleeptime);//Sample obj.wait(1000); 1 second sleep
   }
  }
 } catch (InterruptedException ex) {
   //SomeFishCatching
 }
//=============================== Object lock waiting

//3rd way:  Loop waiting = less efficient but most accurate than the two.
long expectedtime = System.currentTimeMillis();
while (true) {//Or any Loops
   while(System.currentTimeMillis() < expectedtime){
     //Empty Loop   
   }
   expectedtime += sleeptime;//Sample expectedtime += 1000; 1 second sleep
   //Do Something
}
//===================================== Loop waiting

Solution 4

As Jigar has indicated you can use another Thread to do work which can operate, sleep etc independently of other Threads. The java.util.Timer class might help you as well since it can perform periodic tasks for you without you having to get into multithreaded programming.

Share:
108,829

Related videos on Youtube

Gray Adams
Author by

Gray Adams

I am a 15 year old developer and designer.

Updated on May 03, 2022

Comments

  • Gray Adams
    Gray Adams almost 2 years

    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).

    • jmj
      jmj over 12 years
      without slowing down the entire code elaborate this
    • Gray Adams
      Gray Adams over 12 years
      @JigarJoshi I'm trying to delay this while loop at 1 second intervals for my Minecraft plugin. But most of the wait methods pause the entire server for the set time, rather than just the loop
    • Dead Programmer
      Dead Programmer over 12 years
      you want the execution of while loop for a fixed period of time and then again in fixed delay.
  • Gray Adams
    Gray Adams over 12 years
    Won't this pause the whole script/computer?
  • jmj
    jmj over 12 years
    This would just pause that particular thread in which this code is running. your computer won't get freezed
  • Tom
    Tom over 8 years
    I like the third way, because it can inhibit a whole cpu core and no one would ever call this a code smell.
  • Adam Howell
    Adam Howell almost 5 years
    Another advantage of the third method is it does not require additional try/catch blocks.