1. Why use a thread pool
- Reduce resource consumption. By reusing the thread has been created to reduce thread creation and destruction caused by consumption.
- Improve the response speed. When the mission arrives, the task may not need to wait until the thread creation can be implemented immediately.
- Thread improve manageability. A thread is a scarce resource, if the unlimited creation, not only consumes system resources, but also reduce the stability of the system, using a thread pool can be unified distribution, tuning and monitoring.
2, thread pool parameters
parameter | Explanation |
---|---|
corePoolSize | The core number of threads |
maximumPoolSize | The maximum number of threads, is generally greater than the number of threads equal to the core |
keepAliveTime | Thread survival time (the time for the maximum number of threads is greater than the core number of threads, non-core thread) |
unit | Survival time units, and supporting the use of thread survival time |
workQueue | Task Queue |
threadFactory | Thread creation project |
handler | Rejection policy |
There are several strategies refused
Rejection policy | Explanation |
---|---|
AbortPolicy | Java thread pool for the default blocking policy, do not perform this task, but also directly throw a runtime exception. |
DiscardOldestPolicy | Discarding foremost task queue, and then try to re-execute task (repeat this procedure) |
DiscardPolicy | Also discard task, but do not throw an exception |
CallerRunsPolicy | It handles this task by the scheduler thread |
1) corePollSize: a core number of threads.
After you create a thread pool thread does not have any thread to wait until the arrival of a mission to create a thread to perform the task. By default, after you create a thread pool, the number of threads in the thread pool is 0, when there are tasks to later, it will create a thread to perform the task, when the number of threads in the thread pool reaches corePoolSize, will reach the the task queue them into the cache.
2) maximumPoolSize: maximum number of threads.
It shows that the number of threads in the thread up to be able to create.
3) keepAliveTime: idle time threads reserved.
4) TimeUnit: Retention time in idle thread.
TimeUnit.DAYS; //天
TimeUnit.HOURS; //小时
TimeUnit.MINUTES; //分钟
TimeUnit.SECONDS; //秒
TimeUnit.MILLISECONDS; //毫秒
TimeUnit.MICROSECONDS; //微妙
TimeUnit.NANOSECONDS; //纳秒
5) BlockingQueue <Runnable>: blocking queue storing tasks awaiting execution.
Parameters ArrayBlockingQueue, LinkedBlockingQueue, SynchronousQueue optional.
6) ThreadFactory: thread factory used to create a thread
7) RejectedExecutionHandler: queue is full, and the task is greater than the exception-handling strategy largest thread.
The following values
ThreadPoolExecutor.AbortPolicy: RejectedExecutionException discard task and throw an exception.
ThreadPoolExecutor.DiscardPolicy: the task is discarded, but does not throw an exception.
ThreadPoolExecutor.DiscardOldestPolicy: discard the foremost task queue, and then try to perform the task (Repeat this process) again
ThreadPoolExecutor.CallerRunsPolicy: This task is handled by the calling thread
3, the thread pool work flow diagram
Many articles cited below:
http://ifeve.com/java-threadpool/
https://segmentfault.com/a/1190000019319050
https://blog.csdn.net/hsf15768615284/article/details/78261173