Java中的线程池
2016-03-31 22:55
471 查看
线程池对应的类为java.util.concurrent.ThreadPoolExecutor
构造方法:
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
RejectedExecutionHandler handler)
*@param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* 核心线程数,就是即使空闲也保持存活,将允许核心线程退出设置为true
* @param maximumPoolSize the maximum number of threads to allow in the
* pool 最大线程数量
* @param keepAliveTime when the number of threads is greater than
* the core, this is the maximum time that excess idle threads
* will wait for new tasks before terminating.
* @param unit the time unit for the {@code keepAliveTime} argument
* @param workQueue the queue to use for holding tasks before they are
* executed. This queue will hold only the {@code Runnable}
* tasks submitted by the {@code execute} method.
* 任务队列,当核心线程不空闲时,任务将会被添加到任务队列
* @param handler the handler to use when execution is blocked
* because the thread bounds and queue capacities are reached
* 当线程池中的线程数量等于最大线程数量,且任务队列已满,则由handle分配处理策略
通过ThreadPoolExecutor类的方法public void execute(Runnable command)将线程任务添加到线程池的步骤:
(1)如果线程池中的数量小于corePoolSize,即使线程池中的线程都处于空闲状态也会创建新的线程来处理被添加的任务
(2)如果线程池中的数量等于corePoolSize且都不空闲,但是缓冲队列未满(缓冲队列有任务证明线程池中至少有corePoolSize个不空闲线程),则将任务添加到缓冲队列中
(3)如果线程池中的数量等于或大于corePoolSize小于maximumPoolSize ,缓冲队列满,则创建新线程处理任务
(4)如果线程池中的数量等于maximumPoolSize ,且缓冲队列满,则采用handler所指定的策略来处理任务
构造方法:
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
RejectedExecutionHandler handler)
*@param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* 核心线程数,就是即使空闲也保持存活,将允许核心线程退出设置为true
* @param maximumPoolSize the maximum number of threads to allow in the
* pool 最大线程数量
* @param keepAliveTime when the number of threads is greater than
* the core, this is the maximum time that excess idle threads
* will wait for new tasks before terminating.
* @param unit the time unit for the {@code keepAliveTime} argument
* @param workQueue the queue to use for holding tasks before they are
* executed. This queue will hold only the {@code Runnable}
* tasks submitted by the {@code execute} method.
* 任务队列,当核心线程不空闲时,任务将会被添加到任务队列
* @param handler the handler to use when execution is blocked
* because the thread bounds and queue capacities are reached
* 当线程池中的线程数量等于最大线程数量,且任务队列已满,则由handle分配处理策略
通过ThreadPoolExecutor类的方法public void execute(Runnable command)将线程任务添加到线程池的步骤:
(1)如果线程池中的数量小于corePoolSize,即使线程池中的线程都处于空闲状态也会创建新的线程来处理被添加的任务
(2)如果线程池中的数量等于corePoolSize且都不空闲,但是缓冲队列未满(缓冲队列有任务证明线程池中至少有corePoolSize个不空闲线程),则将任务添加到缓冲队列中
(3)如果线程池中的数量等于或大于corePoolSize小于maximumPoolSize ,缓冲队列满,则创建新线程处理任务
(4)如果线程池中的数量等于maximumPoolSize ,且缓冲队列满,则采用handler所指定的策略来处理任务
相关文章推荐
- javase学习笔记16.3.31
- JAVA多线程程序开发基础知识
- 关于Java8函数式编程你需要了解的几点
- Java中equals和==的区别
- Spring.Net学习笔记(5)-集合注入
- 了解RxJava之响应式好处(三)
- 20145313张雪纯 《Java程序设计》第5周学习总结
- spring(2)
- java多选自动批量上传文件并提交表单
- Struts过滤器
- Java多态的体现之接口
- Java异常
- 关于Java CPU占用过高的问题解决总结
- eclipse经常卡死的解决方法总结
- ssm框架搭建一:中文乱码问题
- Spring6:基于注解的Spring MVC(上篇)
- java 成员变量和局部变量
- Spring IOC
- RxJava中的错误处理
- Spring框架中IOC和aop