ThreadPoolExecutor 的 shutdown() 和shutdownNow()
2015-10-26 17:54
417 查看
public List<Runnable> shutdownNow() {
List<Runnable> tasks;
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
checkShutdownAccess();
advanceRunState(STOP);
interruptWorkers();
tasks = drainQueue();
} finally {
mainLock.unlock();
}
tryTerminate();
return tasks;
}
public void shutdown() {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
checkShutdownAccess();
advanceRunState(SHUTDOWN);
interruptIdleWorkers();
onShutdown(); // hook for ScheduledThreadPoolExecutor
} finally {
mainLock.unlock();
}
tryTerminate();
} 对比其代码可知 shutdown 只是将空闲的线程interrupt() 了, 因此在shutdown()之前提交的任务可以继续执行直到结束。而shutdownNow 是interrupt所有线程, 因此大部分线程将立刻被中断。之所以是大部分,而不是全部 ,是因为interrupt()方法能力有限。如果线程中没有sleep 、wait、Condition、定时锁等应用, interrupt()方法是无法中断当前的线程的。所以,ShutdownNow()并不代表线程池就一定立即就能退出,它可能必须要等待所有正在执行的任务都执行完成了才能退出。
例如下面这种情况 将一直打印 go to sleeppackage edu.neu.xiaobing.concurrent;import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class Interrupt { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub ExecutorService executor = Executors.newCachedThreadPool();
executor.execute(new Interrupt().new MyRunnable());
executor.shutdownNow();
}
class MyRunnable implements Runnable{ @Override
public void run() {
// TODO Auto-generated method stub
while(true){
System.out.println("go to sleep");
// try {
//
// Thread.sleep(100000);
//
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// System.out.println("sleeping");
// }
}
}
}}
List<Runnable> tasks;
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
checkShutdownAccess();
advanceRunState(STOP);
interruptWorkers();
tasks = drainQueue();
} finally {
mainLock.unlock();
}
tryTerminate();
return tasks;
}
public void shutdown() {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
checkShutdownAccess();
advanceRunState(SHUTDOWN);
interruptIdleWorkers();
onShutdown(); // hook for ScheduledThreadPoolExecutor
} finally {
mainLock.unlock();
}
tryTerminate();
} 对比其代码可知 shutdown 只是将空闲的线程interrupt() 了, 因此在shutdown()之前提交的任务可以继续执行直到结束。而shutdownNow 是interrupt所有线程, 因此大部分线程将立刻被中断。之所以是大部分,而不是全部 ,是因为interrupt()方法能力有限。如果线程中没有sleep 、wait、Condition、定时锁等应用, interrupt()方法是无法中断当前的线程的。所以,ShutdownNow()并不代表线程池就一定立即就能退出,它可能必须要等待所有正在执行的任务都执行完成了才能退出。
例如下面这种情况 将一直打印 go to sleeppackage edu.neu.xiaobing.concurrent;import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class Interrupt { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub ExecutorService executor = Executors.newCachedThreadPool();
executor.execute(new Interrupt().new MyRunnable());
executor.shutdownNow();
}
class MyRunnable implements Runnable{ @Override
public void run() {
// TODO Auto-generated method stub
while(true){
System.out.println("go to sleep");
// try {
//
// Thread.sleep(100000);
//
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// System.out.println("sleeping");
// }
}
}
}}
相关文章推荐
- 精选30道Java笔试题解答
- light OJ 1258 - Making Huge Palindromes【Manacher】
- Codeforces Round #327 (Div. 2) B. Rebranding 模拟
- PHP垃圾回收机制
- 使用js实现前端缓存
- C++:对象的初始化和构造函数
- 关于ListView和GridView的应用
- 设计模式 --> (2)单例模式
- 夺命雷公狗jquery---22-bind为jquery对象绑定多个相关事件
- LintCode : 有效回文串
- jsp中getParameter() 和 getAttribute()
- MediaCodec : H264硬解码核心代码总结
- 【JAVA核心技术卷一】Interface 接口
- JAVA WEB 导出 Word
- Yii2 自定义过滤器
- 【JAVA核心技术卷一】Inheritance 继承
- 1022. D进制的A+B (20)
- 回文子串【Manacher】算法时间复杂度O(n)
- Java 四大域对象总结
- Java WebService入门实例