0. Thread activity and security
Active: Events in the desired multi-threading must occur. Active Issue: Thread starvation deadlock.
Security: Things that never happen wrong.
1, Thread starvation deadlock
What is starvation deadlock?
(1) The thread pool size is 1, and the task being performed submits a new task to the thread pool, and the current task waits until the new task executes, and the result is returned to the end. This situation can lead to starvation deadlocks.
(2) When the thread pool is larger, the task being performed in the thread pool waits for the result of the task in the thread queue. There will also be starvation deadlocks.
Hunger deadlock, in fact, is caused by the interdependence between tasks.
2. How to set the thread pool size
The size of the thread pool is not typically limited in code. Can be set by configuration mechanism generally.
Principle: Not too big, not too small. Too large, will lead to a large number of threads competing CPU, resulting in memory consumption, resulting in depletion of resources, too small, the resources are not fully utilized, the system throughput is low.
Setup method:
(1) compute-intensive: the size of the thread pool is the number of CPUs plus 1, that is: Nthread = ncpu + 1;
(2) IO-intensive:
A. Basic calculation method
Nthread = ncpu * UCPU * (1 + w/c)
which
Ucpu:cpu use rate, range 0~1
W:CPU wait time during use
Calculation time of C:CPU
B. Additional resource limitations
Such as: memory, file handle, socket handle, database connection.
Calculation method, MAX (nthread) = total resource Size/resource size required per task
C. Restricted by other resource pools
For example, the database connection pool size limit. The database connection pool and the thread pool size are mutually restrictive.
3, Configuration Threadpoolexecutor
(1) The relationship between each class
Inheritance Relationship:
Executor->executorservice->abstractexecutorservice->threadpoolexecutor
(2) Generate Threadpoolexecutor
The executors framework provides Newcachedthreadpool, Newffxedthreadpool, Newsinglethreadpool These methods to generate threadpoolexecutor for different internal parameters.
Public Threadpoolexecutor (int corepoolsize, int maximumpoolsize, Long KeepAliveTime, timeunit unit, blockingqueue<Runnable> workQueue) { This (Corepoolsize, maximumpoolsize, KeepAliveTime, Unit, WorkQueue, executors.defaultthreadfactory (), DefaultHandler); }
Newffxedthreadpool: You can set the basic parameters of the thread pool.
Newcachedthreadpool: Thread pool size unbounded, Max is integer,max_vaule. The base size is 1. The time-out is 1min.
(3) Buffer of one level thread through task queue
Newffxedthreadpool: A thread queue for use: bounded queues. such as: Arrayblockingqueue, bounded Linkedblockingqueue, priorityblockingqueue
Newcachedthreadpool: Suitable for unbounded queues: unbounded Linkedblockingqueue
(4) Saturation strategy
The saturation strategy is designed for bounded queues.
Saturation policies are triggered to handle tasks only when the bounded queue is filled.
The saturation strategy is: ...
The caller runs the policy and neither discards the task nor throws an exception.
A task that will saturate after executing the main thread of the call to execute. During execution, new tasks are no longer accepted into the thread pool.
The overload scenario for this strategy allows for a gentle performance reduction under high load conditions. Because of its overloaded process, the thread pool------The application->tcp-> client, the task queue.
(5) Threadpoolexecutor is a thread factory method that creates threads. Thread factories can customize them.
The Java Concurrency thread pool