1️⃣ Callable vs. Runnable 비교

특징 Runnable Callable
메서드 이름 run() call()
반환값 없음 (void) 있음 (제네릭 사용 가능)
예외 처리 가능 여부 불가능 가능 (throws Exception)
Future 사용 가능 여부 불가능 가능

언제 사용???

2️⃣ CompletableFuture vs Future

둘 다 비동기 작업을 처리하는 인터페이스이지만, CompletableFutureFuture의 기능을 확장한 더 강력한 API이다.

Future

단점

  1. 결과를 기다려야 함 (get()이 블로킹됨)
  2. 콜백 기능 없음
  3. 여러 개의 Future를 조합할 수 없음
  4. 비동기 처리 중에 발상해는 예외를 처리하기 어려움
import java.util.concurrent.*;

public class FutureExample {
    public static void main(String[] args) {
        ExecutorService executor = Executors.newSingleThreadExecutor();

        Future<String> future = executor.submit(() -> {
            Thread.sleep(2000);
        });

        try {
            String result = future.get(); // 결과를 기다려야 함 (블로킹)
            System.out.println(result);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }

        executor.shutdown();
    }
}

CompletableFuture

CompletableFutureFuture의 단점을 보완하여 비동기 작업을 더욱 유연하게 처리할 수 있다.

CompletableFuture의 장점