delay in java 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: delay a function call in java
new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
}
},
5000
);