- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经在网上搜索了一个星期了,但没有像这样的帖子 How do I get FutureTask to return after TimeoutException?似乎回答了我的问题。我从我的代码中提取了一个代码示例:
Future<Object> result = executorService.submit(new Callable<Object>() {
@Override public Object call() throws Exception {
...........
}
});
try {
return result.get(timeout.getValue(), timeout.getUnit().getTimeUnit());
} catch (TimeoutException e) {
result.cancel(true);
throw new TTimeoutException(e);
}
如果我运行这个,有时(千分之一)我会得到
java.lang.InterruptedException
at java.util.concurrent.FutureTask.awaitDone(FutureTask.java:400)
at java.util.concurrent.FutureTask.get(FutureTask.java:199)
at com.abc.callers.A.call(A.java:83)
第 83 行是上面代码示例中显示的 future 任务的 result.get() 。
现在我的问题是,将来调用 result.cancel(true) 会导致 result.get 中出现 InterruptedException 吗?如果没有,谁可以改变我当前线程的中断状态? AFAIK result.get() 与运行我正在取消的提交任务的线程不是同一线程..
最佳答案
来自 Javadoc:
boolean cancel(boolean mayInterruptIfRunning)
Attempts to cancel execution of this task. This attempt will fail if the task has already completed, has already been cancelled, or could not be cancelled for some other reason. If successful, and this task has not started when cancel is called, this task should never run. If the task has already started, then the
mayInterruptIfRunning
parameter determines whether the thread executing this task should be interrupted in an attempt to stop the task.After this method returns, subsequent calls to
isDone()
will always return true. Subsequent calls toisCancelled()
will always return true if this method returned true.
因此,如果设置了 mayInterruptIfRunning
,那么是的,它将尝试中断运行任务的线程。
换句话说,.cancel(true)
将尝试中断正在运行的任务,而 .cancel(false)
则不会。
听起来,如果您调用 .cancel(false)
并且任务尚未开始,result.get()
仍然会被中断。
关于java - FutureTask 中的awaitDone 抛出InterruptedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27171980/
基本上我有以下代码片段, (let [task (FutureTask. fn) thr (Thread. task)] (.start thr) ;;wait for signa
我正在尝试将请求发送到服务器...直到请求没有响应我想执行一个打印整数的循环..问题是我的循环一直持续下去,如果我执行它直到 100 它会打印 100 个值并且如果直到 1000,它就会打印 1000
我正在创建一个 Java 应用程序。启动时,我的应用程序将下载所有必需的文件。我的应用程序将解析 XML 文件并从 XML 文件的 URL 下载文件。我希望我的应用程序“一步一步”下载文件,所以我使用
在 FutureTask.get 的 CancellationException 异常之后访问底层 Callable 的首选方法是什么? 我有以下代码- public class Ping imple
假设我用 Java 做了一些事情,比如: RemoteResponse response = null; try { FutureTask task new FutureTask(....);
我正在制作一些虚拟程序来了解这个 Java 类。我的定时任务调用了一个什么也不做的任务,在中断它之前给它 3 秒的时间。这是代码: FutureTask task = new FutureTask<>
new Thread(new Runnable() { public void run() { .............
这是一个非常简单的代码: FutureTask task = new FutureTask<>(new Callable(){ @Overrid
我创建了以下 FutureTask 方法来异步运行方法。 public FutureTask SendAggregateEventAsync(final com.Company.Pro
我已经实现了并发实践中描述的自定义取消逻辑。 Encapsulating nonstandard cancellation in a task with newTaskFor . 这很好用,我可以调用
我检查了 Oracle Java API,它提供了一些信息 FutureTask.isDone() 但我需要检查任务是否已完成或因任何错误而终止。isDone() 方法即使完成/终止也会返回。但我需要
我目前正试图了解如何 FutureTask.cancel(true)正在工作,这是官方文档的相关部分 If the task has already started, then the mayInte
我尝试实现一个内部方法,以便在新线程中执行以下代码 MyPojo result = null; final MyPojo result2 = result; FutureTask runnableTa
下面的代码无法编译(JDK 1.8.0_40),我无法理解为什么。 public abstract class BackgroundThread { private final Executo
我已经在网上搜索了一个星期了,但没有像这样的帖子 How do I get FutureTask to return after TimeoutException?似乎回答了我的问题。我从我的代码中提
我想为昂贵的路径查找任务设置一个 FutureTask。所以我创建了这个 Callable 类。 public class GetPath implements Callable> { pri
考虑在 Callable 实例中进行长时间运行的计算。 并且考虑到这个计算的结果可以有一定的精度取决于计算时间,即:如果任务将被取消,那么它应该返回取消之前到目前为止计算的内容(例如,我们有一个计算无
我遇到了这个令人沮丧的问题:我试图查看下面的代码中是否抛出 TimeoutException,但这种情况从未发生过。它总是打印当前时间 5 次并完成。 class MyTask1 implements
在我的可调用代码中,我使用信号通知向另一个线程通知多个结束行为。 Callable 对象与 Executor 中的 FutureTasks 一起排队。排队后也可能被取消。 现在,我的问题是,我至少依赖
我有一个应用程序,它定期提交要在专用线程中执行的任务。这些任务是 FutureTask并且线程只不过是一个无限循环,它在作业进入队列时执行作业,如果为空则进入 hibernate 状态。 有时我需要等
我是一名优秀的程序员,十分优秀!