How to make java wait - Programmers Heaven

Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How to make java wait

java4lifejava4life Posts: 1Member
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

  • zibadianzibadian Posts: 6,349Member
    : 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
  • joangerardjoangerard Posts: 1Member
    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]
  • abhiwavesabhiwaves Posts: 6Member
    This post has been deleted.
  • mcragheadmcraghead Posts: 2Member
    : 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"
  • Faisal_903Faisal_903 Posts: 3Member
    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]
  • mcragheadmcraghead Posts: 2Member
    : 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


  • devuniondevunion Posts: 9Member
    This post has been deleted.
Sign In or Register to comment.