The thread pool is an advanced technology used to manage threads. Generally, it provides the following functions:
- Through thread management, more reasonable resource allocation. Generally, a group of Idle threads are maintained in the thread pool and provided externally. The number of Idle threads is increased or decreased dynamically based on the degree of System Busy. More advanced features are also provided to automatically detect abnormal threads.
- By maintaining the existing thread in the pool, you can save the thread creation overhead, especially for Web server, which is frequently processed and the processing process is fast.ProgramThe thread creation overhead cannot be ignored.
It is similar to adding an OS to a bare metal instance to reasonably manage computer resources. Using a thread pool can effectively manage thread lifecycles and system resources at a low cost, and provide more convenient and easy-to-use interfaces for external use, this makes development faster and safer.
For a simple thread pool, referArticleJava Theory and Practice: thread pool and work queue
Here, we implement a simple thread pool with a working Queue (Queue). Of course, there are some simple scheduling tasks that will not be described here.
Thread Pool framework
Compared with the complex scheduling and planning in threadpoolexecutor in jdk1.5, the above example is a little simpler. Doug Lee defines the command mode interface executor, which separates the thread execution logic from the time series logic, improving robustness and scalability, so that when the demand changes, it is no longer to rewrite the entire program, but only to modify the corresponding execution logic-As for the time series logic, hand it over to threadpool, it will help you deal with everything. Don't believe it? Let's look at how it achieves this magical function with inside threadpoolexecutor.
First, we have a general understanding of the threadpoolexecutor framework. threadpoolexecutor inheritsAbstractexecutorservice, which implements the executorservice interface, and executorservice is the sub-interface of executor; that is to say, threadpoolexecutor is actually an executor implementation. .
Threadpoolexecutor has five internal classes. The rejectexecutionhandler defines the discard method for the task, which has four instance classes. The worker class is actually the thread entity executed in the thread pool, so it is also very important to note that it is defined as an internal class, so that you can easily access the threadpoolexecutor class method.
Here we will briefly introduce these interfaces or classes related to threadpoolexecutor. Executor has already introduced the interface for logical separation, which defines the abstract method execute (). Executorservice is a service interface that is responsible for managing a group of tasks. Some of its implementations are already in its subclass.Abstractexecutorservice is complete. Threadpoolexecutor must implement abstract methods such as execute () and Shutdown.
ThreadpoolexecutorCodeAnalysis
Execute () is the core method of the entire thread pool. It can be said that the thread pool schedules tasks here. Let's look at the for loop. If the current thread pool is running, the reject policy is used to drop the task reject. If the current thread count is smaller than the number of core pools, the new thread is used to solve the current task, note that even the active threads are idle, they are not used. Once the number of threads exceeds the number of core pools, new threads are not added, but tasks are added to the queue, if the number of threads in the core pool cannot meet the number of tasks, in this case, the backup thread is enabled. There are three possible results returned by addifundermaximumpoolsize (command): 1. create a thread to process the current task. 2. not created successfully; 3. the new thread processes the tasks in the queue but does not process the current task. In the first two cases, if is used for judgment. Only the final case is that the for loop must be re-processed.
Public void execute (runnable command ){
If (command = NULL)
Throw new nullpointerexception ();
For (;;){
If (runstate! = Running ){
Reject (command );
Return;
}
If (poolsize <corepoolsize & addifundercorepoolsize (command ))
Return;
If (workqueue. Offer (command ))
Return;
Runnable r = addifundermaximumpoolsize (command );
If (r = command)
Return;
If (r = NULL ){
Reject (command );
Return;
}
// Else retry
}
} |
More, see http://www.360watcher.net/html/35/656.htm