Java 线程池中非核心线程开启的时机
Java 大约 1804 字执行三步曲
第一步
核心线程数未满时,开启新的核心线程。
第二步
核心线程数已满,加入等待队列。
第三步
等待队列已满,开启非核心线程。超过非核心线程上限,执行拒绝策略。
源码
java.util.concurrent.ThreadPoolExecutor#execute
public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
/*
* Proceed in 3 steps:
*
* 1. If fewer than corePoolSize threads are running, try to
* start a new thread with the given command as its first
* task. The call to addWorker atomically checks runState and
* workerCount, and so prevents false alarms that would add
* threads when it shouldn't, by returning false.
*
* 2. If a task can be successfully queued, then we still need
* to double-check whether we should have added a thread
* (because existing ones died since last checking) or that
* the pool shut down since entry into this method. So we
* recheck state and if necessary roll back the enqueuing if
* stopped, or start a new thread if there are none.
*
* 3. If we cannot queue task, then we try to add a new
* thread. If it fails, we know we are shut down or saturated
* and so reject the task.
*/
int c = ctl.get();
if (workerCountOf(c) < corePoolSize) {
if (addWorker(command, true))
return;
c = ctl.get();
}
if (isRunning(c) && workQueue.offer(command)) {
int recheck = ctl.get();
if (! isRunning(recheck) && remove(command))
reject(command);
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}
else if (!addWorker(command, false))
reject(command);
}
阅读 378 · 发布于 2023-06-19
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看
换一批
-
Spring Boot 使用 @Valid 校验前端传递的参数阅读 4982
-
Kubernetes 数据存储 PV 和 PVC阅读 1204
-
Linux 恢复删除的文件阅读 5416
-
软考-系统架构设计师:软件测试阅读 2564
-
Windows 网络适配器显示:网络管理员已经禁用了 Internet 连接共享阅读 3741
-
Spring Boot 返回加密后的 Response阅读 628
-
Vue computed 计算属性阅读 495
-
IDEA HTTP Client 上传文件阅读 910
-
Flutter Web 构建 release 版本阅读 170
-
PostgreSQL 备份与还原阅读 5304