Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the class called RunnableFuture<T> instead of CallableFuture<T>? [closed]

The following code is from AbstractExecutorService:

 /**
 * Returns a <tt>RunnableFuture</tt> for the given callable task.
 *
 * @param callable the callable task being wrapped
 * @return a <tt>RunnableFuture</tt> which when run will call the
 * underlying callable and which, as a <tt>Future</tt>, will yield
 * the callable's result as its result and provide for
 * cancellation of the underlying task.
 * @since 1.6
 */
 protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
    return new FutureTask<T>(callable);
 }

I fail to see why the class of the returned object from newTaskFor() would be called RunnableFuture instead of CallableFuture? What am I missing here ?

like image 650
Inquisitive Avatar asked Dec 13 '22 00:12

Inquisitive


1 Answers

The point of a RunnableFuture is:

  1. It's a Future
  2. It's a Runnable

It converts the Callable you already have into something that is both a Future and a Runnable. It covers the exact use case it is intended to cover. If you have a Callable and need a Future, there's the FutureTask constructor.

like image 144
Marko Topolnik Avatar answered Dec 29 '22 00:12

Marko Topolnik