zel*_*ler 6 java multithreading executorservice
如果我有一个ExecutorService向其提供Runnable任务的东西,我可以选择一个并中断它吗?
我知道我可以取消Future返回(也提到这里:how-to-interrupt-executors-thread),但我怎么能提出一个InterruptedException.取消似乎没有这样做(事件虽然它应该通过查看源,可能OSX实现不同).至少这个片段不打印'它!' 也许我误解了一些东西而不是自定义runnable获得异常?
public class ITTest {
static class Sth {
public void useless() throws InterruptedException {
Thread.sleep(3000);
}
}
static class Runner implements Runnable {
Sth f;
public Runner(Sth f) {
super();
this.f = f;
}
@Override
public void run() {
try {
f.useless();
} catch (InterruptedException e) {
System.out.println("it!");
}
}
}
public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService es = Executors.newCachedThreadPool();
Sth f = new Sth();
Future<?> lo = es.submit(new Runner(f));
lo.cancel(true);
es.shutdown();
}
Run Code Online (Sandbox Code Playgroud)
}
Gra*_*ray 11
在这里做的正确的事是取消Future.问题是,这不一定会导致InterruptedException.
如果作业尚未运行,那么它将从可运行队列中删除 - 我认为这是你的问题.如果工作已经完成,那么它将不会做任何事情(当然).如果它仍在运行,那么它将中断线程.
中断线程只会导致sleep(),wait()和其他一些方法抛出InterruptedException.您还需要测试以查看线程是否已被中断:
if (Thread.currentThread().isInterrupted()) {
Run Code Online (Sandbox Code Playgroud)
此外,如果您抓住InterruptedException以下内容,重新设置中断标志是一个很好的模式:
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// this is a good pattern otherwise the interrupt bit is cleared by the catch
Thread.currentThread().interrupt();
...
}
Run Code Online (Sandbox Code Playgroud)
在你的代码中,我会在你打电话之前试着睡一觉lo.cancel(true).可能是您在有机会执行之前取消了未来.