In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the workflow of the thread pool". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the workflow of the thread pool"?
Various parameters of thread pool
The most common question during the interview is the meaning of the various parameters of the thread pool and the entire running process of the thread pool.
ThreadPoolExecutor has a total of four constructors, but the last one to call is the following
Parameter meaning corePoolSize core thread pool size maximumPoolSize thread pool maximum capacity size keepAliveTime thread pool idle, thread survival time TimeUnit thread activity hold time unit BlockingQueue task queue, blocking queue for tasks waiting to be executed ThreadFactory is used to set the thread's factory RejectedExecutionHandler saturation policy
To learn these parameters by analogy, we compare the thread pool to the project team, and the thread is a member of the company.
CorePoolSize: the minimum number of threads in the thread pool, a project team must have corePoolSize people to hold their ground, are signed labor contracts, can not be withdrawn casually.
MaximumPoolSize: when the project is busy, you have to add people and ask people from other project teams to help. But the company's space is limited and can only be added to maximumPoolSize individuals at most. When the project is idle, you will have to withdraw, at most, to corePoolSize individuals.
KeepAliveTime & unit: it is mentioned above that projects increase or decrease people according to their availability, so in the programming world, how do you define busy and idle? If a thread does not execute a task within the keepAliveTime (time number) * unit (time unit) time, the thread is idle. If the number of threads is greater than corePoolSize at this time, the thread will be reclaimed
WorkQueue: the task queue
ThreadFactory: customize if you create a thread, such as assigning a meaningful name to the thread
What should I do when the handler:workQueue is full (the schedule is full) and then submit the task? This is the processing strategy. The thread pool provides four strategies. You can also implement the RejectedExecutionHandler interface to customize the policy.
Class policy AbortPolicy discards tasks, discards runtime exceptions (default processing policy) CallerRunsPolicy executes tasks DiscardPolicy ignores, nothing happens DiscardOldestPolicy discards the most recent task in the queue and executes the current task
Workflow of thread pool
You can refer to the source code to understand the following process
1. When the thread pool was first created, there was no thread in it. The task queue is passed in as a parameter. However, even if there are tasks in the queue, the thread pool will not execute them immediately.
two。 When the execute () method is called to add a task, the thread pool makes the following judgment:
a. If the number of running threads is less than corePoolSize, create a thread to run the task immediately
b. If the number of running threads is greater than or equal to corePoolSize, put this task in the queue
c. If the queue is full and the number of running threads is less than maximunPoolSize, create a non-core thread to run the task immediately
d. If the queue is full and the number of running threads is greater than or equal to maximunPoolSize, the thread pool throws a RejectedExecutionException
3. When a thread finishes a task, it takes the next task from the queue to execute
4. When a thread has nothing to do for more than a certain amount of time (keepAliveTime), the thread pool determines that if the number of threads currently running is greater than corePoolSize, then the thread is stopped. So when all the tasks of the thread pool are completed, it will eventually shrink to the size of corePoolSize
The following figure can be used to represent the overall process
At this point, I believe you have a deeper understanding of "what is the workflow of the thread pool". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.