본문 바로가기

WEB개발/JAVA

람다 Lambda

람다  형태매칭 인터페이스메서드
() -> { ... } Runnable void run()
() -> 값 Supplier T get()
() -> 값 Callable T call()
(x) -> {} Consumer void accept(T)
(x) -> 값 Function<T,R> R apply(T)
(a,b) -> 값 BiFunction<T,U,R> R apply(T,U)
(a,b) -> {} BiConsumer<T,U> void accept(T,U)
(t) -> true/false Predicate boolean test(T)

 

predicate example

public class Main {
    public static void main(String[] args) throws InterruptedException {
        check(5, t-> t>10);
    }

    public static void check(int value, Predicate<Integer> predicate) {
        if(predicate.test(value)) {
            System.out.println("SUCCESS");
        } else {
            System.out.println("FAIL");
        }
    }
}

 

Supplier vs Callable 차이

구분  Supplier Callable
패키지 java.util.function java.util.concurrent
메서드 T get() T call() throws Exception
예외 ❌ throws 불가 ⭕ checked exception 가능
주 용도 값 “제공자” 비동기 작업
Executor 사용 ❌ 직접 submit 불가 ⭕ ExecutorService.submit()
병렬 실행 목적 ❌ 없음 ⭕ 있음

 

=> Callable은 비동기 실행 전용 계약이라
예외 처리, Future, 취소, 타임아웃까지 지원함.

Supplier는 그냥 “함수”라 쓰레드 컨트롤 개념이 없음.

 

'WEB개발 > JAVA' 카테고리의 다른 글

java Stream API  (0) 2026.01.12
Checked Exception, Unchecked Exception (Runtime Exception)  (0) 2025.12.08
[JAVA - NIO] New Input/Output  (0) 2025.12.04
Daemon Thread vs User Thread  (0) 2025.02.21
CyclicBarrier, CountDownLatch  (0) 2025.02.21