Close

Java - RunnableFuture and FutureTask

[Updated: Oct 3, 2018, Created: Oct 3, 2018]

RunnableFuture<V> Interface

This interface extends both Future<V> and Runnable interfaces.

Successful execution of the run method causes completion of the Future and allows access to its results.

FutureTask<V> class

This class implements RunnableFuture. It wraps either a Callable<T> or Runnable.

Examples

Following example uses FutureTask with java.lang.Thread.

package com.logicbig.example;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class FutureTaskExample  {
  public static void main(String[] args) throws ExecutionException, InterruptedException {
      FutureTask<String> futureTask = new FutureTask<>(new Callable<String>() {
          @Override
          public String call() {
              try {
                  //simulating long running task
                  Thread.sleep(1000);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              System.out.println("task finished");
              return "The result";
          }
      });

      new Thread(futureTask).start();
      System.out.println("Thread started");
      String s = futureTask.get();
      System.out.println(s);
  }
}
Thread started
task finished
The result

Following examples use FutureTask with ExecutorService.

package com.logicbig.example;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

public class FutureTaskWithExecutorExample {
  public static void main(String[] args) throws ExecutionException, InterruptedException {
      FutureTask<String> futureTask = new FutureTask<>(new Runnable() {
          @Override
          public void run() {
              try {
                  //simulating long running task
                  Thread.sleep(1000);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              System.out.println("task finished");

          }
      }, "The result");

      ExecutorService es = Executors.newSingleThreadExecutor();
      es.execute(futureTask);
      String s = futureTask.get();
      System.out.println(s);
      es.shutdown();
  }
}
task finished
The result
package com.logicbig.example;

import java.util.concurrent.*;

public class FutureTaskWithExecutorExample2 {
  public static void main(String[] args) throws ExecutionException, InterruptedException {
      FutureTask<String> futureTask = new FutureTask<>(new Callable<String>() {
          @Override
          public String call() throws Exception {
              //simulating long running task
              Thread.sleep(1000);
              System.out.println("returning");
              return "The result";
          }
      }
      );

      ExecutorService es = Executors.newSingleThreadExecutor();
      es.execute(futureTask);
      String s = futureTask.get();
      System.out.println(s);
      es.shutdown();
  }
}
returning
The result

Example Project

Dependencies and Technologies Used:

  • JDK 11
  • Maven 3.5.4

RunnableFuture Example Select All Download
  • java-runnable-future-interface
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • FutureTaskExample.java

    See Also