[英]Interrupting a runnable in executor threadpool
Suppose I started 10 threads using executor framework in java. 假设我在Java中使用executor框架启动了10个线程。 I want to stop/interrupt a callables based on certain conditions later on.
我想稍后根据某些条件停止/中断可调用对象。 What is the best way to do that.
这样做的最好方法是什么。 I understand future.cancel(true), does not solve the issue.
我了解future.cancel(true),无法解决问题。
Calling shutdownNow() or cancel() doesn't stop the ongoing runnable. 调用shutdownNow()或cancel()不会停止正在进行的可运行对象。 What these methods do is simply call .interrupt() on the respective thread(s).
这些方法所做的只是在相应线程上调用.interrupt()。 The problem is, your runnable doesn't handle InterruptedException (and it can't).
问题是,您的runnable不处理InterruptedException(并且不能)。 It's a pretty common problem described in multiple books and articles, but still it's a bit counterintuitive.
这是在许多书籍和文章中描述的一个非常普遍的问题,但是仍然有点违反直觉。
In order to do that, you need to do quite a few things. 为此,您需要做很多事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.