인프런 강의 "더 자바, JAVA8"(백기선님)의 강의를 듣고 정리한 글 입니다.
JAVA8에 추가된 핵심 기능들을 이해하기 쉽게 설명해 주시니 한번씩 들어보시는 것을 추천드립니다.
자바에서 비동기 (Asynchronous) 프로그래밍을 가능케하는 인터페이스.
public class App {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(4);
Future<String> future = executorService.submit(() -> "hello");
// todo
future.get();
// future 에서 가져온 값을 가지고 무언가를 하는 작업은 future.get() 이후에 나와야 한다.
}
}
public class App {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<String> future = new CompletableFuture<>();
future.complete("jay"); // 명시적으로 값을 주는 방법
String result = future.get();
System.out.println("result = " + result); // result = jay
CompletableFuture<String> future2 = CompletableFuture.completedFuture("jay");
System.out.println(future2.get()); // jay
}
}
public class App {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
System.out.println("Thread : " + Thread.currentThread().getName());
});
future.get();
}
}
public class App {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
System.out.println("Thread : " + Thread.currentThread().getName());
return "hi";
});
future.get();
}
}
public class App {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
System.out.println("Thread : " + Thread.currentThread().getName());
return "hi";
}).thenApply(String::toUpperCase);
System.out.println(future.get());
}
}
public class App {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
System.out.println("Thread : " + Thread.currentThread().getName());
return "hi";
}).thenAccept((s) -> {
System.out.println();
});
System.out.println(future.get());
}
}
public class App {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<String> hello = CompletableFuture.supplyAsync(() -> {
System.out.println("hello" + Thread.currentThread().getName());
return "hello";
});
CompletableFuture<String> future = hello.thenCompose(App::world);
String s = future.get();
System.out.println("s = " + s); // s = helloworld
}
public static CompletableFuture<String> world (String s) {
return CompletableFuture.supplyAsync(() -> {
System.out.println(s + "world" + Thread.currentThread().getName());
return s + "world";
});
}
}
public class App {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<String> hello = CompletableFuture.supplyAsync(() -> {
System.out.println("hello" + Thread.currentThread().getName());
return "hello";
});
CompletableFuture<String> world = CompletableFuture.supplyAsync(() -> {
System.out.println("world" + Thread.currentThread().getName());
return "world";
});
CompletableFuture<String> future = hello.thenCombine(world, (h, w) -> {
return h + w;
});
System.out.println(future.get()); // helloworld
}
}
public class App {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<String> hello = CompletableFuture.supplyAsync(() -> {
if (true) {
throw new IllegalArgumentException();
}
System.out.println("hello" + Thread.currentThread().getName());
return "hello";
}).exceptionally(ex -> {
return "error";
});
System.out.println("hello = " + hello.get()); // hello = error
}
}
public class App {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<String> hello = CompletableFuture.supplyAsync(() -> {
if (true) {
throw new IllegalArgumentException();
}
System.out.println("hello" + Thread.currentThread().getName());
return "hello";
}).handle((result, ex) -> { // 정상동작, 에러 동작 핸들링
if (ex != null) {
System.out.println("error");
return "error";
}
return result;
});
System.out.println("hello = " + hello.get()); // hello = error
}
}