作为java程序员,new关键字每天都会使用,是时候真正的了解它了.这里写个笔记,以备时习之.
这里使用的JDK版本为:
openjdk version "1.8.0_262"复制代码
1.字节码指令
new:创建有一个对象,并将其引用值压入栈顶. dup:复制栈顶的数值,并将复制的数值压入栈顶. invokespecial:以栈顶的reference类型的数据所指向的对象作为方法接收者,调用此对象的超类构造方法、实例初始化方法或私有方法. astore_1:将栈顶引用型数值存入第二个本地变量. pop:将栈顶数值弹出(数值不能是long或double类型)复制代码
2.示例代码
public class Main {public static void main(String[] args) { Main ref = new Main(); } }复制代码
3.字节码解析
先将上面的代码通过javac Main.java编译成.class文件,再通过javap -c -verbose Main.class对.class文件进行反汇编,反汇编之后的字节码如下:
public class com.jsonz.jvm.Main minor version: 0 major version: 52 flags: ACC_PUBLIC, ACC_SUPERConstant pool: #1 = Methodref #4.#13 // java/lang/Object."<init>":()V #2 = Class #14 // com/jsonz/jvm/Main #3 = Methodref #2.#13 // com/jsonz/jvm/Main."<init>":()V #4 = Class #15 // java/lang/Object #5 = Utf8 <init> #6 = Utf8 ()V #7 = Utf8 Code #8 = Utf8 LineNumberTable #9 = Utf8 main #10 = Utf8 ([Ljava/lang/String;)V #11 = Utf8 SourceFile #12 = Utf8 Main.java #13 = NameAndType #5:#6 // "<init>":()V #14 = Utf8 com/jsonz/jvm/Main #15 = Utf8 java/lang/Object { public com.jsonz.jvm.Main(); descriptor: ()V flags: ACC_PUBLIC Code: stack=1, locals=1, args_size=1 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return LineNumberTable: line 12: 0 public static void main(java.lang.String[]); descriptor: ([Ljava/lang/String;)V flags: ACC_PUBLIC, ACC_STATIC Code: stack=2, locals=2, args_size=1 0: new #2 // class com/jsonz/jvm/Main 3: dup 4: invokespecial #3 // Method "<init>":()V 7: astore_1 8: return LineNumberTable: line 14: 0line 15: 8} SourceFile: "Main.java"复制代码
常量池以及编译器自动生成的默认构造函数这里直接略过,只需要关注从38行到42行的内容.从上面可以看出,在源码中的一行简单代码,编译之后需要四条字节码指令来完成工作.
- 38行,通过new指令创建了一个Main对象,并将指向该对象的引用压入栈顶.
- 39行,通过dup指令复制了一份栈顶的引用,并将复制的引用也压入栈顶.此时栈中存在两个指向同一个Main对象的引用,如下图所示.其中Main引用2为dup指令复制的引用.
3. 40行,invokespecial指令会消耗掉栈顶的Main引用2引用,将其所指向的对象作为方法接收者,来调用<init>方法,对对象进行初始化.在invokespecial指令执行完成之后,此时栈中就只会存在Main引用1了.
4. 41行,astore_1指令将栈顶的Main引用1存入第二个本地变量(Main方法的第一个本地变量槽会被默认占用,不知道被什么东西占用了).
上面四条指令完成之后,就可以通过ref引用变量来使用new出来的Main对象了.
4.只创建对象,不赋值.
在测试的过程中,还试了下如果只是new一个对象,而不进行赋值,编译器会怎么处理呢? 代码如下:
public class Main {public static void main(String[] args) {new Main(); } }复制代码
字节码如下:
public static void main(java.lang.String[]); descriptor: ([Ljava/lang/String;)V flags: ACC_PUBLIC, ACC_STATIC Code: stack=2, locals=1, args_size=1 0: new #2 // class com/jsonz/jvm/Main 3: dup 4: invokespecial #3 // Method "<init>":()V 7: pop 8: return LineNumberTable: line 14: 0line 15: 8复制代码
从上面可以看出,除了最后的pop指令之外,其他都是一样的.可以发现编译器还是很聪明的,如果发现你只是创建对象,而不使用的话,会主动把引用从栈中弹出,防止你占着茅坑不拉屎.
5.单例-双重检查的问题
了解过单例模式的开发,应该都知道双重检查锁机制存在一定的风险.这里通过代码进行解析,代码如下:
public class Singleton {private volatile static Singleton instance = null;private Singleton() { }public static Singleton getInstance() {if (instance == null) {synchronized (Singleton.class) {if (instance == null) { instance = new Singleton(); } } }return instance; } }复制代码
要了解这种机制的风险,需要从字节码的层面去进行分析,字节码如下:
public class com.jsonz.jvm.Singleton minor version: 0 major version: 52 flags: ACC_PUBLIC, ACC_SUPERConstant pool: #1 = Methodref #5.#20 // java/lang/Object."<init>":()V #2 = Fieldref #3.#21 // com/jsonz/jvm/Singleton.instance:Lcom/jsonz/jvm/Singleton; #3 = Class #22 // com/jsonz/jvm/Singleton #4 = Methodref #3.#20 // com/jsonz/jvm/Singleton."<init>":()V #5 = Class #23 // java/lang/Object #6 = Utf8 instance #7 = Utf8 Lcom/jsonz/jvm/Singleton; #8 = Utf8 <init> #9 = Utf8 ()V #10 = Utf8 Code #11 = Utf8 LineNumberTable #12 = Utf8 getInstance #13 = Utf8 ()Lcom/jsonz/jvm/Singleton; #14 = Utf8 StackMapTable #15 = Class #23 // java/lang/Object #16 = Class #24 // java/lang/Throwable #17 = Utf8 <clinit> #18 = Utf8 SourceFile #19 = Utf8 Singleton.java #20 = NameAndType #8:#9 // "<init>":()V #21 = NameAndType #6:#7 // instance:Lcom/jsonz/jvm/Singleton; #22 = Utf8 com/jsonz/jvm/Singleton #23 = Utf8 java/lang/Object #24 = Utf8 java/lang/Throwable { public static com.jsonz.jvm.Singleton getInstance(); descriptor: ()Lcom/jsonz/jvm/Singleton; flags: ACC_PUBLIC, ACC_STATIC Code: stack=2, locals=2, args_size=0 0: getstatic #2 // Field instance:Lcom/jsonz/jvm/Singleton; 3: ifnonnull 37 6: ldc #3 // class com/jsonz/jvm/Singleton 8: dup 9: astore_010: monitorenter11: getstatic #2 // Field instance:Lcom/jsonz/jvm/Singleton;14: ifnonnull 2717: new #3 // class com/jsonz/jvm/Singleton20: dup21: invokespecial #4 // Method "<init>":()V24: putstatic #2 // Field instance:Lcom/jsonz/jvm/Singleton;27: aload_028: monitorexit29: goto 3732: astore_133: aload_034: monitorexit35: aload_136: athrow37: getstatic #2 // Field instance:Lcom/jsonz/jvm/Singleton;40: areturn Exception table: from to target type11 29 32 any32 35 32 any LineNumberTable: line 19: 0line 20: 6line 21: 11line 22: 17line 24: 27line 27: 37 StackMapTable: number_of_entries = 3frame_type = 252 /* append */ offset_delta = 27 locals = [ class java/lang/Object ]frame_type = 68 /* same_locals_1_stack_item */ stack = [ class java/lang/Throwable ]frame_type = 250 /* chop */ offset_delta = 4 static {}; descriptor: ()V flags: ACC_STATIC Code: stack=1, locals=0, args_size=0 0: aconst_null 1: putstatic #2 // Field instance:Lcom/jsonz/jvm/Singleton; 4: return LineNumberTable: line 13: 0} SourceFile: "Singleton.java"复制代码
问题出在46和47行,因为invokespecial和putstatic指令符合as-if-serial语义,因此在运行时这两条指令的执行顺序可能是随机的.举个例子,假设有两个线程,线程1执行到46行,由于指令重排序,导致putstatic指令先被执行,那么此时静态变量instance可能就不为null了(这里之所以说可能,是因为线程1可能没有将引用值从工作内存刷新到堆内存中去),如果这个时候有第二个线程(线程2)进入方法拿到不为null的instance引用,就会绕过null判断,直接返回.此时用户拿到的引用所指向的对象可能还没有执行invokespecial指令来进行初始化,用户在之后对该对象的操作都是危险的.因此,这里最好的办法就是用volatile对instance变量进行修饰.