线程池创建线程的逻辑图:

我们分析CachedThreadPool线程池里的线程是如何被回收的。

//Executors
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
} //ThreadPoolExecutor
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}

牢牢记住CachedThreadPool的corePoolSize=0, maximumPoolSize=Integer.MAX_VALUE

工作线程的死循环:

//ThreadPoolExecutor
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 ((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);
}
}

当工作线程第二次获取的task等于null时,线程将退出while循环,于是就死掉了。

//ThreadPoolExecutor
private Runnable getTask() {
// 标记poll()是否超时
boolean timedOut = false; retry:
for (;;) {
int c = ctl.get();
int rs = runStateOf(c); // Check if queue empty only if necessary.
if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
//工作线程数-1
decrementWorkerCount();
//返回null,工作线程将退出while循环,即线程会死掉
return null;
} boolean timed; // Are workers subject to culling? for (;;) {
int wc = workerCountOf(c);
// allowCoreThreadTimeOut 默认为 false, newCachedThreadPool的corePoolSize为0
// 所以 timed = false || true,timed恒为true
timed = allowCoreThreadTimeOut || wc > corePoolSize;
// 对于newCachedThreadPool,wc 恒小于 maximumPoolSize
// 第一次进for循环 true && !(false && true) = true
// poll超时后,第二次进for循环 true && !(true && true) = false
if (wc <= maximumPoolSize && ! (timedOut && timed))
break;
if (compareAndDecrementWorkerCount(c))
//poll超时后,第二次进for循环,
return null;
c = ctl.get(); // Re-read ctl
if (runStateOf(c) != rs)
continue retry;
// else CAS failed due to workerCount change; retry inner loop
} try {
//走poll分支
//poll会阻塞,直到有人调用workQueue.offer;或者超时返回null
Runnable r = timed ?
workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
workQueue.take();
if (r != null)
return r;
//超时
timedOut = true;
} catch (InterruptedException retry) {
timedOut = false;
}
}
}

如图中所示:有2种情况会创建工作线程,1. 工作线程数小于corePoolSize;2. 入队失败,且工作线程数小于maximumPoolSize

//ThreadPoolExecutor
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();
}
//对于CachedThreadPool,如果有工作线程在poll中阻塞,则入队成功
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);
}
//对于CachedThreadPool,如果没有工作线程在poll中阻塞,则入队失败
//初次调用execute,走这个分支,创建工作线程
else if (!addWorker(command, false))
reject(command);
}

CachedThreadPool使用的是SynchronousQueue的

入队 :offer(E e)

出队:poll(long timeout, TimeUnit unit)

工作线程调用poll阻塞,等待timeout时间,如果超时,则返回null并回收线程;如果在等待期内,有任务入队,则成功返回任务,继续执行线程while循环。

