diff --git a/java8to11/src/completableFuture/App.java b/java8to11/src/completableFuture/App.java index d43c534f..c83274f2 100644 --- a/java8to11/src/completableFuture/App.java +++ b/java8to11/src/completableFuture/App.java @@ -3,6 +3,7 @@ package completableFuture; import java.util.Arrays; import java.util.List; import java.util.concurrent.*; +import java.util.stream.Collectors; public class App { @@ -10,24 +11,111 @@ public class App { return () -> System.out.println(message + ": " + Thread.currentThread().getName()); } + private static CompletableFuture getWorld(String message) { + return CompletableFuture.supplyAsync(() -> { + System.out.println("World: " + Thread.currentThread().getName()); + return message + " World"; + }); + } + public static void main(String[] args) throws InterruptedException, ExecutionException { /** * CompletableFuture */ - // ThreadPool 을 직접 생성하여 작업 - ExecutorService executorService = Executors.newFixedThreadPool(4); - CompletableFuture future = CompletableFuture.supplyAsync(() -> { - System.out.println("Hello: " + Thread.currentThread().getName()); - return "world"; - }, executorService).thenAcceptAsync(s -> { - System.out.println(Thread.currentThread().getName()); - System.out.println(s.toUpperCase()); - }, executorService); + // exception + boolean throwError = true; - future.get(); - executorService.shutdown(); + CompletableFuture hello = CompletableFuture.supplyAsync(() -> { + if (throwError) { + throw new IllegalArgumentException(); + } + System.out.println("Hello: " + Thread.currentThread().getName()); + return "Hello"; + }).handle((result, error) -> { + if (error != null) { + System.out.println(error); + return "Error!"; + } + return result; + }); + + System.out.println(hello.get()); + +// CompletableFuture hello = CompletableFuture.supplyAsync(() -> { +// if (throwError) { +// throw new IllegalArgumentException(); +// } +// System.out.println("Hello: " + Thread.currentThread().getName()); +// return "Hello"; +// }).exceptionally(ex -> { +// System.out.println(ex); +// return "Error!"; +// }); +// +// System.out.println(hello.get()); + + + +// // Compose - 연관관계가 없을 때 +// CompletableFuture hello = CompletableFuture.supplyAsync(() -> { +// System.out.println("Hello: " + Thread.currentThread().getName()); +// return "Hello"; +// }); +// +// CompletableFuture world = CompletableFuture.supplyAsync(() -> { +// System.out.println("World: " + Thread.currentThread().getName()); +// return "World"; +// }); +// +// CompletableFuture future = hello.thenCombine(world, (h, w) -> { +// return h + " " + world; +// }); +// +// System.out.println(future.get()); +// +// // allOf : 2개 이상일 때 모든 결과를 받아서 처리 +// List> futures = Arrays.asList(hello, world); +// CompletableFuture futureArray = CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])); +// +// CompletableFuture> results = CompletableFuture.allOf(futureArray) +// .thenApply(v -> { +// return futures.stream() +// .map(CompletableFuture::join) +// .collect(Collectors.toList()); +// }); +// +// results.get().forEach(System.out::println); +// +// // anyOf : 결과 하나 +// CompletableFuture anyFuture = CompletableFuture.anyOf(hello, world).thenAccept(System.out::println); +// future.get(); + + +// // Compose - 연관관계가 있을 때 +// CompletableFuture hello = CompletableFuture.supplyAsync(() -> { +// System.out.println("Hello: " + Thread.currentThread().getName()); +// return "Hello"; +// }); +// +// CompletableFuture future = hello.thenCompose(App::getWorld); +// +// System.out.println(future.get()); + + +// // ThreadPool 을 직접 생성하여 작업 +// ExecutorService executorService = Executors.newFixedThreadPool(4); +// CompletableFuture future = CompletableFuture.supplyAsync(() -> { +// System.out.println("Hello: " + Thread.currentThread().getName()); +// return "world"; +// }, executorService).thenAcceptAsync(s -> { +// System.out.println(Thread.currentThread().getName()); +// System.out.println(s.toUpperCase()); +// }, executorService); +// +// future.get(); +// executorService.shutdown(); // // Callback - 리턴 값을 받아서 수행 만