start();//创建一定数量的线程池,进行线程循环
stop();//停止所有线程循环,回收所有资源
addTask();//添加任务
Executors.newFixedThreadPool(100);//创建固定大小的线程池
Executors.newSingleThreadExecutor();//创建只有一个线程的线程池
Executors.newCachedThreadPool();//创建一个不限线程数上限的线程池,任何提交的任务都将立即执行
ThreadPoolExecutor的构造方法public ThreadPoolExecutor(
int corePoolPoolSize,//线程池长期维持的线程数
int maximumPoolSize, //线程数的上限
long keepAliveTime,//空闲线程存活时间
TimeUnit unit,//时间单位
BlockingQueue<Runnable> workQueue,//任务的排队队列
ThreadFactory threadFactory,//新线程的产生方式
RejectedExecutionHandler handler//拒绝策略
)
maximumPoolSize,对于新加入的任务,新建线程。maximumPoolSize,对于新加入的任务,执行拒绝策略(线程池默认的拒绝策略是抛异常)。Interger. MAX_VALUEpublic static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
ThreadFactory创建新线程。Interger. MAX_VALUE)Executors.newCachedThreadPool();
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
(1)Executors.newFixedThreadPool(int nThreads);//nThreads为线程的数量
(2)Executors.newFixedThreadPool(int nThreads,ThreadFactory threadFactory);//nThreads为线程的数量,threadFactory创建线程的工厂方式
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
newFixedThreadPool(1) 不同,可保证无需重新配置此方法所返回的执行程序即可使用其他的线程。(1)Executors.newSingleThreadExecutor() ;
(2)Executors.newSingleThreadExecutor(ThreadFactory threadFactory);// threadFactory创建线程的工厂方式
public class ScheduledThreadPoolExecutor
extends ThreadPoolExecutor
implements ScheduledExecutorService {
....................
/**
* Creates a new {@code ScheduledThreadPoolExecutor} with the
* given core pool size.
*
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @throws IllegalArgumentException if {@code corePoolSize < 0}
*/
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue());
}
.......................
}
(1)Executors.newScheduledThreadPool(int corePoolSize);// corePoolSize线程的个数
(2)newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory);// corePoolSize线程的个数,threadFactory创建线程的工厂
(1)Executors.newSingleThreadScheduledExecutor() ;
(2)Executors.newSingleThreadScheduledExecutor(ThreadFactory threadFactory);//threadFactory创建线程的工厂
maximumPoolSizes以避免拒绝新提交的任务。corePoolSize线程即可。(maximumPoolSize的值没有任何作用。)maximumPoolSizes一起使用时,有界队列有助于防止资源耗尽,但是调整和控制起来会更加困难。RejectedExecutionExceptionRejectedExecutionHandler rejected = null;
rejected = new ThreadPoolExecutor.AbortPolicy();//默认,队列满了丢任务抛出异常
rejected = new ThreadPoolExecutor.DiscardPolicy();//队列满了丢任务不异常
rejected = new ThreadPoolExecutor.DiscardOldestPolicy();//将最早进入队列的任务删,之后再尝试加入队列
rejected = new ThreadPoolExecutor.CallerRunsPolicy();//如果添加到线程池失败,那么主线程会自己去执行该任务
参考链接:
https://www.cnblogs.com/cdf-opensource-007/p/8769777.html
https://www.cnblogs.com/vince66/p/9325638.html
http://www.mamicode.com/info-detail-2740441.html
作者:天才程序YUAN
https://blog.csdn.net/JAck_chen0309/article/details/105250643
公众号“大咖笔记”所发表内容注明来源的,版权归原出处所有(无法查证版权的或者未注明出处的均来自网络,系转载,转载的目的在于传递更多信息,版权属于原作者。如有侵权,请联系,笔者会第一时间删除处理!
精品资料,超赞福利!
>Java精选面试题<
3000+ 道 BAT 大厂面试题在线刷,最新、最全 Java 面试题!
推荐 IntelliJ IDEA 15 款超神级插件(自用,真的超级牛掰)
Spring Boot 集成 WebSocket,实现前后端即时通讯,如此简单!
Spring Boot 项目不同环境打包配置与Shell脚本部署实践,太实用了!
技术交流群!
最近有很多人问,有没有读者交流群!想知道如何加入?方式很简单,兴趣相投的朋友,只需要点击下方卡片,回复“加群”,即可无套路入交流群!
文章有帮助的话,在看,转发吧。
谢谢支持哟 (*^__^*)