-1

There is code like :

System.out.println("I wanna print next statement after 5 seconds");
// 5 seconds later
System.out.println("Time to show !");

How could I realize that? please give me an answer. ( // 5 seconcds later)

ton1
  • 7,238
  • 18
  • 71
  • 126

3 Answers3

0

Try this one.

try {
    Thread.sleep(5000);    
}catch (InterruptedException ex){
    ex.printStackTrace();
}

5000 milliseconds = 5 seconds
Thread.sleep is a static method telling the
current thread to sleep for N milliseconds.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
0

Try this:

 try {
   Thread.sleep( 5000 );
 } catch ( InterruptedException ex) { }
0

Thread.sleep(numberOfMilliseconds) is the way to make the current thread wait for a given amount of time. If you need to fire off something that should be done in 5 seconds, but don't want to stall the entire application, look into TimerTask.

MattPutnam
  • 2,927
  • 2
  • 17
  • 23