CKTIS / Java
Future — an asynchronous task result
Future<V> represents an asynchronous task result. Future.get retrieves the result, waiting if necessary, while Future.isDone checks completion, which does not necessarily mean success.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
Future<V> future = executor.submit(task);
V result = future.get();
boolean done = future.isDone();java.util.concurrent.Future<V> represents an asynchronous computation's result; V specifies its type. ExecutorService.submit submits a task for execution and returns a Future. The handle itself is not a thread.
Future.get() waits for the result. It can throw ExecutionException for task failure, CancellationException after cancellation, or InterruptedException when waiting is interrupted. get(timeout, unit) bounds the wait and can throw TimeoutException; timing out does not cancel the task.
Future.isDone() does not wait: it returns true after success, failure, or cancellation. Do not interpret this as a guaranteed result. cancel(true) only attempts cancellation, potentially interrupting the executing thread; it does not guarantee computation stops.
The example calls get() before isDone(), so the expected output is always 42, then true. Checking before get() would depend on scheduling. The try-with-resources statement closes the executor and waits for its tasks to finish. The example targets Java SE 21; the production runtime is not attested.
Examples
import java.util.concurrent.ExecutionException;
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 InterruptedException, ExecutionException {
try (ExecutorService executor = Executors.newFixedThreadPool(1)) {
Future<Integer> future = executor.submit(() -> 6 * 7);
System.out.println(future.get());
System.out.println(future.isDone());
}
}
}Result
42 true
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.