싱글톤
목적
구현 방법
public class Settings{ private static final Settings INSTANCE = new Settings(); private Settings(){} public static Settings getInstance() { return INSTANCE; } } //byte code private final static Lstudy/Settings; INSTANCE // access flags 0x2 private <init>()V L0 LINENUMBER 7 L0 ALOAD 0 INVOKESPECIAL java/lang/Object.<init> ()V RETURN L1 LOCALVARIABLE this Lstudy/Settings; L0 L1 0 MAXSTACK = 1 MAXLOCALS = 1 // access flags 0x9 public static getInstance()Lstudy/Settings; L0 LINENUMBER 10 L0 GETSTATIC study/Settings.INSTANCE : Lstudy/Settings; ARETURN MAXSTACK = 1 MAXLOCALS = 0 // access flags 0x8 static <clinit>()V L0 LINENUMBER 5 L0 NEW study/Settings DUP INVOKESPECIAL study/Settings.<init> ()V PUTSTATIC study/Settings.INSTANCE : Lstudy/Settings; RETURN MAXSTACK = 2 MAXLOCALS = 0public class Settings{ private static Settings instance; private Settings(){} public static Settings getInstance() { if(instance == null){ instance = new Settings(); } return instance; } } // public class study/Settings { // compiled from: Settings.java // access flags 0xA private static Lstudy/Settings; instance // access flags 0x2 private <init>()V L0 LINENUMBER 7 L0 ALOAD 0 INVOKESPECIAL java/lang/Object.<init> ()V RETURN L1 LOCALVARIABLE this Lstudy/Settings; L0 L1 0 MAXSTACK = 1 MAXLOCALS = 1 // access flags 0x9 public static getInstance()Lstudy/Settings; L0 LINENUMBER 10 L0 GETSTATIC study/Settings.instance : Lstudy/Settings; IFNONNULL L1 L2 LINENUMBER 11 L2 NEW study/Settings DUP INVOKESPECIAL study/Settings.<init> ()V PUTSTATIC study/Settings.instance : Lstudy/Settings; L1 LINENUMBER 13 L1 FRAME SAME GETSTATIC study/Settings.instance : Lstudy/Settings; ARETURN MAXSTACK = 2 MAXLOCALS = 0 }public class Settings{ private static Settings instance; private Settings(){} public static synchronized Settings getInstance() { if(instance == null){ instance = new Settings(); } return instance; } } //byte code public class study/Settings { // compiled from: Settings.java // access flags 0xA private static Lstudy/Settings; instance // access flags 0x2 private <init>()V L0 LINENUMBER 7 L0 ALOAD 0 INVOKESPECIAL java/lang/Object.<init> ()V RETURN L1 LOCALVARIABLE this Lstudy/Settings; L0 L1 0 MAXSTACK = 1 MAXLOCALS = 1 // access flags 0x29 public static synchronized getInstance()Lstudy/Settings; L0 LINENUMBER 10 L0 GETSTATIC study/Settings.instance : Lstudy/Settings; IFNONNULL L1 L2 LINENUMBER 11 L2 NEW study/Settings DUP INVOKESPECIAL study/Settings.<init> ()V PUTSTATIC study/Settings.instance : Lstudy/Settings; L1 LINENUMBER 13 L1 FRAME SAME GETSTATIC study/Settings.instance : Lstudy/Settings; ARETURN MAXSTACK = 2 MAXLOCALS = 0 }public class Settings{ private static volatile Settings instance; private Settings(){} public static Settings getInstance() { if(instance == null){ synchronized ( Settings.class ){ if(instance == null){ instance = new Settings(); } } } return instance; } } //byte code public class study/Settings { // compiled from: Settings.java // access flags 0x4A private static volatile Lstudy/Settings; instance // access flags 0x2 private <init>()V L0 LINENUMBER 7 L0 ALOAD 0 INVOKESPECIAL java/lang/Object.<init> ()V RETURN L1 LOCALVARIABLE this Lstudy/Settings; L0 L1 0 MAXSTACK = 1 MAXLOCALS = 1 // access flags 0x9 public static getInstance()Lstudy/Settings; TRYCATCHBLOCK L0 L1 L2 null TRYCATCHBLOCK L2 L3 L2 null L4 LINENUMBER 10 L4 GETSTATIC study/Settings.instance : Lstudy/Settings; IFNONNULL L5 L6 LINENUMBER 11 L6 LDC Lstudy/Settings;.class DUP ASTORE 0 MONITORENTER L0 LINENUMBER 12 L0 GETSTATIC study/Settings.instance : Lstudy/Settings; IFNONNULL L7 L8 LINENUMBER 13 L8 NEW study/Settings DUP INVOKESPECIAL study/Settings.<init> ()V PUTSTATIC study/Settings.instance : Lstudy/Settings; L7 LINENUMBER 15 L7 FRAME APPEND [java/lang/Object] ALOAD 0 MONITOREXIT L1 GOTO L5 L2 FRAME SAME1 java/lang/Throwable ASTORE 1 ALOAD 0 MONITOREXIT L3 ALOAD 1 ATHROW L5 LINENUMBER 17 L5 FRAME CHOP 1 GETSTATIC study/Settings.instance : Lstudy/Settings; ARETURN MAXSTACK = 2 MAXLOCALS = 2 }public static Settings getInstance() { if (instance == null) { Class var0 = Settings.class; synchronized(Settings.class) { if (instance == null) { instance = new Settings(); } } } return instance; }public class Settings{ private static Settings instance; private Settings(){} private static class SettingsHolder { private static final Settings INSTANCE = new Settings(); } public static synchronized Settings getInstance() { return SettingsHolder.INSTANCE; } } //bytecode public class study/Settings { // compiled from: Settings4.java NESTMEMBER study/Settings$SettingsHolder // access flags 0xA private static INNERCLASS study/Settings$SettingsHolder study/Settings SettingsHolder // access flags 0x2 private <init>()V L0 LINENUMBER 8 L0 ALOAD 0 INVOKESPECIAL java/lang/Object.<init> ()V RETURN L1 LOCALVARIABLE this Lstudy/Settings; L0 L1 0 MAXSTACK = 1 MAXLOCALS = 1 // access flags 0x9 public static getInstance()Lstudy/Settings; L0 LINENUMBER 15 L0 GETSTATIC study/Settings$SettingsHolder.INSTANCE : Lstudy/Settings; ARETURN MAXSTACK = 1 MAXLOCALS = 0 }
+) 자바의 동기화
싱글톤이 깨지는 경우
1. Reflection
2. Serialize / DeSerialize
Last updated