Link Search Menu Expand Document

기본 개념

  • java8에서 추가 되었습니다.
  • Lambda expressions(람다 표현식)이란 익명 함수를 지칭한다.
  • java에서 함수형 인터페이스(method가 하나 있는 인터페이스)는 람다 표현식으로 사용할수 있다.
  • 식별자 없이 실행 가능한 함수

Thread에서 Runable을 람다식으로 사용한 예

// 메서드가 하나인 함수형 인터페이스 Runable
@FunctionalInterface
public interface Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run();
}
  • Runable interface는 함수형 인터페이스로 run이란 하나의 메서드를 가지고 있습니다.
// Thread의 생성자
public Thread(Runnable target) {
    init(null, target, "Thread-" + nextThreadNum(), 0);
}
  • Thread의 생성자에서 Runable을 파라메터로 받고 있기 때문에 람다식을 대입할 수 있습니다.
// Thread의 Lambda expression 사용
new Thread(()->{
	System.out.println("Hello World.");
}).start();
  • 함수형 인터페이스인 Runable이 익명 함수로 사용되어 람다식이 된 모습입니다.

자바에서 기본적으로 사용되는 함수형 인터페이스

  • @FunctionalInterface 어노테이션은 함수형 인터페이스를 나타냅니다.

참고링크