Next, in the last article introduced the thread pool design requirements and use, here we analyze the concurrent package thread pool implementation class Threadpoolexecutor design, is not feel meaningless, people have realized also analysis what? Of course is to improve our own design capabilities. For the improvement of the design ability, I think we should be able to learn from the predecessors in the concrete practice.
Summary of design ideas (such as design patterns, etc.), according to the specific context, can be used to integrate them. On the other hand, the analysis of excellent framework design and implementation is also a good way, of course, JDK design is the best learning materials. More gossip, cut into the theme, Design points:
Looking at the thread pool we implemented ourselves before JDK1.5, or the thread pool provided by concurrent, we first want two containers to maintain the threads in the thread pool and the tasks that are committed to the thread pool.
How a thread pool is associated with a task, and what task can be executed in that thread pool. So we're going to define an interface that separates the thread pool from the concrete
Task coupling, Threadpoolexecutor can accept specific tasks that implement runnable interfaces or callable interfaces (which in fact are also assembled as runnable interfaces).
Threads in the thread pool are executed from the task queue.
These are the main points of thread pool design.
In Threadpoolexecutor, there is an internal class worker, which implements the runnable, the thread in the thread pool, and it goes without saying that its run method is from the task queue
Take a task, call the task's Run method (the task implements the Runnable interface), execute the task, and so on, until there are no tasks in the queue. Look at the picture.