ThreadPoolExecutor 中为什么WorkQueue会在corePoolSize满了之后入队
简介
看别人的博客, 不清楚为什么会入队.
code
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 简单来说就是如果小于corePoolSize直接创建新线程
* 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)) { // 反正只要正在运行着, 就会直接入队了. 入队也会有四种判定方式, 一种是直接提交队列, 就是大于corePoolSize, 小于maxPoolSize创建新的线程. 一种是无界队列, 反正来的任务都等着执行, 还有有界, 就是大于corePoolSize, 且大于设定的有界个数, 才会去创建新的线程.
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);
}
ThreadPoolExecutor 中为什么WorkQueue会在corePoolSize满了之后入队的更多相关文章
- Java: 线程池(ThreadPoolExecutor)中的参数说明
最近在看<阿里巴巴Android开发手册>,里面有这样几句话: [强制]新建线程时,必须通过线程池提供(AsyncTask 或者ThreadPoolExecutor或者其他形式自定义的线程 ...
- 线程池中状态与线程数的设计分析(ThreadPoolExecutor中ctl变量)
目录 预备知识 源码分析 submit()源码分析 shutdownNow()源码分析 代码输出 设计目的与优点 预备知识 可以先看下我的另一篇文章对于Java中的位掩码BitMask的解释. 1.一 ...
- Linux内核中的Workqueue机制分析
1. 什么是workqueue Linux中的workqueue(工作队列)主要是为了简化在内核创建线程而设计的.通过相应的工作队列接口,可以使开发人员只关心与特定功能相关的处理流程,而不必关心内核线 ...
- ThreadPoolExecutor中策略的选择与工作队列的选择(java线程池)
工作原理 1.线程池刚创建时,里面没有一个线程.任务队列是作为参数传进来的.不过,就算队列里面有任务,线程池也不会马上执行它们. 2.当调用 execute() 方法添加一个任务时,线程池会做如下判断 ...
- ThreadPoolExecutor 中的 shutdown() 、awaitTermination() 、 shutdownNow() 的用法
shutdown和awaitTermination为接口ExecutorService定义的两个方法,一般情况配合使用来关闭线程池. 方法简介shutdown方法:将线程池状态置为SHUTDOWN.平 ...
- ThreadPoolExecutor 中的 shutdown() 、 shutdownNow() 、 awaitTermination() 的用法和区别
Java并发编程中在使用到ThreadPoolExecutor时,对它的三个关闭方法(shutdown().shutdownNow().awaitTermination())的异同点如下: shutd ...
- 策略模式在ThreadpoolExecutor中的应用
偶然读到ThreadpoolExecutor的源码,发现里面使用到了策略模式,抓出来和大家分享下: 感兴趣的朋友们可以读读 ThreadPoolExecutor的源码: public void set ...
- ThreadPoolExecutor中execute和submit的区别
1:入参不同 excute() 传入的是 Runable, submit 传入的是 Callable 或 Runable 1):execute 方法源码 public void execute(Run ...
- ThreadPoolExecutor中的submit()方法详细讲解
https://blog.csdn.net/qq_33689414/article/details/72955253
- Java调度线程池ScheduleExecutorService
如果在一个ScheduleExecutorService中提交一个任务,这个任务的调度周期设置 的时间比任务本身执行的时间短的话会出现什么情况?也就是在线程调度时间已经到了 但是上次的任务还没有做完的 ...
随机推荐
- @PathVaribale
/** * @pathVaribale * 作用: 用于获取url 中的占位符的值. * 例如:请求 url 中 /delete/{id},这个{id}就是 url 占位符. * url 支持占位符是 ...
- adb常见命令及日志
一.adb介绍 1.adb(Android Debug Bridge)是android sdk的一个工具 2.adb是用来连接安卓设备和PC端的桥梁,用户可以通过adb在电脑上对手机进行一系列操作 3 ...
- Apache Druid RCE漏洞复现及修复(CVE-2023-25194)
Apache Druid RCE漏洞复现及修复(CVE-2023-25194) 2023-03-16 声明:本文分享的安全工具和项目均来源于网络,漏洞环境本地搭建,仅供安全研究与学习,如用于其他用途, ...
- 通过IP计算分析归属地
在产品中可能存在不同客户端,请求同一个服务端接口的场景. 例如小程序和App或者浏览器中,如果需要对请求的归属地进行分析,前提是需要先获取请求所在的国家或城市,这种定位通常需要主动授权,而用户一般是不 ...
- 个人对Debian桌面系统的简单使用分享
前言 自从安装Debian12作为双系统已经过了大半个月,平常主用Debian,偶尔切回Windows找找文档,总体来说体验还是很不错的.先贴个桌面照 为什么要使用Linux作为个人桌面 当初刚从Wi ...
- 基于ThinkPHP5知识付费系统AntPayCMS
历时6个月开发基于ThinkPHP5.1知识付费系统AntPayCMS,自己作IT开发已经10年,一直想自己开发自己的系统,虽然看网上也有很多知识付费类的网站的,但基于TP基本很少,而且自己也一直想做 ...
- 开启PyTorch的第一天
为积极响应导师的要求,我开启了我的深度学习之旅,成为炼丹大队的一员. 今天先是安装PyTorch,路子大家网上已经摸的很清了,我主要参考的是 https://blog.csdn.net/weixin_ ...
- 1-1, 一个简单的mysql 安装教程,基于mysql 5.7解压版本.
下载mysql. 略,去官网 1. 配置my.cnf. 把mysql提供的配置模板 copy到/etc/my.cnf (先读取/etc/my.cnf,再去读/etc/mysql/my.cnf,第三个读 ...
- 2 MyBatis动态sql之where标签|转
1 MyBatis动态SQL之if 语句 2 MyBatis动态sql之where标签|转 3 MyBatis动态SQL之set标签|转 4 MyBatis动态SQL之trim元素|转 5 MyBat ...
- Spring注解之@Autowired:装配构造函数和属性
在User类中创建一个构造函数,传入参数student: import org.springframework.beans.factory.annotation.Autowired; import o ...