- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正试图了解如何 FutureTask.cancel(true)
正在工作,这是官方文档的相关部分
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.
cancel
的实现摘自 Github
public boolean cancel(boolean mayInterruptIfRunning) {
if (!(state == NEW &&
UNSAFE.compareAndSwapInt(this, stateOffset, NEW,
mayInterruptIfRunning ? INTERRUPTING : CANCELLED)))
return false;
try { // in case call to interrupt throws exception
if (mayInterruptIfRunning) {
try {
Thread t = runner;
if (t != null)
t.interrupt();
} finally { // final state
UNSAFE.putOrderedInt(this, stateOffset, INTERRUPTED);
}
}
} finally {
finishCompletion();
}
return true;
}
所以,基本上我们可以看到唯一的东西
cancel(true)
正在做的是打电话
interrupt
在工作线程上。那么,如果
call
我的方法
FutureTask
看起来像这样
SomeType call() {
for(int i = 0; i < 1000000000; i++) {
//pass
}
return someValue;
}
所以,我的问题 - 我是否必须添加手动检查线程中断才能取消这种类型的 FutureTasks ?我的意思是这似乎很明显,因为我没有调用任何可以处理中断的 IO 函数,也没有检查
Thread.currentThread().isInterrupted()
,所以这个任务似乎是不可取消的,但是在任何官方文档中仍然没有提到我们必须处理中断或自我才能取消任务,所以最好问问别人的意见。
最佳答案
Java 中没有先发制人。对于任何可中断的事物,它都必须合作。所以是的,任务必须检查它是否被中断,否则即使 future 被取消,它也会运行到最后。令人失望,我知道。
关于java - 我是否必须在 FutureTask 中手动处理中断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66420192/
基本上我有以下代码片段, (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 状态。 有时我需要等
我是一名优秀的程序员,十分优秀!