java sleep for 1 second code example
Example 1: java sleep in code
import java.util.concurrent.TimeUnit
TimeUnit.SECONDS.sleep(1);
or
TimeUnit.MINUTES.sleep(1);
Example 2: java delay
Syntax :
=========================================================
Thread.sleep(1000);
=========================================================
int i=0;
for(;;){
Thread.sleep(2000);
System.out.println(i++);
}
Example 3: 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 4: sleep() java
import java.util.concurrent.TimeUnit
try {
TimeUnit.SECONDS.sleep(1);
}
catch (Exception e) {
System.out.println("Oops! Something went wrong!")
}