CompletableFuture
thenApply 메소드는 비동기 연산 결과로 넘어온 값을 입력으로 받고 처리 후 반환하는 함수를 입력으로 받는다.
@Test
public void TestFuture() {
CompletableFuture<String> completableFuture
= CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Hello";
});
CompletableFuture<Void> future = completableFuture
.thenAcceptAsync(s -> System.out.println("Computation finished: " + s));
try {
future.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}