Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the states of java thread pool and an example analysis of state transition

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "what is the state of java thread pool and the analysis of state transition examples". In daily operation, I believe many people have doubts about what the state of java thread pool is and the analysis of state transition examples. The editor consulted all kinds of materials and sorted out simple and useful operation methods, hoping to help you answer the doubts about "what is the state of java thread pool and the analysis of state transition examples?" Next, please follow the editor to study!

Foreword:

In Java, the state of the thread pool is completely different from the state of the thread

Threads have six states:

NEW: initialization status,

RUNNABLE: runnable / runnable,

BLOCKED: blocking state,

WAITING: unlimited wait statu

TIMED_WAITING: time-bound wait status and

TERMINATED: termination status.

The thread pool has the following five states:

RUNNING: running state, which is entered after the thread pool is created, and if the close method is not manually called, the thread pool remains in this state for the entire program run.

SHUTDOWN: closed state, no longer accept new task submissions, but will finish processing tasks that have been saved in the task queue.

STOP: stops, no longer accepts new task submissions, interrupts currently executing tasks, and abandons existing tasks in the task queue.

TIDYING: collates the state when the number of active threads in the current thread pool drops to 0 after all tasks have been executed (including those in the task queue). After reaching this state, the thread pool's terminated () method is called.

TERMINATED: destroys the state, which changes to this state after the thread pool's terminated () method is executed.

These five states can be found in the ThreadPoolExecutor source code, as shown in the following figure:

Thread pool state transition

There are two paths to the state transition of a thread pool:

When the shutdown () method is called, the state of the thread pool goes from RUNNING to SHUTDOWN, to TIDYING, and finally to TERMENATED destruction.

When the shutdownNow () method is called, the state of the thread pool goes from RUNNING to STOP, to TIDYING, and finally to TERMENATED destruction.

The process of thread state transition is shown in the following figure:

Terminated method

The terminated () method in the thread pool, which is called when the thread pool transitions from TIDYING to TERMINATED state, is empty by default

Its source code is as follows:

We can override the terminated () method when creating the thread pool, as shown in the following code:

Import java.util.concurrent.LinkedBlockingQueue;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;public class ThreadPoolStateTransition {public static void main (String [] args) throws InterruptedException {/ / create thread pool ThreadPoolExecutor threadPool = new ThreadPoolExecutor (10,10,0L, TimeUnit.SECONDS, new LinkedBlockingQueue (100)) {@ Override protected void terminated () {super.terminated () System.out.println ("execute terminated () method");}}; / / close thread pool threadPool.shutdown (); / / wait for thread pool to finish execution before exiting while (! threadPool.awaitTermination (1, TimeUnit.SECONDS)) {System.out.println ("thread pool is running") At this point, the study on "what is the status of java thread pool and the instance analysis of state transition" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report