多线程下的单例模式+反汇编

多线程下的单例模式的实现,顺便做了反汇编。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public class MySingleton {
    private static MySingleton INSTANCE;

    private MySingleton() {
    }

    public static MySingleton getInstance() {
        if (INSTANCE == null) {
            synchronized (MySingleton.class) {
                INSTANCE = new MySingleton();
            }
        }
        return INSTANCE;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Compiled from "MySingleton.java"
public class MySingleton {
  public static MySingleton getInstance();
    Code:
       0: getstatic     #2                  // Field INSTANCE:LMySingleton; //+获得类的指定域,并压入栈顶
       3: ifnonnull     32					//+不为null时跳转到行号32
       6: ldc_w         #3                  // class MySingleton //+常量值从常量池中推送至栈顶(宽索引),推送的为地址
       9: dup  								//+复制栈顶数值,并且复制值进栈         
      10: astore_0      					//+将栈顶数值(objectref)存入当前          frame的局部变量数组中指定下标(index)处的变量中,栈顶数值出栈。这里存的是MySingleton类定义的地址
      11: monitorenter  					//+获得对象锁即MySingleton地址
      12: new           #3                  // class MySingleton //+创建一个对象,并且其引用进栈
      15: dup           					//+复制栈顶数值,并且复制值进栈
      16: invokespecial #4                  // Method "<init>":()V //+调用超类构造方法、实例初始化方法、私有方法
      19: putstatic     #2                  // Field INSTANCE:LMySingleton; //+为指定的类的静态域赋值
      22: aload_0       					//+当前frame的局部变量数组中下标为           index的引用型局部变量进栈,这里是MySingleton类定义的地址
      23: monitorexit   					//+释放对象锁
      24: goto          32					//+跳转到行号32
      27: astore_1      					//+将栈顶数值(objectref)存入当前          frame的局部变量数组中指定下标(index)处的变量中,栈顶数值出栈。
      28: aload_0       					//+当前frame的局部变量数组中下标为           0的引用型局部变量进栈
      29: monitorexit   					//+//+释放对象锁
      30: aload_1       					//+当前frame的局部变量数组中下标为           1的引用型局部变量进栈
      31: athrow        					//+将栈顶的数值作为异常或错误抛出
      32: getstatic     #2                  // Field INSTANCE:LMySingleton; //+获得类的指定域,并压入栈顶
      35: areturn       					//+从方法中返回一个对象的引用
    Exception table:
       from    to  target type
          12    24    27   any
          27    30    27   any
}

Comments

comments powered by Disqus