CachedThreadPool里的线程是如何被回收的?的更多相关文章

  1. Linux线程退出、资源回收、资源清理的方法

    首先说明线程中要回收哪些资源,理解清楚了这点之后在思考资源回收的问题. 1.子线程创建时从父线程copy出来的栈内存; 线程退出有多种方式,如return,pthread_exit,pthread_c ...

  2. 小谈Java里的线程

    今天,我们来谈一谈Java里的线程. 一.进程与线程的基本概念 大家可能没听过线程这个概念,但是相信,用计算机的朋友都听过进程这个概念.打开电脑的任务管理器,我们就可以看到许多进程.它们主要分为三类, ...

  3. Linux 系统编程 学习:09-线程:线程的创建、回收与取消

    Linux 系统编程 学习:09-线程:线程的创建.回收与取消 背景 我们在此之前完成了 有关进程的学习.从这一讲开始我们学习线程. 完全的开发可以参考:<多线程编程指南> 在Linux ...

  4. 并发编程——认识java里的线程

    本文系作者 chaoCode原创,转载请私信并在文章开头附带作者和原文地址链接. 违者,作者保留追究权利. 前言 并发编程在我们日常开发中是时时刻刻都有在用的,只不过大部分的代码底层已经帮我们去做了一 ...

  5. 简短总结一下C#里跨线程更新UI(转)

    摘自: http://my.oschina.net/sdqxcxh/blog/53707 跨线程更新UI是写多线程程序尤其是通信类的程序经常遇到的问题,这里面主要的问题是冲突,比如数据线程想要更新UI ...

  6. 简短总结一下C#里跨线程更新UI

    摘自: http://my.oschina.net/sdqxcxh/blog/53707 跨线程更新UI是写多线程程序尤其是通信类的程序经常遇到的问题,这里面主要的问题是冲突,比如数据线程想要更新UI ...

  7. 【原创】一个线程oom,进程里其他线程还能运行吗?

    引言 这题是一个网友@大脸猫爱吃鱼给我的提问,出自今年校招美团三面的一个真题.大致如下 一个进程有3个线程,如果一个线程抛出oom,其他两个线程还能运行么? 先说一下答案,答案是还能运行 不瞒大家说, ...

  8. [并发并行]_[C/C++]_[C++标准库里的线程安全问题]

    场景 1.写普通的程序时, 经常会使用cout来做输出, 每个进程只有一个控制台, 如果多线程调用cout时会出状况吗? 2.之所以研究cout会不会在并发下调用有问题, 是因为曾经有一个bug的崩溃 ...

  9. 一个线程oom,进程里其他线程还能运行吗?

    线程之间互相不影响:守护线程生活周期相同 引言 这题是一个网友@大脸猫爱吃鱼给我的提问,出自今年校招美团三面的一个真题.大致如下 一个进程有3个线程,如果一个线程抛出oom,其他两个线程还能运行么? ...

随机推荐

  1. 【Maven】2.使用Nexus3搭建Maven私服+上传第三方jar包到本地maven仓库

    参考文章: http://www.cnblogs.com/luotaoyeah/p/3791966.html --------------------------------------------- ...

  2. [Redis] - 高并发下Redis缓存穿透解决

    高并发情况下,可能都要访问数据库,因为同时访问的方法,这时需要加入同步锁,当其中一个缓存获取后,其它的就要通过缓存获取数据. 方法一: 在方法上加上同步锁 synchronized //加同步锁,解决 ...

  3. IDEA启动时自动报Plugin Error错误

    Plugin Error Problems found loading plugins: Plugin "JBoss Integration" was not loaded: re ...

  4. 【hbuilder】如何根据Geolocation获得的坐标获取所在城市?

    第一步通过mui.plusReady[表示页面加载事件]调用hbuilder提供的百度定位 mui.plusReady(function() { plus.geolocation.getCurrent ...

  5. Mysql 统计查询

    SELECT ub.telphone, SUM(IF(b.type = 1 AND b.level = 1, 1, 0)) AS type11, SUM(IF(b.type = 1 AND b.lev ...

  6. Machine Learning 资料

    Awesome系列 Awesome Machine Learning Awesome Deep Learning Awesome TensorFlow Awesome TensorFlow Imple ...

  7. ninja install error

    ninja install ...... CMake Error at cmake_install.cmake:36 (FILE):  file INSTALL cannot find  " ...

  8. Atcoder Yet Another Palindrome Partitioning(状压dp)

    Atcoder Yet Another Palindrome Partitioning 思路: 一个字符串满足条件的情况是奇数字母个数小于等于1,也就是异或起来是1<<j(0<=j& ...

  9. Vue项目骨架屏注入实践

    相比于早些年前后端代码紧密耦合.后端工程师还得写前端代码的时代,如今已发展到前后端分离,这种开发方式大大提升了前后端项目的可维护性与开发效率,让前后端工程师关注于自己的主业.然而在带来便利的同时,也带 ...

  10. IdentityServer3零星笔记

    Scope 是什么?有哪几种类型?每种类型都怎么使用? StandardScopes.All是什么概念? 解释:在Scope的Claims属性里包含的所有声明(类型是ScopeClaim,它的name ...