Java Thread.sleep() Guide

In Java, you can use the Thread.sleep() method to pause the current executing thread for a specified amount of time. The method takes a long parameter that represents the length of time the thread should sleep in milliseconds.

Here is a sample code demonstrating how to call the Thread.sleep() method:

public class SleepExample {
    public static void main(String[] args) {
        System.out.println("Start");
        
        try {
            // 使当前线程休眠3秒
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        System.out.println("End");
    }
}

In the example above, after calling the Thread.sleep(3000) method, the current thread will sleep for 3 seconds before continuing to execute the remaining code. It is important to handle the InterruptedException exception.

bannerAds