Functional interface acts as a type for lambda expression and can be used to invoke lambda expression. java.lang.Runnable is a functional interface as it has only one abstract method, public void run(). We’ll see an example to create Thread without lambda and then with the use of lambda expression.
public class RunnableExampleWithoutLambda {
public static void main(String args[]) {
// Implementing Runnable using anonymous class
Runnable runnable1 = new Runnable() {
@Override
public void run() {
System.out.println("Runnable not using lambda " + Thread.currentThread().getName());
}
};
System.out.println(Thread.currentThread().getName() + " started");
Thread thread1 = new Thread(runnable1);
thread1.start();
// Implementing Runnable using lambda
Runnable runnable2 = () -> System.out.println("Runnable using lambda " + Thread.currentThread().getName());
Thread thread2 = new Thread(runnable2);
thread2.start();
}
}
Output
main started
Runnable not using lambda Thread-0
Runnable using lambda Thread-1
