- 浏览: 87310 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (148)
- 全文检索 (1)
- java (29)
- xml (2)
- json (2)
- redis springmvc (1)
- Redis (5)
- 开发常识 (1)
- tomcat (2)
- 单元测试Junit (2)
- 设计模式 (2)
- spring (10)
- jvm (2)
- eclipse (4)
- echart (1)
- mybatis (1)
- mysql (3)
- web (1)
- js (2)
- PL/SQL (2)
- 其他 (1)
- 人生 (1)
- 安全 (2)
- jsp (2)
- 硬件电脑 (1)
- linux (3)
- git (10)
- oracle (8)
- ant (1)
- maven (2)
- 正则表达式 (2)
- chrome (1)
- 面试 (6)
- 多线程 (19)
- bug (11)
- java工具类 (3)
- 算法 (1)
- bug,git (1)
- shell (2)
- springmvc (2)
- Java8 (1)
- 消息队列-rocketmq (1)
- es (1)
- dubbo (0)
- spring cloud (0)
- hashmap (0)
- springboot (1)
- velocity (0)
参考:https://www.cnblogs.com/waytobestcoder/p/5323130.html
https://www.cnblogs.com/bqcoder/p/6089101.html CountDownLatch
构造器源码 jdk 1.6.0_43
参数详解:
corePoolSize :the number of threads to keep in the pool, even if they are idle.
核心线程数:核心线程会一直存活,即使没有任务需要执行 ,即使他们是空闲的。
当线程数小于核心线程数时,即使有线程空闲,线程池也会优先创建新线程处理。
当线程数>=corePoolSize,且任务队列已满时。线程池会创建新线程来处理任务。
maximumPoolSize: the maximum number of threads to allow in the pool.
最大线程数:线程池中允许的最大线程数。
当线程数=maxPoolSize,且任务队列已满时,线程池会拒绝处理任务而抛出异常。
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.
翻译:当线程数量大于核心时,这是多余的空闲线程在终止之前等待新任务的最大时间。
线程池中超过corePoolSize数目的空闲线程最大存活时间;
unit:the time unit for the keepAliveTime argument.
第三个参数keepAliveTime的时间单位
workQueue:the queue to use for holding tasks before they are executed. This queue will hold only the <tt>Runnable</tt> tasks submitted by the <tt>execute</tt> method.
二、ThreadPoolExecutor执行顺序:
线程池按以下行为执行任务
(1)当线程数小于核心线程数时,创建线程。
(2)当线程数大于等于核心线程数,且任务队列未满时,将任务放入任务队列。
(3)当线程数大于等于核心线程数,且任务队列已满
a.若线程数小于最大线程数,创建线程
b.若线程数等于最大线程数,抛出异常,拒绝任务
https://www.cnblogs.com/bqcoder/p/6089101.html CountDownLatch
//单例线程池一 package ddd; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class ThreadPool { private static ThreadPoolExecutor te; // 核心线程池大小(线程池初始大小); private static int SHOPPINGCOREPOOLSIZE = 50; // 最大线程池大小; private static int SHOPPINGMAXIMUMPOOLSIZE = 50; // 线程池中超过corePoolSize数目的空闲线程最大存活时间; private static int SHOPPINGKEEPALIVETIME = 5; // 线程池队列容量 private static int SHOPPINGQUEUESIZE = 4; public static ThreadPoolExecutor getInstance(){ if(null == te){ synchronized(ThreadPool.class){ if(null == te){ te = new ThreadPoolExecutor(SHOPPINGCOREPOOLSIZE, SHOPPINGMAXIMUMPOOLSIZE, SHOPPINGKEEPALIVETIME, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(SHOPPINGQUEUESIZE)); } } } return te; } }
构造器源码 jdk 1.6.0_43
/** * Creates a new <tt>ThreadPoolExecutor</tt> with the given initial * parameters and default thread factory and rejected execution handler. * It may be more convenient to use one of the {@link Executors} factory * methods instead of this general purpose constructor. * * @param corePoolSize the number of threads to keep in the * pool, even if they are idle. * @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 keepAliveTime * argument. * @param workQueue the queue to use for holding tasks before they * are executed. This queue will hold only the <tt>Runnable</tt> * tasks submitted by the <tt>execute</tt> method. * @throws IllegalArgumentException if corePoolSize or * keepAliveTime less than zero, or if maximumPoolSize less than or * equal to zero, or if corePoolSize greater than maximumPoolSize. * @throws NullPointerException if <tt>workQueue</tt> is null */ public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), defaultHandler); }
参数详解:
corePoolSize :the number of threads to keep in the pool, even if they are idle.
核心线程数:核心线程会一直存活,即使没有任务需要执行 ,即使他们是空闲的。
当线程数小于核心线程数时,即使有线程空闲,线程池也会优先创建新线程处理。
当线程数>=corePoolSize,且任务队列已满时。线程池会创建新线程来处理任务。
maximumPoolSize: the maximum number of threads to allow in the pool.
最大线程数:线程池中允许的最大线程数。
当线程数=maxPoolSize,且任务队列已满时,线程池会拒绝处理任务而抛出异常。
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.
翻译:当线程数量大于核心时,这是多余的空闲线程在终止之前等待新任务的最大时间。
线程池中超过corePoolSize数目的空闲线程最大存活时间;
unit:the time unit for the keepAliveTime argument.
第三个参数keepAliveTime的时间单位
workQueue:the queue to use for holding tasks before they are executed. This queue will hold only the <tt>Runnable</tt> tasks submitted by the <tt>execute</tt> method.
二、ThreadPoolExecutor执行顺序:
线程池按以下行为执行任务
(1)当线程数小于核心线程数时,创建线程。
(2)当线程数大于等于核心线程数,且任务队列未满时,将任务放入任务队列。
(3)当线程数大于等于核心线程数,且任务队列已满
a.若线程数小于最大线程数,创建线程
b.若线程数等于最大线程数,抛出异常,拒绝任务
发表评论
-
解决线程同步问题的思路
2018-07-31 16:35 401线程同步机制 操作系统中实现线程同步有一种工 ... -
发送线程与接收线程 速率一致
2018-08-07 09:17 373package com.jbx.xiezuo; /* ... -
线程间的协作关系与线程同步
2018-07-30 16:21 5101.线程间的协作关系 当一个进程中的多个线程为完成同 ... -
带互斥锁的并发线程执行流程
2018-07-30 12:38 426... -
线程间的竞争关系与线程互斥
2018-07-30 11:19 648线程间的竞争关系与线 ... -
线程的同步机制
2018-07-17 11:31 350线程的同步机制 (一) ... -
3个线程,A,B,C,这三个线程分别只能打印 “a”,“b”,“c”,怎么让这三个线程依次打印“abc"
2018-07-17 11:19 337package com.jbx.thread; /* ... -
定时器与图形动画设计
2018-07-17 11:15 0动画(animation) 都是利用人眼的视 ... -
线程分类
2018-07-17 09:10 295线程分类: 线程可分为用户线程(user thread) ... -
线程优先级
2018-07-17 09:08 333Java提供10个等级的线程优先级,分别用1~10表示,优先级 ... -
设计滚动字演示线程状态及改变方法
2018-07-16 16:17 279本例演示线程对象的生 ... -
线程对象的生命周期
2018-07-16 11:28 337线程对象的生命周期 1.Thread.State类声明的线程状 ... -
声明实现Runnable接口的奇数/偶数序列线程
2018-07-11 14:02 404package com.jbx.thread; ... -
声明继承Thread类的奇数/偶数序列线程
2018-07-10 23:58 395package com.jbx.com; /** ... -
Java的线程对象Runnable接口和Thread类
2018-07-10 21:42 368Java支持内置的多线程机制。 Java语言包中的Runnab ... -
并发程序设计
2018-07-09 17:22 251并发程序设计 1.顺序程 ... -
线程介绍
2018-07-09 16:55 276线程 1.引入线程机制的 ... -
进程介绍
2018-07-09 15:38 349进程 进程的定义和属性 进程是一个可并发执行的 ... -
多线程
2018-07-09 14:31 292...
相关推荐
死磕ThreadPoolExecutor线程池.pdf!!死磕ThreadPoolExecutor线程池.pdf死磕ThreadPoolExecutor线程池.pdf死磕ThreadPoolExecutor线程池.pdf
主要为大家详细介绍了ThreadPoolExecutor线程池的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
ThreadPoolExecutor线程池
主要介绍了解决python ThreadPoolExecutor 线程池中的异常捕获问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
提供工厂方法来创建不同类型的线程池,这篇文章主要介绍了Java ThreadPoolExecutor 线程池的使用介绍,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来...
在《阿里巴巴java开发手册》中...另外由于前面几种方法内部也是通过ThreadPoolExecutor方式实现,使用ThreadPoolExecutor有助于大家明确线程池的运行规则,创建符合自己的业务场景需要的线程池,避免资源耗尽的风险。
下面小编就为大家带来一篇ThreadPoolExecutor线程池原理及其execute方法(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
下面小编就为大家带来一篇简单谈谈ThreadPoolExecutor线程池之submit方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
线程池设计思路,java提供自带的线程池类ThreadPoolExecutor详解,Executors调用静态方法创建线程池
线程池ThreadPoolExecutor实战及其原理分析(下)线程池ThreadPoolExecutor实战及其原理分析(下)线程池ThreadPoolExecutor实战及其原理分析(下)线程池ThreadPoolExecutor实战及其原理分析(下)线程池ThreadPoolExecutor...
主要介绍了java ThreadPoolExecutor 并发调用实例详解的相关资料,需要的朋友可以参考下
java代码 ThreadPoolExecutor线程池并发测试例子如有误欢迎指正
ThreadPoolExecutor的使用和Android常见的4种线程池使用介绍
线程池的工作主要是控制运行的线程的数量,处理过程中将任务放入队列,然后在线程创建后启动这些任务,如果线程数量超过了最大数量,那么超出数量的线程排队等候,等其他线程执行完毕再从队列中取出任务来执行。...
ThreadPoolExecutor线程池源码
主要介绍了Python线程池模块ThreadPoolExecutor用法,结合实例形式分析了Python线程池模块ThreadPoolExecutor的导入与基本使用方法,需要的朋友可以参考下
线程池原理-ThreadPoolExecutor源码解析 1.构造方法及参数 2.阻塞对列: BlockingQueue 3.线程工厂: DefaultThreadFactory 4.拒绝策略: RejectedExecutionHandler 5.执行线程 Executor
从Python3.2开始,标准库为我们提供了 concurrent.futures 模块,它提供了 ThreadPoolExecutor (线程池)和ProcessPoolExecutor (进程池)两个类。 相比 threading 等模块,该模块通过 submit 返回的是一个 future ...