- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在 Callable 中处理 Thread.interrupted() 的正确方法是什么?我猜可调用对象应该抛出一个 InterruptedException;例如:
public class MyCallable implements Callable<Object> {
public Object call() {
Object result = null;
// Simulate long-running operation that calculates result
while (true) {
...
if (Thread.interrupted()) {
throw new InterruptedException();
}
}
result = ... // something produced by the long-running operation
return result;
}
}
这是正确的,还是有更合适的处理方式?谢谢。
最佳答案
编辑:
经过一些来回,您似乎希望能够中断您的 IO 例程。对于某些 NIO InterrutibleChannel
类来说,这似乎是一项不错的工作。例如,从下面的 BufferedReader
中读取是可中断的,并且会抛出 InterruptedIOException
。在这里查看 more examples of the NIO code .
BufferedReader in = new BufferedReader(new InputStreamReader(
Channels.newInputStream((new FileInputStream(
new File(...))).getChannel())));
然后,您可以调用 future.cancel()
,这将中断您的线程并导致 IO 抛出 InterruptedIOException
。如果发生这种情况,您可以不捕获IOException
并让它从call()
方法中流出。
如果您想将 call()
方法中断的情况传回给 Future
,那么我认为抛出 InterruptedException
就可以了。另一种选择是仅 return null;
或 call()
方法中的其他标记对象。如果线程被中断,我通常会这样做。
要记住的一件事是,如果 call()
抛出 InterruptedException
,当您执行 future.get()
时,它会抛出一个ExecutionException
并且该异常的原因 将是 InterruptedException
。如果 get(long timeout, TimeUnit unit)
超时,请不要混淆 future.get()
本身也会抛出 InterruptedException
.
try {
result = future.get();
} catch (ExecutionException e) {
if (e.getCause() instanceof InterruptedException) {
// call() method was interrupted
}
} catch (InterruptedException e) {
// get was interrupted
}
但是,如果 future.cancel(true)
被调用,则 future.get()
将抛出一个 CancellationException
。
关于java - 在 Callable 中处理 Thread.interrupted() 的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12805391/
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
我想知道如果我们有一个引用 const 的函数参数会发生什么功能如下图。 版本 1 int anotherFunc() { std::cout<<"inside anotherFunc"<
我正在尝试运行YOLOv8重新训练的对象检测测试,但我一直收到此错误。。我已经试过安装gnu-cobol和更新西芹了,还有其他想法吗?谢谢
我做了一个静态类方法(这里称为“函数”)f需要一个列表 Callable小号: import java.util.concurrent.*; import java.util.*; class A {
以下是我的类定义: class logline: def __init__(self,t,cmp,msg): self.t = t self.cmp = cmp
我正在用 dart 做一个小实验,我找不到一种方法来确定一个变量是否是“可调用的”,而无需显式检查每种类型(字符串、整数、 bool 等),并猜测它是否是可调用的,如果没有的话那些。我还尝试了一个 t
我大量使用来自 python 3 的 python 类型支持。 最近我试图将一个函数作为参数传递,但我没有找到使用 kwargs 的任何帮助。在 typing.Callable签名。 请检查下面的代码
每当我运行实现可调用的程序时,我都会以顺序形式获得输出。 就像,这是我的程序: package com.handson; import java.util.concurrent.ArrayBlocki
我编写了一个注释(在Spring Boot应用程序中)并尝试将其应用到Callable的call()方法上,但它不起作用,但另一方面,当应用于普通方法时(请参阅下面的代码),它有效,这一直困扰着我,你
我试图将对象列表拆分为较小的子列表,并在不同的线程上单独处理它们。所以我有以下代码: List instances = xmlInstance.readInstancesFromXml
我想我可以用 std.traits.functionAttributes 来做到这一点,但它不支持 static。对于任何类型的可调用对象(包含 opCall 的结构),我如何判断该可调用对象是否使用
我是新来的,如果我把这个放到错误的主题中,很抱歉? 我的问题: class TaskEol implements Callable> { ArrayList coordinates =
我有一些代码可以从目录中的多个 JAR 动态加载类。因为 JAR 中的代码是不受信任的,所以我希望通过对它们进行“沙盒处理”来保护主应用程序免受编码不良或恶意动态类的影响。我想做的一件事是“时间盒”不
这个问题在这里已经有了答案: Overriding special methods on an instance (5 个答案) 关闭 9 年前。 我有以下代码 import numpy as np
我正在尝试制作一个类似于 https://github.com/ElgarL/TownyChat/blob/master/src/com/palmergames/bukkit/TownyChat/To
有人可以解释为什么要继承未参数化和参数化的Callable: from typing import Callable from typing import NoReturn from typing i
我有一个任务要以固定速率运行。但是,我还需要每次执行后的任务结果。这是我尝试过的: 任务 class ScheduledWork implements Callable { public St
给定一个实例 x的 Callable , 我怎样才能运行 x在一个单独的进程中,以便我可以重定向进程的标准输入和输出?例如,有没有办法构建一个 Process来自Callable ?有没有标准Exec
我有以下可调用对象: public class Worker implements Callable{ @Override public Boolean call(){ boo
现在很清楚了what a metaclass is ,有一个相关的概念,我一直在使用,但不知道它的真正含义。 我想每个人都犯了一次括号错误,导致“对象不可调用”异常。更何况,使用 __init__ 和
我是一名优秀的程序员,十分优秀!