Benefits of using the thread pool:
1. Reduced resource consumption: reduced resource consumption for thread creation and destruction
2. Improve response speed, when the task arrives, the thread can not enjoy the creation of direct processing
3. Improve the manageability of threads. Threads can be used for unified management, monitoring, and use of thread pools.
Source code Analysis of thread pool:
public void Execute (Runnable command) {
if (command==null) {
throw new NullPointerException ();
}
Create a thread and perform a task if the number of execution threads is less than the base thread
if (poolsize>=corepoolsize| |! Addifundercoresize (command)) {
If the number of thread pools is greater than or equal to the number of cores or thread creation fails, directly into the queue
if (runstate==runnable&&workqueue.offer (command)) {
if (runstate!=runnable| | poolsize==0) {
eusurequeuetaskhandled (command);
}
}
Create a new thread if the thread pool is not running or cannot be placed in the task queue and the current thread is less than the maximum allowable thread
if (!addundermaxpoolsize (command)) {
Reject (command);
}
}
}
The worker thread pseudo-code is as follows:
public void runnable () {
Runnable Task=firsttask;
Firsttask=null;
while (task!=null| | Task=workqueue.get ()!=null) {
Runtask (Task);
Task=null;
}
finally{
Workdone (this);
}
}
Use of the thread pool
Create thread pool: New Threadpoolexecutor (Corepoolsize,maxpoolsize,keepalivetime,millseconds,runnabletaskqueue,hander);
Parameter description
Runnabletaskqueue, is a blocking queue for storing tasks, usually arryayblockingqueue linkedblockingqueue pritoryblockingqueue
Hander: Saturation strategy: 1 AbortPolicy Discard 2 discardpolicy do not discard 3 callerrunspolicy only the caller thread to execute, Discardoldestpolicy discard the most recent task in the queue and perform the current task
KeepAliveTime: When a worker thread in a thread pool is idle, it can remain alive for a valid time
Timeunit: Effective unit days, small hours, seconds, etc. that can maintain survival time
To submit a task to the thread pool: Execute and submit
Execute is used to submit a result without a return value
Submit for submitting a thread with a return value
A thread submitted through the Submit method returns an object of the future type, which can be used to determine whether the return value is successful. Through the future. The Get method gets the return value, the Get method blocks the current thread until the task completes, and the Get method can set the time to implement the asynchronous call back
Close the thread pool
Close Line Cheng Two methods: Shutdown Shutdownnow method to close the thread pool
Shutdown principle: Traversing the worker thread one by one, calling the worker thread's interrupt method, so the thread that cannot interrupt the response may not terminate.
Shutdownnow principle: First set the thread pool to the stop state, and then try to terminate all threads that are executing or pausing the task, and return to the list of tasks awaiting execution. As soon as any of the two methods are called, IsShutDown returns True when all tasks are closed to indicate that the thread pool is closed successfully isterminaled returning the True,shutdownnow method may not necessarily be done. Typically use shutdown to close
Reasonable allocation of thread pool
CPU-intensive tasks should be configured with as small a thread as possible. such as configuring Cup+1 threads. Because IO-intensive tasks do not always carry out tasks. So you should configure as many threads as possible, such as configuring 2*cup threads. If the task is mixed, you can split it. Split into a CPU-intensive task and an IO-intensive task, the throughput is higher than a single-threaded task if the time difference between the two tasks is not large, and it is not necessary to decompose if the difference is too large.
Runtime.getRuntime.getAvaliableProcessors Get Cup Quantity
Tasks with different priorities can be handled using the priority queue Pritorityblockqueue
It is recommended that you use a bounded queue because you can execute a deny policy when the exception task backlog is in time to alert
Monitoring of the thread pool
Taskcount: Number of tasks to be performed in the thread pool
Completaskcount: Number of threads that the thread pool has completed
Largestpoolsize: The maximum number of threads that have ever been created in a thread pool
Poolsize number of threads in the thread pool
Getactivecount: Active line Cheng in thread pool
Thread pooling can be monitored by inheriting threads that you have BeforeExecute AfterExecute terminated methods that customize the thread pool
This article is from the "ITER Summary" blog, please be sure to keep this source http://5855156.blog.51cto.com/5845156/1965072
The art of Java concurrency-reading notes-Nineth thread pool