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
發佈了1 篇原創文章 · 獲贊 4 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章