简体   繁体   English

如何声明Callable执行函数在Java中返回void?

[英]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. 现在我想知道如何恰当地声明那些CallableFuture Should I declare them 我应该申报吗?

Callable<Void> and Future<Void>?

Should I declare them Callable<Void> and Future<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. 对于原始类型intlong等, Void类似于包装类IntegerLong等。你可以说它是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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM