Home > Java > javaTutorial > body text

How to close Java thread pool

王林
Release: 2023-05-09 11:58:15
forward
2945 people have browsed it

1, shutdown() method is to safely shut down the thread pool. After calling the shutdown method, it does not close the thread pool immediately, but executes many tasks in the thread pool, or waits for tasks to be executed in the queue. Wait for all tasks to be completed and then close the thread pool

2, isShutdown() method can determine whether the thread pool has started to close, but cannot determine whether it has been completely closed.

3. isterminated() method can determine whether the thread pool is completely closed. Therefore, after calling shutdown, when executing a task, calling isshutdown returns true, and calling isterminated returns false.

awaitterminationThe method is to determine whether the thread pool is completely closed, which is similar to isterminated, but accepts the waiting time. The following situations may occur when calling this method

(1) During the waiting period (including entering the waiting state) the thread pool is closed and all submitted tasks (including those in execution and waiting in the queue) are completed, which is equivalent to the end of the thread pool. The method returns true

(2) After waiting for timeout, the initial thread pool does not end. The method returns false

(3) The thread is interrupted during waiting, and the method will throw an Internet exception.

shutdownNow: Immediately shut down the thread pool, first send an interrupt signal to the thread in the thread pool, try to interrupt the thread, and then return the tasks waiting in the queue to the caller, who can remedy these tasks.

Therefore, we can choose the appropriate method to stop the thread pool according to our business needs. For example, you can usually use the shutdown() method to shut down and complete the submitted tasks, but if the situation is urgent, you can use the shutdownnow method to speed up the end of the thread pool.

The above is the detailed content of How to close Java thread pool. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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