【发布时间】:2010-06-10 21:44:07
【问题描述】:
我今天发了question 一个关于线程模式的帖子,几乎所有人都建议我研究一下 ExecutorService。
当我查看 ExecutorService 时,我想我遗漏了一些东西。如果服务有一个正在运行或阻塞的线程,并且有人调用了 ExecutorService.shutdown(),会发生什么。正在运行或阻塞的线程会发生什么情况?
ExecutorService 是否在终止之前等待这些线程完成?
我问这个的原因是因为很久以前当我涉足 Java 时,他们弃用了 Thread.stop(),我记得停止线程的正确方法是使用信号量并在必要时扩展 Thread:
public void run () {
while (!this.exit) {
try {
block();
//do something
} catch (InterruptedException ie) {
}
}
}
public void stop () {
this.exit = true;
if (this.thread != null) {
this.thread.interrupt();
this.thread = null;
}
}
ExecutorService 如何处理正在运行的线程?
【问题讨论】:
标签: java multithreading design-patterns executorservice