当前位置:网站首页>JUC常见的线程池源码学习 02 ( ThreadPoolExecutor 线程池 )
JUC常见的线程池源码学习 02 ( ThreadPoolExecutor 线程池 )
2022-07-30 14:11:00 【涛涛英语学不进去】
1. ThreadPoolExecutor设计原理

1.1. 线程池结构

1.2 线程池代码结构
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
//判空
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
//安全检查
this.acc = System.getSecurityManager() == null ?
null :
AccessController.getContext();
//赋值
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
1.3 DefaultThreadFactory
Java 有 1 5 10
默认工厂是默认优先级 5
Daemon 是 false //不是守护线程,所以默认线程池创建出来的是用户线程
Java提供了两种线程:守护线程和用户线程
守护线程,是指在程序运行时 在后台提供一种通用服务的线程,
这种线程并不属于程序中不可或缺的部分。
守护线程,是指在程序运行时 在后台提供一种通用服务的线程,
这种线程并不属于程序中不可或缺的部分。
通俗点讲,任何一个守护线程都是整个JVM中所有非守护线程的"保姆"。
用户线程和守护线程几乎一样,唯一的不同之处在于如果用户线程已经全部退出运行,
只剩下守护线程存在了,JVM也就退出了。
因为当所有非守护线程结束时,没有了被守护者,守护线程也就没有工作可做,
当然也就没有继续执行的必要了,程序就会终止,同时会杀死所有的"守护线程",
也就是说只要有任何非守护线程还在运行,程序就不会终止
在Java语言中,守护线程一般具有较低的优先级,它并非只由JVM内部提供,
用户在编写程序时也可以自己设置守护线程,
例如:将一个线程设置为守护线程的方法就是在调用start()
启动线程之前调用对象的setDaemon(true)方法,若将以上参数设置为false,
则表示的是用户进程模式,
需要注意的是,
当在一个守护线程中产生了其他的线程,那么这些新产生的线程默认还是守护线程,
用户线程也是如此。
/** * The default thread factory */
static class DefaultThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
DefaultThreadFactory() {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
namePrefix = "pool-" +
poolNumber.getAndIncrement() +
"-thread-";
}
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
if (t.isDaemon())
t.setDaemon(false);
if (t.getPriority() != Thread.NORM_PRIORITY)
t.setPriority(Thread.NORM_PRIORITY);
return t;
}
}
1.4 AbortPolicy() 默认拒绝策略
默认拒绝策略:直接抛出 RejectExecutionException异常
public static class AbortPolicy implements RejectedExecutionHandler {
public AbortPolicy() {
}
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
throw new RejectedExecutionException("Task " + r.toString() +
" rejected from " +
e.toString());
}
}
1.5 CallerRunsPolicy拒绝策略
调用者自己执行
public static class CallerRunsPolicy implements RejectedExecutionHandler {
public CallerRunsPolicy() {
}
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
r.run();
}
}
}
1.5 DiscardPolicy()拒绝策略
什么事都没干
public static class DiscardPolicy implements RejectedExecutionHandler {
public DiscardPolicy() {
}
rejectedExecution(Runnable r, ThreadPoolExecutor e) {
}
}
1.6 DicardOldestPolicy()拒绝策略
扔了队列中最早的任务,再把这个任务添加进来
public static class DiscardOldestPolicy implements RejectedExecutionHandler {
public DiscardOldestPolicy() {
}
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
e.getQueue().poll();
e.execute(r);
}
}
}
ok,鉴于源码阶段太耗时,开学水课再看,记录进度:下次该看
ThreadPoolExecutor源码一第11集 ThreadPoolExecutor
边栏推荐
- 关于容器的小案例
- [ARC092B] Two Sequences
- PyQt5快速开发与实战 9.1 使用PyInstaller打包项目生成exe文件
- ToDesk版本更新,引入RTC传输技术,是否早以替代向日葵远程控制?
- Six-faced ant financial clothing, resisting the bombardment of the interviewer, came to interview for review
- LeetCode二叉树系列——199二叉树的右视图
- 获取Google Advertising ID作为唯一识别码
- 43.【list链表的定义及初始化】
- 百家号取消接口发文功能:插外链获权重被堵死
- 数字信号处理课程实验报告(数字信号处理需要什么基础)
猜你喜欢

Teach you how to write an eye-catching software testing resume, if you don't receive an interview invitation, I will lose

自动化测试之数据驱动DDT详细篇

A new generation of open source free terminal tools, so cool

新一代开源免费的终端工具,太酷了

ARC117E零和范围2

算力顶天地,存力纳乾坤:国家超级计算济南中心的一体两面

开始学习C语言了

The truth of the industry: I will only test those that have no future, and I panic...

CS内网横向移动 模拟渗透实操 超详细

桌面软件开发框架大赏
随机推荐
What should I do if the sql server installation fails (what should I do if the sql server cannot be installed)
5. DOM
Why did I switch from developer to testing, 3 years software testing engineer, tell you the secret of this
MySQL客户端工具的使用与MySQL SQL语句
从实例来看DAO:权力分散的伟大尝试
时序数据库在船舶风险管理领域的应用
开源工具推荐:高性能计算辅助工具MegPeak
开始学习C语言了
Teach you how to write an eye-catching software testing resume, if you don't receive an interview invitation, I will lose
Six-faced ant financial clothing, resisting the bombardment of the interviewer, came to interview for review
【Vue.js 3.0源码】KeepAlive 组件:如何让组件在内存中缓存和调度?
Baijiahao cancels the function of posting documents on the interface: the weight of the plug-in chain is blocked
CF603E Pastoral Oddities
算力顶天地,存力纳乾坤:国家超级计算济南中心的一体两面
吃透Chisel语言.28.Chisel进阶之有限状态机(二)——Mealy状态机及与Moore状态机的对比
CS内网横向移动 模拟渗透实操 超详细
Android jump to google app market
[Advanced ROS] Lecture 11 Robot co-simulation based on Gazebo and Rviz (motion control and sensors)
CF780G Andryusha and Nervous Barriers
网络安全——lcx的使用