Hi, my name is Mic, a Java programmer who has been working for 14 years.
Recently, many friends have privately messaged me and let me talk about some thread pool related issues.
There are quite a lot of points to examine in this direction of thread pool, if you just rely on brushing the interview questions
Interviewers are easy to identify, I'll just name a few.
- How does the thread pool implement thread recycling?
- Whether the core thread can be recycled
- What happens when the shutdown method of the thread pool is called?
The interview must be a series of questions to determine the applicant's understanding of the field.
Regarding the questions related to thread pool recycling, I have compiled the answers of the experts into a 20W interview document, you can privately message me to receive it.
Check out the experts' answers below
Expert:
First, the thread pool is divided into core threads and non-core threads.
A core thread is a worker thread that resides in the thread pool. It is initialized in two ways.
- When adding tasks to the thread pool, passive initialization
- Active call
prestartAllCoreThreads
method
When the queue in the thread pool is full, in order to increase the task processing capacity of the thread pool.
Thread pools increase non-core threads.
The number of core threads and non-core threads is set when the thread pool is constructed, and can also be changed dynamically.
Since non-core threads are temporarily added to solve the problem of too many tasks, when the task processing is completed, when the worker thread is in an idle state, it needs to be recycled.
Because all worker threads obtain tasks to be executed from the blocking queue, as long as the blocking queue does not have any tasks that can be processed within a certain period of time, the thread can end.
This function is accomplished by blocking the poll method in the queue. This method provides two parameters: timeout time and timeout time unit. When the task is not obtained after the specified time, the poll method returns null, thereby terminating the current thread to complete thread recycling.
By default, the thread pool will only recycle non-core threads. If you want to recycle core threads, you can set the allowCoreThreadTimeOut property to true. Under normal circumstances, we will not recycle core threads.
Because the thread pool itself realizes the reuse of threads, and these core threads are blocked when there are no tasks to be processed and do not occupy CPU resources.
Summarize
Regarding the thread pool, it is something that every Java programmer must master in depth.
It is very important that threads are everywhere in our application system.
Including application development, it is inevitable to use thread pools.
Mastering it enables you to write more robust and stable programs.
Everyone remember to like, favorite and follow
Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Please indicate the source forMic带你学架构
!
If this article is helpful to you, please help to follow and like, your persistence is the driving force for my continuous creation. Welcome to follow the WeChat public account of the same name to get more technical dry goods!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。