Computer Science Canada

How do I create a delay in java?

Author:  Heybob [ Mon Mar 20, 2006 10:23 am ]
Post subject:  How do I create a delay in java?

If I want to delay something in java, how would I do it? I know in turing there is a delay (*) but Im not sure what it is for java.

Author:  Tony [ Mon Mar 20, 2006 10:29 am ]
Post subject: 

I think you might be looking for sleep()

Author:  xtreemboarder [ Fri Apr 21, 2006 12:47 pm ]
Post subject: 

use this... put it in the same directory as your program... and in your program put "Delay.stop(int);" before whatever you want delayed.... just like in turing.

Author:  xHoly-Divinity [ Wed Apr 26, 2006 4:10 pm ]
Post subject: 

Our teacher taught us to use a useless for loop that runs like a million times Confused . I guess that works as well Twisted Evil

Author:  HellblazerX [ Wed Apr 26, 2006 8:37 pm ]
Post subject: 

errrr not really, cuz you're delaying by the number of times your CPU can go through that for loop, so the slower comp you have, the longer delay you'll have.

Author:  [Gandalf] [ Thu Apr 27, 2006 12:49 am ]
Post subject: 

xHoly-Divinity wrote:
Our teacher taught us to use a useless for loop that runs like a million times Confused . I guess that works as well Twisted Evil

This is far worse than using processes in Turing, and you know how bad that is. Unless, of course, you have a constant frame rate, but even then using the Thread.sleep() method is what you should be using.

Author:  codemage [ Thu Apr 27, 2006 8:48 am ]
Post subject: 

code:
public static void delay (int millisecs)
    {
        try
        {
            Thread.currentThread ().sleep (millisecs);
        }
        catch (InterruptedException e)
        {
        }
    }

Author:  [Gandalf] [ Fri Apr 28, 2006 2:08 am ]
Post subject: 

Note that you don't need to explicitly state that you're delaying currentThread(), Thread.sleep() should be sufficient, especially since you're probably only working with the default Thread.

Author:  Martin [ Fri Apr 28, 2006 3:49 am ]
Post subject: 

code:
void delay_a_bit() {
  for (int i=0;i<1000000;++i) {
    int x = Math.sqrt (i * i);
  }
}


: