Delay in Java

Asked

Viewed 1,078 times

4

Is it possible to delay in Java? Like for example, I have two messages and I want the second one to appear just a few seconds later,?

  • Beyond the Thread.sleep() it is possible to use the enum java.uti.concurrent.TimeUnit, it increases the readability of the code, for example a delay of 5 minutes with Thread.sleep() stays Thread.sleep(300000), already with TimeUnit stays TimeUnit.MINUTES.sleep(5)

1 answer

3


Try using Thread.Sleep();

package com.journaldev.threads;

public class ThreadSleep {

    public static void main(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();
        // pausa de 2 segundos
        Thread.sleep(2000);
        System.out.println("Sleep time in ms = "+(System.currentTimeMillis()-start));

    }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.