Java 线程池详解学习:FixedThreadPool,CachedThreadPool,ScheduledThreadPool...
Java常用的线程池有FixedThreadPool和CachedThreadPool,我们可以通过查看他们的源码来进行学习。
Java的源码下载参考这篇文章:Java源码下载和阅读(JDK1.8) - zhangpeterx的博客
在源码的目录java/util/concurrent
下找到Executors.java
文件查看源码。
里面提到的封装类有:
newScheduledThreadPool
,newSingleThreadScheduledExecutor
,newCachedThreadPool
,newSingleThreadExecutor
,newFixedThreadPool
,newWorkStealingPool
,newFixedThreadPool
我这里只把newFixedThreadPool
,newCachedThreadPool
的源码放出来:
public class Executors {/*** Creates a thread pool that reuses a fixed number of threads* operating off a shared unbounded queue. At any point, at most* {@code nThreads} threads will be active processing tasks.* If additional tasks are submitted when all threads are active,* they will wait in the queue until a thread is available.* If any thread terminates due to a failure during execution* prior to shutdown, a new one will take its place if needed to* execute subsequent tasks. The threads in the pool will exist* until it is explicitly {@link ExecutorService#shutdown shutdown}.** @param nThreads the number of threads in the pool* @return the newly created thread pool* @throws IllegalArgumentException if {@code nThreads <= 0}*/public static ExecutorService newFixedThreadPool(int nThreads) {return new ThreadPoolExecutor(nThreads, nThreads,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());}/*** Creates a thread pool that reuses a fixed number of threads* operating off a shared unbounded queue, using the provided* ThreadFactory to create new threads when needed. At any point,* at most {@code nThreads} threads will be active processing* tasks. If additional tasks are submitted when all threads are* active, they will wait in the queue until a thread is* available. If any thread terminates due to a failure during* execution prior to shutdown, a new one will take its place if* needed to execute subsequent tasks. The threads in the pool will* exist until it is explicitly {@link ExecutorService#shutdown* shutdown}.** @param nThreads the number of threads in the pool* @param threadFactory the factory to use when creating new threads* @return the newly created thread pool* @throws NullPointerException if threadFactory is null* @throws IllegalArgumentException if {@code nThreads <= 0}*/public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {return new ThreadPoolExecutor(nThreads, nThreads,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>(),threadFactory);}/*** Creates a thread pool that creates new threads as needed, but* will reuse previously constructed threads when they are* available. These pools will typically improve the performance* of programs that execute many short-lived asynchronous tasks.* Calls to {@code execute} will reuse previously constructed* threads if available. If no existing thread is available, a new* thread will be created and added to the pool. Threads that have* not been used for sixty seconds are terminated and removed from* the cache. Thus, a pool that remains idle for long enough will* not consume any resources. Note that pools with similar* properties but different details (for example, timeout parameters)* may be created using {@link ThreadPoolExecutor} constructors.** @return the newly created thread pool*/public static ExecutorService newCachedThreadPool() {return new ThreadPoolExecutor(0, Integer.MAX_VALUE,60L, TimeUnit.SECONDS,new SynchronousQueue<Runnable>());}/*** Creates a thread pool that creates new threads as needed, but* will reuse previously constructed threads when they are* available, and uses the provided* ThreadFactory to create new threads when needed.* @param threadFactory the factory to use when creating new threads* @return the newly created thread pool* @throws NullPointerException if threadFactory is null*/public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {return new ThreadPoolExecutor(0, Integer.MAX_VALUE,60L, TimeUnit.SECONDS,new SynchronousQueue<Runnable>(),threadFactory);}
上面的代码说明了newFixedThreadPool
,newCachedThreadPool
只是把ThreadPoolExecutor
进行了封装而已,所以建议直接使用ThreadPoolExecutor
而不是这些封装类。
阿里巴巴代码规范中也提到这件事:
【强制】线程池不允许使用 Executors 去创建,而是通过 ThreadPoolExecutor 的方式,这样
的处理方式让写的同学更加明确线程池的运行规则,规避资源耗尽的风险。
说明:Executors 返回的线程池对象的弊端如下:
1)FixedThreadPool 和 SingleThreadPool:
允许的请求队列长度为 Integer.MAX_VALUE,可能会堆积大量的请求,从而导致 OOM。
2)CachedThreadPool 和 ScheduledThreadPool:
允许的创建线程数量为 Integer.MAX_VALUE,可能会创建大量的线程,从而导致 OOM。
下篇文章:Java线程池详解学习:ThreadPoolExecutor
Java 线程池详解学习:FixedThreadPool,CachedThreadPool,ScheduledThreadPool...相关推荐
- Java线程池详解学习:ThreadPoolExecutor
Java线程池详解学习:ThreadPoolExecutor Java的源码下载参考这篇文章:Java源码下载和阅读(JDK1.8) - zhangpeterx的博客 在源码的目录java/util/ ...
- Java 线程池详解及实例代码
转载自 Java 线程池详解及实例代码 这篇文章主要介绍了Java 线程池的相关资料,并符实例代码,帮助大家学习参考,需要的朋友可以参考下 线程池的技术背景 在面向对象编程中,创建和销毁对象是很费时 ...
- java线程池详解及五种线程池方法详解
基础知识 Executors创建线程池 Java中创建线程池很简单,只需要调用Executors中相应的便捷方法即可,比如Executors.newFixedThreadPool(int nThrea ...
- Java 线程池详解
构造一个线程池为什么需要几个参数?如果避免线程池出现OOM?Runnable和Callable的区别是什么?本文将对这些问题一一解答,同时还将给出使用线程池的常见场景和代码片段. 基础知识 Execu ...
- 【转载】Java线程池详解
目录 1.线程池的优势 2.Java中的ThreadPoolExecutor类 3.线程池的主要参数 4.线程池流程 5.线程池为什么需要使用(阻塞)队列? 6.线程池为什么要使用阻塞队列而不使用非阻 ...
- 【java线程系列】java线程系列之java线程池详解
一线程池的概念及为何需要线程池: 我们知道当我们自己创建一个线程时如果该线程执行完任务后就进入死亡状态,这样如果我们需要在次使用一个线程时得重新创建一个线程,但是线程的创建是要付出一定的代价的,如果在 ...
- 一文详解java线程池 详解Java线程池的七个参数 详解池化技术 java如何选择核心线程数 详解Java线程池的拒绝策略
目录 引言 线程池使用场景 加快请求响应(响应时间优先) 加快处理大任务(吞吐量优先) 特殊说明 线程池的池化技术 线程池的创建 手动创建 创建newFixedThreadPool线程池 创建newS ...
- Java线程池详解及常用方法
前言 最近被问到了线程池的相关问题.于是准备开始写一些多线程相关的文章.这篇将介绍一下线程池的基本使用. Executors Executors是concurrent包下的一个类,为我们提供了创建线程 ...
- Java中线程池详解
一.线程池简介 线程池的概念 线程池就是首先创建一些线程,它们的集合称为线程池,使用线程池可以很好的提高性能,线程池在系统启动时既创建大量空闲的线程,程序将一个任务传给线程池.线程池就会启动一条线程来 ...
最新文章
- Silverlight实用窍门系列:52.Silverlight中的MVVM框架极速入门(以MVVM Light Toolkit为例)...
- 商汤及联合实验室入选论文重点解读 | ECCV 2018
- 【ABAP】更新交货单交货数量和拣配数量
- 涨姿势了,raise...from... 是个什么操作?
- 百度地图标点点击变色_《和平精英》版本爆料第三弹:雪地洞穴开启!组队标点功能升级~...
- 两个点击事件共用一个方法_工作必技:教你简单方法一个电脑开两个,多个微信!...
- c语言读取acc文件的采样率,C语言文件操作:打开检查文件指针访问模式
- python整数加法计算器_Python应用实例赏析2.1简单计算
- 视频编解码,bbv 缓冲区的上溢和下溢
- 103_Power Pivot 透视表中空白标签处理及百分比
- fullcalendar next 不变化_让不懂编程的人爱上iPhone开发(2017秋iOS11+Swift4+Xcode9版)-第3篇...
- android项目中导入actionbarsherlock 需要注意的地方
- ZStack CMP多云管理平台有何不同?
- 移动iptv安装三方软件
- 华为主题包hwt下载_hwtTool-hwtTool(华为主题开发工具)下载 v9.1.3.302官方版--pc6下载站...
- 音频驱动不支持您的计算机硬件,声卡驱动装不上导致电脑没声音怎么办?
- HCNE实验指导文档(全)
- 台式电脑开机显示蓝屏 检查计算机上的病毒,电脑出现蓝屏是怎么回事
- CCS7.3安装,关闭win10家庭版自带杀毒
- line-height行高
热门文章
- PostgreSQL 常用命令
- MPB:中科院生态环境中心邓晔组-从环境样本中提取高质量DNA-研磨加DNeasy试剂盒方法...
- 广东省生态土壤所孙蔚旻团队FEMS: 砷锑污染土壤剖面的微生物世界
- Cell:基因表达的改变和群落的更替塑造了全球海洋宏转录组
- 中科院遗传发育所发表“重组菌群体系在根系微生物组研究中应用”的重要综述...
- NBT:牛瘤胃微生物组的参考基因组集
- python使用np.argsort对一维numpy概率值数据排序获取升序索引、获取的top索引(例如top2、top5、top10)索引二维numpy数组中对应的原始数据:原始数据概率最小的头部数据
- R语言Brown-Forsythe检验验证组间方差是否相等实战:执行Brown-Forsythe检验、如果各组间的方差不相等我们该怎么办(进行方差分析)
- python实现D‘Agostino‘s K-squared test正态分布检验
- NLP语义标注模型数据准备及实战