多线程线程池
Posted 飞翔在天
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多线程线程池相关的知识,希望对你有一定的参考价值。
参考:JDK文档
ThreadPoolExecutor
1. ThreadPoolExecutor实现
-
fixed
-
single
-
cache类型阻塞队列区别,有界还是无界队列
2. ThreadPoolExecutor
When a new task is submitted in method execute(Runnable):
if fewer than corePoolSize threads are running, a new thread is created to handle the request, even if other worker threads are idle.
// 场景:当前线程数少于corePoolSize,创建新线程(尽管有其他工作线程是空闲状态)
Else if fewer than maximumPoolSize threads are running, a new thread will be created to handle the request only if the queue is full.
// 场景:线程数大于corePoolSize但小于maxinum,仅当队列满时才创建新的线程
2.1 几个参数
-
ThreadFactory: 定义线程组名称,线程优先级和daemon参数
-
Keep-alive:If the pool currently has more than corePoolSize threads, excess threads will be terminated if they have been idle for more than the keepAliveTime // 线程池数大于corePoolSize,如果线程空闲时间大于keepAliveTime将被终止。
-
BlockingQueue: transfer and hold submitted tasks
-
-
If fewer than corePoolSize threads are running, the Executor always prefers adding a new thread rather than queuing.
-
// 线程数量小于corePoolSize,直接新增线程执行任务
-
-
If corePoolSize or more threads are running, the Executor always prefers queuing a request rather than adding a new thread.
// 大于等于corePoolSize时,新来的任务将被排队
-
If a request cannot be queued, a new thread is created unless this would exceed maximumPoolSize, in which case, the task will be rejected.
-
// 请求不能被入队(队列满)时,如果线程数未超过maximum,线程将被创建。否则将被拒绝
排队的几种策略:
-
-
Direct handoffs直接传递:A good default choice for a work queue is a
-
-
-
Unbounded queues:
-
-
以上是关于多线程线程池的主要内容,如果未能解决你的问题,请参考以下文章