当前位置:网站首页>JUC common thread pool source learning 02 ( ThreadPoolExecutor thread pool )
JUC common thread pool source learning 02 ( ThreadPoolExecutor thread pool )
2022-07-30 14:59:00 【Taotao can't learn English】
1. ThreadPoolExecutor设计原理

1.1. 线程池结构

1.2 Thread pool code structure
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
The default factory is the default priority 5
Daemon 是 false //不是守护线程,So the default thread pool creates user threads
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()拒绝策略
The oldest task in the queue is thrown,Add this task again
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,Given that the source code stage is too time-consuming,Look at the water class when the school starts,记录进度:Watch it next time
ThreadPoolExecutorsource code first11集 ThreadPoolExecutor
边栏推荐
猜你喜欢

MongoDB starts an error Process: 29784 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=14)

那些破釜沉舟入局Web3.0的互联网精英都怎么样了?

5. DOM

Flink优化

LeetCode_98_验证二叉搜索树

这个编辑器居然号称快如闪电!

CVE-2022-33891 Apache Spark 命令注入复现

MaxWell抓取数据

A simple change for problem, knapsack problem sets of shell

ECCV 2022 | Towards Data Efficient Transformer Object Detectors
随机推荐
Flink optimization
Flask框架——Flask-SQLite数据库
Huawei's 7-year-experienced software testing director, gives some advice to all friends who want to change careers to learn software testing
Redis6.0 source code learning (5) ziplist
LoRaWAN网关源码分析(V1.0.2)
OFDM Sixteen Lectures 3- OFDM Waveforms
Could not acquire management access for administration
How awesome is the "12306" architecture?
[Advanced ROS] Lecture 11 Robot co-simulation based on Gazebo and Rviz (motion control and sensors)
惊艳!京东T8纯手码的Redis核心原理手册,基础与源码齐下
3 years of software testing experience, the interview requires a monthly salary of 22K, obviously he has memorized a lot of interview questions...
有关收集箱的改进建议
(论文翻译]未配对Image-To-Image翻译使用Cycle-Consistent敌对的网络
业内人士真心话:只会测试没有前途的,我慌了......
Cookie simulation login "recommended collection"
canal抓取数据
算力顶天地,存力纳乾坤:国家超级计算济南中心的一体两面
5. DOM
What is the relationship between the construction of smart cities and 5G technology in the new era
The evolution of content products has three axes: traffic, technology, and product form