/** * 匿名内部类和lambada的区别 */ @Test public void test1() throws InterruptedException { new Thread(new Runnable() { public void run() { String name = Thread.currentThread().getName(); System.err.println(name + " is run as a thread "); } }).start(); Thread.sleep(2000);
Compiled from "LambadaTest1.java" public class com.jdk.lambada.LambadaTest1 { public com.jdk.lambada.LambadaTest1(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return LineNumberTable: line 11: 0 LocalVariableTable: Start Length Slot Name Signature 0 5 0 this Lcom/jdk/lambada/LambadaTest1;
public void test1() throws java.lang.InterruptedException; Code: 0: new #2 // class java/lang/Thread 3: dup 4: new #3 // class com/jdk/lambada/LambadaTest1$1 7: dup 8: aload_0 9: invokespecial #4 // Method com/jdk/lambada/LambadaTest1$1."<init>":(Lcom/jdk/lambada/LambadaTest1;)V 12: invokespecial #5 // Method java/lang/Thread."<init>":(Ljava/lang/Runnable;)V 15: invokevirtual #6 // Method java/lang/Thread.start:()V 18: ldc2_w #7 // long 2000l 21: invokestatic #9 // Method java/lang/Thread.sleep:(J)V 24: return LineNumberTable: line 19: 0 line 24: 15 line 25: 18 line 27: 24 LocalVariableTable: Start Length Slot Name Signature 0 25 0 this Lcom/jdk/lambada/LambadaTest1; }
/** * @program: lambada * @description: 测试jdk1.8 * @author: zhaoxudong * @create: 2020-05-05 11:42 **/ public class Test {
public static void main(String[] args) throws InterruptedException { new Thread(()->{ System.err.println(" test" + Thread.currentThread().getName() + " is run as a thread " ); }).start(); TimeUnit.SECONDS.sleep(5L); } }
同样的,我们先把class文件放到idea下面查看反编译之后到代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) //
import java.util.concurrent.TimeUnit;
public class Test { public Test() { }
public static void main(String[] args) throws InterruptedException { (new Thread(() -> { System.err.println(" test" + Thread.currentThread().getName() + " is run as a thread "); })).start(); TimeUnit.SECONDS.sleep(5L); } }
public class Test { public Test(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return LineNumberTable: line 9: 0 LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LTest;
public static void main(java.lang.String[]) throws java.lang.InterruptedException; Code: 0: new #2 // class java/lang/Thread 3: dup 4: invokedynamic #3, 0 // InvokeDynamic #0:run: ()Ljava/lang/Runnable; 9: invokespecial #4 // Method java/lang/Thread."<init>":(Ljava/lang/Runnable;)V 12: invokevirtual #5 // Method java/lang/Thread.start:()V 15: getstatic #6 // Field java/util/concurrent/TimeUnit.SECONDS:Ljava/util/concurrent/TimeUnit; 18: ldc2_w #7 // long 5l 21: invokevirtual #9 // Method java/util/concurrent/TimeUnit.sleep:(J)V 24: return LineNumberTable: line 12: 0 line 14: 12 line 15: 15 line 16: 24 LocalVariableTable: Start Length Slot Name Signature 0 25 0 args [Ljava/lang/String;
private static void lambda$main$0(); Code: 0: getstatic #10 // Field java/lang/System.err:Ljava/io/PrintStream; 3: new #11 // class java/lang/StringBuilder 6: dup 7: invokespecial #12 // Method java/lang/StringBuilder."<init>":()V 10: ldc #13 // String test 12: invokevirtual #14 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 15: invokestatic #15 // Method java/lang/Thread.currentThread:()Ljava/lang/Thread; 18: invokevirtual #16 // Method java/lang/Thread.getName:()Ljava/lang/String; 21: invokevirtual #14 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 24: ldc #17 // String is run as a thread 26: invokevirtual #14 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 29: invokevirtual #18 // Method java/lang/StringBuilder.toString:()Ljava/lang/String; 32: invokevirtual #19 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 35: return LineNumberTable: line 13: 0 line 14: 35 }
public static void main(String[] args) throws InterruptedException { new Thread(()->{ System.err.println(" test" + Thread.currentThread().getName() + " is run as a thread " ); System.err.println("lambada this is = " + this); }).start(); TimeUnit.SECONDS.sleep(5L); }
这里惊讶的发现,编译居然无法通过!!!!
我们再对比使用匿名内部类的方式:
1 2 3 4 5 6 7 8 9
public static void main(String[] args) throws InterruptedException { new Thread(new Runnable() { public void run() { String name = Thread.currentThread().getName(); System.err.println(name + " is run as a thread " + this); } }).start(); Thread.sleep(2000); }
/** * @program: smartframwork * @description: lambada表达式学习1 * @author: zhaoxudong * @create: 2020-05-05 10:29 **/ public class LambadaTest1 {
public static void main(String[] args) throws InterruptedException { new Thread(new Runnable() { public void run() { String name = Thread.currentThread().getName(); System.err.println(name + " is run as a thread " + this); }