Sibi
0
Q:

java delay

Syntax :
=========================================================
	Thread.sleep(1000); // 1000 milliseconds..          |
=========================================================
int i=0;
for(;;){
	Thread.sleep(2000); // set time delay to 2 seconds.. 
  	System.out.println(i++); // output : every output will display after 2 seconds..   	
}
5
//deprecated in main thread:
try {
  Thread.sleep(1000);//time is in ms (1000 ms = 1 second)
} catch (InterruptedException e) {e.printStackTrace();}

//please use bellow instead:
Timer timer = new Timer();
timer.schedule(new TimerTask() {
  @Override
  public void run() {
    //what you want to do
  }
}, 1000);//wait 1000ms before doing the action
2
System.out.println("Hi");
//Now it will pause
try{
  Thread.sleep(5000)//Time in milliseconds->5000==5 seconds 
  //^freezes whole program for amount of time
  System.out.println("Hola");//Appears after 5 seconds
}
catch(InterruptedException e){
  System.err.println(e.getMessage());
}
1

New to Communities?

Join the community