解决Java线程池任务执行完毕后线程回收问题
|
public static void method1() {
BlockingQueue queue = new LinkedBlockingQueue();
ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 6, 10, TimeUnit.SECONDS, queue);
for ( int i = 0; i < 20; i++) {
executor.execute( new Runnable() {
public void run() {
try {
System. out.println( this.hashCode()/1000);
for ( int j = 0; j < 10; j++) {
System. out.println( this.hashCode() + ":" + j);
Thread.sleep(this.hashCode()%2);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System. out.println(String. format("thread %d finished", this.hashCode()));
}
});
}
}
|
|
debug模式下,任务执行完后,显示的效果如下:
![]() 也就是说,任务已经执行完毕了,但是线程池中的线程并没有被回收。但是我在ThreadPoolExecutor的参数里面设置了超时时间的,好像没起作用,原因如下:
工作线程回收需要满足三个条件:
1) 参数allowCoreThreadTimeOut为true 2) 该线程在keepAliveTime时间内获取不到任务,即空闲这么长时间 3) 当前线程池大小 > 核心线程池大小corePoolSize。
|
|
public static void method1() {
BlockingQueue queue = new LinkedBlockingQueue();
ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 6, 1, TimeUnit.SECONDS, queue);
executor.allowCoreThreadTimeOut(true);
for ( int i = 0; i < 20; i++) {
executor.execute( new Runnable() {
public void run() {
try {
System. out.println( this.hashCode()/1000);
for ( int j = 0; j < 10; j++) {
System. out.println( this.hashCode() + ":" + j);
Thread. sleep(this.hashCode()%2);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System. out.println(String. format("thread %d finished", this.hashCode()));
}
});
}
}
|
| 需要注意的是,allowCoreThreadTimeOut 的设置需要在任务执行之前,一般在new一个线程池后设置;在allowCoreThreadTimeOut设置为true时,ThreadPoolExecutor的keepAliveTime参数必须大于0。 |
|
public static void method1() {
BlockingQueue queue = new LinkedBlockingQueue();
ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 6, 1, TimeUnit.SECONDS, queue);
for ( int i = 0; i < 20; i++) {
executor.execute( new Runnable() {
public void run() {
try {
System. out.println( this.hashCode()/1000);
for ( int j = 0; j < 10; j++) {
System. out.println( this.hashCode() + ":" + j);
Thread. sleep(this.hashCode()%2);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System. out.println(String. format("thread %d finished", this.hashCode()));
}
});
}
executor.shutdown();
}
|
| 在任务执行完后,调用shutdown方法,将线程池中的空闲线程回收。该方法会使得keepAliveTime参数失效。 |
解决Java线程池任务执行完毕后线程回收问题的更多相关文章
- C# 多线程 线程池(ThreadPool) 2 如何控制线程池?
线程池启动了,但是没有方法去控制线程池,如果子线程出现了问题,难道线程池就死了吗? 我们可以设置线程池的线程数量,进行加入任务,线程池会自动分配并且合理的执行,但是控制不了又有啥意思呢. 线程池里线程 ...
- java并发编程(四) 线程池 & 任务执行、终止源码分析
参考文档 线程池任务执行全过程:https://blog.csdn.net/wojiaolinaaa/article/details/51345789 线程池中断:https://www.cnblog ...
- C# 本进程执行完毕后再执行下一线程
最近做了一套MES集成系统,由上料到成品使自动化运行,其中生产过程是逐步的,但是每一个动作都需要独立的线程进行数据监听,那么就需要实现线程等待. 代码: using System; using Sys ...
- 解决批处理命令执行完毕后自动关闭cmd窗口方法
问题描述: 日常开发工作中,为了节省多余操作导致浪费时间,我们经常会自己建一些批处理脚本文件(xx.bat),文件中包含我们需要执行的命令,有时候我们希望执行完毕后看一下执行的结果,但是窗口执行完毕后 ...
- 戏(细)说Executor框架线程池任务执行全过程(下)
上一篇文章中通过引入的一个例子介绍了在Executor框架下,提交一个任务的过程,这个过程就像我们老大的老大要找个老大来执行一个任务那样简单.并通过剖析ExecutorService的一种经典实现Th ...
- Java线程状态、线程start方法源码、多线程、Java线程池、如何停止一个线程
下面将依次介绍: 1. 线程状态.Java线程状态和线程池状态 2. start方法源码 3. 什么是线程池? 4. 线程池的工作原理和使用线程池的好处 5. ThreadPoolExecutor中的 ...
- 戏(细)说Executor框架线程池任务执行全过程(上)
一.前言 1.5后引入的Executor框架的最大优点是把任务的提交和执行解耦.要执行任务的人只需把Task描述清楚,然后提交即可.这个Task是怎么被执行的,被谁执行的,什么时候执行的,提交的人就不 ...
- Android AsyncTask内部线程池异步执行任务机制简要分析
如下分析针对的API 25的AsyncTask的源码: 使用AsyncTask如果是调用execute方法则是同步执行任务,想要异步执行任务可以直接调用executeOnExecutor方法,多数情况 ...
- java线程池与五种常用线程池策略使用与解析
背景:面试中会要求对5中线程池作分析.所以要熟知线程池的运行细节,如CachedThreadPool会引发oom吗? java线程池与五种常用线程池策略使用与解析 可选择的阻塞队列BlockingQu ...
随机推荐
- linux-history-ps1-1
1.串行端口终端(/dev/ttySn) 串行端口终端(Serial Port Terminal)是使用计算机串行端口连接的终端设备.计算机把每个串行端口都看作是一个字符设备.有段时间这些串行端口设备 ...
- docker安装xxl-job
一 安装mysql root@localhost tmp]# docker run -itd --name xxl-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD= ...
- 虚树总结&题单&简要题解
简介 虚树,即剔除所有无关结点,只保留询问点和询问点的相关结点(两两之间的LCA),建一棵新树,这棵新树就是虚树.通过虚树,可以有效的减小询问(甚至修改)的复杂度.设询问点的个数是\(k\),那么建虚 ...
- 使用WebStorm运行vue项目
在WebStorm中怎么打开一个已有的项目,这个不用多说,那么如何运行一个vue项目呢? 1.点击下图中右上角的红框. 2.在出现的弹框中选中左上角“+”下的“npm”,如下图所示. 3.选中第二步的 ...
- @清晰掉 malloc是如何分配内存的?
任何一个用过或学过C的人对malloc都不会陌生.大家都知道malloc可以分配一段连续的内存空间,并且在不再使用时可以通过free释放掉.但是,许多程序员对malloc背后的事情并不熟悉,许多人甚至 ...
- 待处理bug
https://laravel-china.org/docs/laravel/5.1/installation/1039#installation composer 下载laravel 有问题
- iOS自动化--Spaceship使用实践
Spaceship ### 脚本操作 证书,app,provision等一些列apple develop后台操作,快速高效. github地址 spaceship开发文档 文档有列出常用的api调用d ...
- k8s网络通信
K8s有三种网络, 1.节点网络 2.pod网络 3.service集群网络,由kube-proxy来负责
- 【flask】项目集成Sentry收集线上错误日志
flask集成sentry分为4个步骤: 首先在sentry官网注册1个账号 然后创建1个新的项目,这里我选择的是flask,这会得到一些关于sdk的使用说明 接下来创建一个简单的flask项目使用s ...
- OpenStack Nova 高性能虚拟机之 CPU 绑定
目录 文章目录 目录 前文列表 KVM KVM 的功能列表 KVM 工具集 KVM 虚拟机的本质是什么 vCPU 的调度与性能问题 Nova 支持的 vCPU 绑定 vcpu\_pin\_set 配置 ...
