Home > Java > javaTutorial > body text

Java uses the sleep() function of the Thread class to pause the execution of the thread

WBOY
Release: 2023-07-24 12:07:56
Original
1644 people have browsed it

Java uses the sleep() function of the Thread class to pause the execution of threads

In Java, threads are the basic unit of concurrent execution. In order to realize thread suspension and control, Java provides the sleep() method of the Thread class. The sleep() method allows the thread to pause for a period of time before continuing execution. This article will introduce how to use the sleep() method to pause the execution of a thread, with code examples.

The sleep() method of Thread class is a static method, which can be called directly through Thread class. The prototype of this method is as follows:

public static void sleep(long millis)

            throws InterruptedException
Copy after login

Among them, the millis parameter indicates the number of milliseconds to pause. When the sleep() method is called, the current thread will suspend execution, enter the blocking state, and automatically wake up to continue execution after the specified number of milliseconds. It should be noted that the sleep() method will throw InterruptedException, so exception handling is required when using this method.

The following is a simple example that demonstrates how to use the sleep() method to pause the execution of a thread:

public class SleepExample {

    public static void main(String[] args) {
        // 创建一个新线程
        Thread thread = new Thread(new MyRunnable());

        // 启动线程
        thread.start();

        try {
            // 主线程暂停500毫秒
            Thread.sleep(500);

            // 中断线程
            thread.interrupt();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    static class MyRunnable implements Runnable {

        @Override
        public void run() {
            try {
                // 线程执行任务
                System.out.println("线程开始执行任务");

                // 线程暂停1秒
                Thread.sleep(1000);

                // 继续执行任务
                System.out.println("线程继续执行任务");
            } catch (InterruptedException e) {
                // 线程被中断,抛出异常
                System.out.println("线程被中断,任务终止");
                return;
            }

            // 线程执行完成
            System.out.println("线程任务执行完成");
        }
    }
}
Copy after login

In the above example, a new thread is first created and started. Then, the main thread calls the sleep() method to pause execution for 500 milliseconds, and then interrupts the new thread. In the run() method of the new thread, the thread first outputs "the thread starts executing the task", then calls the sleep() method to pause execution for 1 second, and finally outputs "the thread continues executing the task". If the main thread interrupts the new thread before the new thread calls the sleep() method, the new thread will throw an InterruptedException and output "The thread was interrupted and the task was terminated".

By using the sleep() method of the Thread class, we can precisely control the suspension and resumption of threads to achieve inter-thread coordination and synchronization in multi-threaded programming.

The above is the detailed content of Java uses the sleep() function of the Thread class to pause the execution of the thread. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!