What is the interview question?
Let a spiritual guy who has worked for 4 years just go to a technical interview,
Got demoralized. Depressed!
What is behind all this is the loss of morality or the distortion of human nature.
Let's demystify this interview question together.
Regarding, "Briefly describe your understanding of thread pools", look at the answers of ordinary people and experts.
Ordinary people:
Um. . . . . . . . . .
Expert:
I will answer this question in several ways.
First of all, the thread pool is essentially a pooling technology, and the pooling technology is an idea of resource reuse. The more common ones are connection pools, memory pools, and object pools.
The thread pool reuses thread resources. I think there are two core design goals:
- Reduce the performance overhead caused by the frequent creation and destruction of threads, because thread creation will involve CPU context switching, memory allocation, and other work.
The thread pool itself has parameters to control the number of threads created, so as to avoid the problem of excessive resource utilization caused by endlessly creating threads.
played a role in resource protection.
Secondly, I will briefly talk about the thread reuse technology in the thread pool. Because the thread itself is not a controlled technology, that is to say, the life cycle of the thread is determined by the running state of the task and cannot be controlled by humans.
Therefore, in order to realize the reuse of threads, the blocking queue is used in the thread pool. In short, the worker thread in the thread pool is always running, and it will obtain the tasks to be executed from the blocking queue. Once the queue is empty, then This worker thread will be blocked until the next time a new task comes in.
That is to say, the worker thread realizes blocking and waking up according to the situation of the task, so as to achieve the purpose of thread reuse.
Finally, the resource limit in the thread pool is controlled by several key parameters, namely the number of core threads and the maximum number of threads.
The number of core threads represents the default long-lived worker threads, and the maximum number of threads is dynamically created according to the situation of the task, mainly to improve the efficiency of tasks in the blocking queue.
processing efficiency.
The above is my understanding of the problem!
Summarize
When I was reading the source code of the thread pool, I was amazed by the various design ideas in it.
For example, the idea of dynamic expansion and shrinkage, the idea of thread reuse, and the method of thread recycling, etc.
I find that the simpler things are, the less simple they are.
For more interview materials and interview skills, you can privately message me.
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) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。