Java – How to delay few seconds

In Java, you can use Thread.sleep(miliseconds) to make the current executing Java program to sleep or delay few seconds.

TestSleep.java

package com.mkyong.test;

import java.util.Date;

public class TestSleep {

	public static void main(String[] args) {
		
		System.out.println("Testing..." + new Date());
		
		try {
		
			//sleep 5 seconds
			Thread.sleep(5000);
			
			System.out.println("Testing..." + new Date());
			
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		
	}

}

Output


Testing...Tue Sep 22 17:26:51 SGT 2015
Testing...Tue Sep 22 17:26:56 SGT 2015

References

  1. JDK 7 Thread.sleep

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
4 Comment threads
2 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
6 Comment authors
jayant kumarEster SmithbadboyBerndKuchi Recent comment authors
newest oldest most voted
Kuchi
Guest
Kuchi

How about:
TimeUnit.SECONDS.sleep(5);
It’s essentially the same but is nicer to read. (My opinion)

jayant kumar
Guest
jayant kumar

Thanks sir, This help me a lot to resolve my issue.

Stas Miroshnik
Guest
Stas Miroshnik

how to delay many seconds ? ))

badboy
Guest
badboy

5 sec

Bernd
Guest
Bernd

you are fat and gay

Ester Smith
Guest
Ester Smith

What?