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

How to View binary Operation from Thread Pool State Management

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "how to look at binary operations from the perspective of thread pool state management". Many people will encounter this dilemma in the operation of actual cases. next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Thread pool status

First of all, for the sake of the completeness of the article, let's take a look at the state of the thread pool, which can be summarized as follows:

There are five types of thread pool states: RUNNING, SHUTDOWN, STOP, TIDYING, TERMINATED

The meaning of state representation

RUNNING: (running) receives the new task and processes the queued task without interrupting the tasks in progress

SHUTDOWN: (close) do not accept new task, only deal with queued task, and do not interrupt ongoing tasks

STOP: (stop) do not accept the new task, do not process the queued task, and interrupt the tasks in progress

TIDYING: (collate) all task have been terminated, the number of active threads in workCount mentioned above is 0, and the terminated () hook method for interrupted tasks and queued tasks to execute the current task.

TERMINATED: (terminated) identifies the end of the above TIDYING process and identifies the status of the current thread pool successfully and completely stopped

State transition

The general process is as follows:

RUNNING-- > SHUTDOWN-- > STOP-- > TIDYING-- > TERMINATED

The above process is in an one-way order, that is, there will be no such situation as STOP-> SHUTDOWN.

In addition, not every state has to go through.

When does the state transition of the thread pool occur?

RUNNING-> SHUTDOWN: after calling the method shutdown () that terminates the thread

RUNNING or SHUTDOWN-> STOP: after calling the shutdownNow () method, whether it is in the RUNNING state or the SHUTDOWN state, it is directly converted to the STOP state.

In the SHUTDOWN-> TIDYING:SHUTDOWN state, the state changes to TIDYING when both the waiting queue and the task in progress are empty.

In STOP-> TIDYING:STOP state, when all the tasks in progress are interrupted, the state changes to TIDYING.

In the TIDYING-> TERMINATED:TIDYING state, when all the terminated () hook methods are executed, the state changes to TERMINATED and the thread pool is closed!

Manage thread pool status

The state of the thread pool and the current number of active threads in the thread pool are managed through an AtomicInteger variable.

What? One variable manages two states that are so unrelated? That's right

Who is the CTL variable?

Let's take a look at the thread pool implementation for this part:

Private final AtomicInteger ctl = new AtomicInteger (ctlOf (RUNNING, 0)); private static final int COUNT_BITS = Integer.SIZE-3; private static final int CAPACITY = (1

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