๐ŸŽ“ A1Learner
๐Ÿ”
0 of 30 lessons completed

Java Executors and Futures

An executor service runs tasks on a pool of worker threads. A Future holds a result that a task will deliver later.

Three futures read in submission order

Three Callable tasks each return the length of one word. The main thread reads the futures in the order the tasks were submitted.

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class Main {
  public static void main(String[] args) throws Exception {
    ExecutorService pool = Executors.newFixedThreadPool(3);
    Future<Integer> first = pool.submit(new LengthCall("executor"));
    Future<Integer> second = pool.submit(new LengthCall("future"));
    Future<Integer> third = pool.submit(new LengthCall("pool"));
    System.out.println("executor: " + first.get());
    System.out.println("future: " + second.get());
    System.out.println("pool: " + third.get());
    pool.shutdown();
  }
}
class LengthCall implements Callable<Integer> {
  private final String word;
  LengthCall(String word) {
    this.word = word;
  }
  public Integer call() {
    return word.length();
  }
}

A submission after shutdown is refused

The pool shuts down before the task is submitted. The submit call throws a RejectedExecutionException, and the catch block prints the program line for that refusal.

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
public class Main {
  public static void main(String[] args) {
    ExecutorService pool = Executors.newFixedThreadPool(1);
    pool.shutdown();
    try {
      pool.submit(new LengthCall("late"));
      System.out.println("Submission accepted");
    } catch (RejectedExecutionException e) {
      System.out.println("Submission refused after shutdown");
    }
  }
}
class LengthCall implements Callable<Integer> {
  private final String word;
  LengthCall(String word) {
    this.word = word;
  }
  public Integer call() {
    return word.length();
  }
}

Example

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class Main {
  public static void main(String[] args) throws Exception {
    ExecutorService pool = Executors.newFixedThreadPool(3);
    Future<Integer> first = pool.submit(new LengthCall("concurrency"));
    Future<Integer> second = pool.submit(new LengthCall("executor"));
    Future<Integer> third = pool.submit(new LengthCall("future"));
    int firstLength = first.get();
    int secondLength = second.get();
    int thirdLength = third.get();
    pool.shutdown();
    System.out.println("concurrency: " + firstLength);
    System.out.println("executor: " + secondLength);
    System.out.println("future: " + thirdLength);
    System.out.println("Total length: " + (firstLength + secondLength + thirdLength));
  }
}
class LengthCall implements Callable<Integer> {
  private final String word;
  LengthCall(String word) {
    this.word = word;
  }
  public Integer call() {
    return word.length();
  }
}

Key points

  • An executor service accepts tasks and runs them on its pool threads.
  • A Callable task returns a value, and its Future carries that value back.
  • The get method waits for a task to finish and then returns its value.
  • Futures read in submission order keep the printed order fixed, even when tasks finish in another order.
  • The shutdown method stops the pool from accepting new tasks.
  • A task submitted after shutdown is refused with a RejectedExecutionException.

In this example, three tasks return the lengths of three words through their futures. The main thread reads the futures in submission order, prints each length, and prints the exact combined length.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

A program submits three tasks and then reads the first future. What does reading that future give the program?