How to declare Callable to execute function returning void in Java?
13,263
Solution 1
Should I declare them
Callable<Void>
andFuture<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.
Solution 2
I think you should declare them Callable<?>
and Future<?>
. Then you can implement them anyway you want including Callable<Void>
and Future<Void>
.
Comments
-
Michael almost 2 years
Suppose I would like to run static method
foo
asynchronouslyvoid foo() throws Exception {...}
Since
foo
throws an exception I would prefer create aCallable
and invokeExecutorService.submit
with it to get aFuture
.Now I wonder how to declare those
Callable
andFuture
properly. Should I declare themCallable<Void> and Future<Void>?