Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ExecutorService.submit(Runnable task) return Future<?> rather than Future<Void>?

The ExecutorService has the following method:

Future<?> submit(Runnable task)

But since this Future's get method will always return null wouldn't the following signature be more appropriate?

Future<Void> submit(Runnable task)

like image 513
Roland Avatar asked May 15 '17 08:05

Roland


2 Answers

Jdk 1.8 inside submit method implementation void is used while in jdk1.6 it was Object .My understanding is it is more appropriate to use void that's why it is changed in jdk1.7/1.8.Only to support the interface not changing it

 public Future<?> submit(Runnable task) {
        if (task == null) throw new NullPointerException();
        RunnableFuture<Void> ftask = newTaskFor(task, null);
        execute(ftask);
        return ftask;
    }

and in JDK 1.6

 public Future<?> submit(Runnable task) {
       if (task == null) throw new NullPointerException();
       RunnableFuture<Object> ftask = newTaskFor(task, null);
       execute(ftask);
        return ftask;
   }
like image 153
gati sahu Avatar answered Sep 28 '22 16:09

gati sahu


Notice that Runnable's run method returns void primitive and not Void type. For that reason, Future cannot have a Void type and the solution was to make it a wildcard. Note that Future is from java 1.5 whereas Runnable is from 1.0. They wouldn't change run's return type to conform to the Future due to legacy code reasons.

like image 26
PentaKon Avatar answered Sep 28 '22 18:09

PentaKon