今天发了一个关于线程模式的问题,几乎所有人都建议我看一下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 如何处理正在运行的线程?