lambda表达式
- 普通的匿名内部类在编译后都会生成额外的class文件。
public static void main(String[] args) { Thread thread; thread = new Thread(new Runnable() { @Override public void run() { System.out.println("Hello lambdas."); } }); thread = new Thread(new Runnable() { @Override public void run() { System.out.println("Hello lambdas."); } }); System.out.println("Hello world."); }
- lambda表达式使用invokedynamic, 动态的绑定和解绑一个方法,不会产生任何内部类class文件
public static void main(String[] args) { //invokedynamic: you can attach and detach to the function you want invoke dynamically. //Finally function pointers are available on the jvm. //With using lambdas, no inner classes will be created. Thread thread; thread = new Thread(() -> System.out.println("Hello lambdas")); thread = new Thread(() -> System.out.println("Hello lambdas")); thread = new Thread(() -> System.out.println("Hello lambdas")); thread = new Thread(() -> System.out.println("Hello lambdas")); thread = new Thread(() -> System.out.println("Hello lambdas")); thread = new Thread(() -> System.out.println("Hello lambdas")); thread.start(); System.out.println("Hello world."); }