java sleep seconds code example
Example 1: java delay
Syntax :
=========================================================
Thread.sleep(1000);
=========================================================
int i=0;
for(;;){
Thread.sleep(2000);
System.out.println(i++);
}
Example 2: java pause 1 second
try {
Thread.sleep(1000);
} catch (InterruptedException e) {e.printStackTrace();}
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
}
}, 1000);
Example 3: sleep() java
import java.util.concurrent.TimeUnit
try {
TimeUnit.SECONDS.sleep(1);
}
catch (Exception e) {
System.out.println("Oops! Something went wrong!")
}