Java 8 Lambda表达式-接口实现
Java 8 Lambda表达式在只有一个方法的接口实现代码编写中,可以起到简化作用:
(argument list) -> body
具体看Runnable接口的例子
public class RunnableLambdaTest { public static void main(String[] args) { System.out.println(Thread.currentThread().getName() + ": RunnableTest"); //java 8之前的匿名类方法 Runnable task1 = new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName() + " is running"); } }; Thread thread2 = new Thread(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName() + " is running"); } }); //Lambda表达式编写Runnable实现类 Runnable task3 = () -> { System.out.println(Thread.currentThread().getName() + " is running"); }; Thread thread4 = new Thread(() -> { System.out.println(Thread.currentThread().getName() + " is running"); }); Thread thread1 = new Thread(task1); thread1.start(); thread2.start(); thread4.start(); new Thread(task3).start(); } }
https://www.codejava.net/java-core/the-java-language/java-8-lambda-runnable-example