CompletableFuture
구문
public class CompletableFuture<T> extends Object implements Future<T>, CompletionStage<T>
예시
import java.util.function.Supplier; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; public class CompletableFutureTest { public static void main(String args[]) throws ExecutionException, InterruptedException { Calculator calc = new Calculator(4, 7); CompletableFuture<Integer> future = CompletableFuture.supplyAsync(calc); future.thenAccept(result -> { System.out.println(result); }); System.out.println("CompletableFutureTest End.... "); Thread.sleep(10000); } } // Calculator class class Calculator implements Supplier<Integer> { private int x, y; public Calculator(int x, int y) { this.x = x; this.y = y; } @Override public Integer get() { try { Thread.sleep(3000); } catch(InterruptedException e) { e.printStackTrace(); } return x + y; } }
출력
CompletableFutureTest End.... 11