How to make java wait

Hi,
I am new to java. I wanted to ask if there is a way to make java wait for a few seconds before printing something in the terminal window.

Thanks.

Comments

  • : Hi,
    : I am new to java. I wanted to ask if there is a way to make
    : java wait for a few seconds before printing something in the
    : terminal window.
    :
    : Thanks.

    You can use the Thread.sleep() method for that. To get the current thread use Thread.currentThread(). For more info see: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html
  • Another without Treads. It waits the n seconds you want

    [code]
    public static void wait (int n){
    long t0,t1;
    t0=System.currentTimeMillis();
    do{
    t1=System.currentTimeMillis();
    }
    while (t1-t0<1000);
    }
    [/code]
  • This post has been deleted.
  • : Another without Treads. It waits the n seconds you want
    :
    : [code]:
    : public static void wait (int n){
    : long t0,t1;
    : t0=System.currentTimeMillis();
    : do{
    : t1=System.currentTimeMillis();
    : }
    : while (t1-t0<1000);
    : }
    : [/code]:
    :


    I don't see how this code causes a wait of 'n' seconds. Its value is passed in but never used. Plus, unless I'm missing something, it's only waiting 1 second.

    Michael K. Craghead
    "Nunca Pescao, Siempre Tiburon"
  • change the (1000) in the code with n. That should make the method waits for n milliseconds. This is how the code should look like:
    [code] public static void waiting (int n){

    long t0, t1;

    t0 = System.currentTimeMillis();

    do{
    t1 = System.currentTimeMillis();
    }
    while (t1 - t0 < n);
    }[/code]
  • : change the (1000) in the code with n. That should make the method
    : waits for n milliseconds. This is how the code should look like:
    : [code]: public static void waiting (int n){
    :
    : long t0, t1;
    :
    : t0 = System.currentTimeMillis();
    :
    : do{
    : t1 = System.currentTimeMillis();
    : }
    : while (t1 - t0 < n);
    : }[/code]:
    :


    It's supposed to wait n seconds, not milliseconds. I think this is the correct answer:

    [code]: public static void waiting (int n){

    long t0, t1;

    t0 = System.currentTimeMillis();

    do{
    t1 = System.currentTimeMillis();
    }
    while ((t1 - t0) < (n * 1000));
    }[/code]:

    Michael K. Craghead


  • This post has been deleted.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories