16

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>?
Michael
  • 41,026
  • 70
  • 193
  • 341

2 Answers2

23

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.

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • 1
    And as you have to return a result of type Void, your Callback#call() method has to return null. And thus, as you anyway have to return, you might use Boolean as return type and return Boolean.TRUE (or in case of an error Boolean.FALSE). So it just might have better readability. – user2777500 May 05 '14 at 14:23
2

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

emory
  • 10,725
  • 2
  • 30
  • 58