[英]How to declare Callable to execute function returning void in Java?
Suppose I would like to run static method foo
asynchronously 假设我想异步运行静态方法
foo
void foo() throws Exception {...}
Since foo
throws an exception I would prefer create a Callable
and invoke ExecutorService.submit
with it to get a Future
. 由于
foo
抛出异常,我宁愿创建一个Callable
并使用它调用ExecutorService.submit
来获取Future
。
Now I wonder how to declare those Callable
and Future
properly. 现在我想知道如何恰当地声明那些
Callable
和Future
。 Should I declare them 我应该申报吗?
Callable<Void> and Future<Void>?
Should I declare them
Callable<Void>
andFuture<Void>
?我应该声明它们
Callable<Void>
和Future<Void>
吗?
Yes. 是。
Void
is similar to the wrapper classes Integer
, Long
etc. for the primitive types int
, long
etc. You could say it's a wrapper class for void
, even though void
is not really a type. 对于原始类型
int
, long
等, Void
类似于包装类Integer
, Long
等。你可以说它是void
的包装类,即使void
实际上不是一个类型。
I think you should declare them Callable<?>
and Future<?>
. 我认为你应该声明它们
Callable<?>
和Future<?>
。 Then you can implement them anyway you want including Callable<Void>
and Future<Void>
. 然后你可以实现它们,包括
Callable<Void>
和Future<Void>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.