Sleep and stop thread execution for a while in JAVA

Sleep and stop thread execution for a while in JAVA

The Sleep method of Thread class is used to stop code execution for a specified number of milliseconds.

public class ThreadSleep {

    public static void main(String args[]) {
        long d = System.currentTimeMillis();
        try {
            // Sleep main thread for 2 seconds
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(System.currentTimeMillis()-d);
        // Ouput: 2000
    }

}