当前位置:网站首页>线程池
线程池
2022-06-09 21:09:00 【InfoQ】
线程池

public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
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))
// 抛出RejectedExecutionException异常
reject(command);
}
final void runWorker(Worker w) {
Thread wt = Thread.currentThread();
Runnable task = w.firstTask;
w.firstTask = null;
w.unlock(); // allow interrupts
boolean completedAbruptly = true;
try {
while (task != null || (task = getTask()) != null) {
w.lock();
// If pool is stopping, ensure thread is interrupted;
// if not, ensure thread is not interrupted. This
// requires a recheck in second case to deal with
// shutdownNow race while clearing interrupt
if ((runStateAtLeast(ctl.get(), STOP) ||
(Thread.interrupted() &&
runStateAtLeast(ctl.get(), STOP))) &&
!wt.isInterrupted())
wt.interrupt();
try {
beforeExecute(wt, task);
Throwable thrown = null;
try {
task.run();
} catch (RuntimeException x) {
thrown = x; throw x;
} catch (Error x) {
thrown = x; throw x;
} catch (Throwable x) {
thrown = x; throw new Error(x);
} finally {
afterExecute(task, thrown);
}
} finally {
task = null;
w.completedTasks++;
w.unlock();
}
}
completedAbruptly = false;
} finally {
processWorkerExit(w, completedAbruptly);
}
}
边栏推荐
- Design of spectrum analyzer based on nexys3 VHDL board
- Daily 2 Trivia^_^
- Express order information extraction [3] - five labeled data to improve accuracy. Only five labeled samples are required to quickly complete the express order information task
- 队列在线程池中的应用
- PHP uses unlink to delete the file prompt without permission
- 如何定比特到服務器CPU飆高的原因
- PaddleNLP--UIE(二)--小样本快速提升性能(含doccona标注)
- 【流量分析】buu_[安洵杯 2019]Attack
- 保存和复制绘图时保留最少的空白
- Tke builds efk log service
猜你喜欢

Summary of linear regression
![Acitivit's mental journey: activiti6.0 customizes any jump command [including rollback]](/img/5f/97852ac7279fccdeff55c445344cbc.png)
Acitivit's mental journey: activiti6.0 customizes any jump command [including rollback]

paddleNLP-BUG和一些细节记录【一】

Heap (priority queue)

Yolo series target detection post-processing - non maximum suppression

Case study on infrared meter reading project of energy equipment intelligent system platform construction

10 common high-frequency business scenarios that trigger IO bottlenecks

Spider PI intelligent vision hexapod robot color tracking face recognition 0604

The highest monthly salary is 16K. After surviving the difficult times, we can finally usher in the dawn~

Deep learning and CV tutorial (13) | target detection (SSD, Yolo Series)
随机推荐
js基本数据类型和引用数据类型
Mysql:1062 Duplicate entry '1' for key 'PRIMARY'
JS cast and implicit type conversion and Unicode encoding
Comprendre le go des modules go. MOD et go. SUM
A thorough understanding of the very important ticket lock in concurrent programming -- stampedlock
一种RS485串口接口电流传感器开合式卡扣互感器支持modbus总线通讯规约协议
js 自增和自减(一元运算符)
SPI communication principle +verilog implementation and simulation (complete code)
An RS485 serial interface current sensor snap on type mutual inductor supports Modbus communication protocol
Save and copy drawings with minimal white space
Database, looking at the operation training problem, I suddenly can't do it. Solve it
能源设备智能化系统平台建设 红外抄表项目案例
Fedformer:frequency enhanced decomposedtransformer for long term series forecasting[still learning...]
Gbase8s database select Clause 2
易买网开发 趣买买 数据库的导入与数据库结构一览表 0605
保存和复制绘图时保留最少的空白
winform编程 控件TreeView树视图的基本使用 20220527
Modbus协议与SerialPort端口读写
Shell record
laravel8 使用when搜索