Michael
Michael

Reputation: 42050

How to declare Callable to execute function returning void in Java?

Suppose I would like to run static method foo asynchronously

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.

Now I wonder how to declare those Callable and Future properly. Should I declare them

Callable<Void> and Future<Void>?

Upvotes: 16

Views: 14543

Answers (2)

emory
emory

Reputation: 10891

I think you should declare them Callable<?> and Future<?>. Then you can implement them anyway you want including Callable<Void> and Future<Void>.

Upvotes: 2

Jesper
Jesper

Reputation: 206806

Should I declare them Callable<Void> and 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.

Upvotes: 23

Related Questions