In java,
I want to delay the execution of thread for 5 sec
Is it possible to delay the execution of code or thread for certain time ?
If yes ,then please help me out....
public class Main {
public static void main(String[] args) {
System.out.println("Program Started...");
System.out.println("Current Thread name : " + Thread.currentThread().getName());
try {
System.out.println("Sleeping for 5 seconds");
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println("Thread is interrupted");
}
System.out.println("Code delay completed.");
System.out.println("Program Ended");
}
}
In Java, we can use
TimeUnit.SECONDS.sleep()
orThread.sleep()
to delay few seconds.1) TimeUnit :
Thread.sleep :