Kotlin单例
下面介绍一下kotlin 线程安全的几种单例写法。
0x01 饿汉模式
1 2 3 4 5 6 7 8 9 10 11 12
| public final class Singleton { public static final Singleton INSTANCE;
private Singleton() { }
static { Singleton var0 = new Singleton(); INSTANCE = var0; } }
|
0x02 双重校验锁式
双重校验锁式(Double Check)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| class Singleton private constructor() { companion object { @Volatile private var instance: Singleton? = null
@JvmStatic fun getInstance(): Singleton { if (instance == null) { synchronized(Singleton::class.java) { if (instance == null) { instance = Singleton() } } } 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| public final class Singleton { private static volatile Singleton instance; public static final Singleton.Companion Companion = new Singleton.Companion((DefaultConstructorMarker)null);
private Singleton() { }
public Singleton(DefaultConstructorMarker $constructor_marker) { this(); }
@JvmStatic @NotNull public static final Singleton getInstance() { return Companion.getInstance(); }
public static final class Companion { @JvmStatic @NotNull public final Singleton getInstance() { if (Singleton.instance == null) { Class var1 = Singleton.class; boolean var2 = false; boolean var3 = false; synchronized(var1) { int var4 = false; if (Singleton.instance == null) { Singleton.instance = new Singleton((DefaultConstructorMarker)null); }
Unit var6 = Unit.INSTANCE; } }
Singleton var10000 = Singleton.instance; if (var10000 == null) { Intrinsics.throwNpe(); }
return var10000; }
private Companion() { }
public Companion(DefaultConstructorMarker $constructor_marker) { this(); } } }
|
0x03 静态内部类
1 2 3 4 5 6 7 8 9 10 11 12
| public class SingletonDemo { private static class SingletonHolder{ private static SingletonDemo instance=new SingletonDemo(); } private SingletonDemo(){ System.out.println("Singleton has loaded"); } public static SingletonDemo getInstance(){ return SingletonHolder.instance; } }
|
1 2 3 4 5 6 7 8 9 10 11
| class Singleton private constructor() { companion object { @JvmStatic fun getInstance() = SingletonHolder.instance } private object SingletonHolder { @JvmStatic val instance: Singleton = Singleton() } }
|
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 30 31 32 33 34
| public final class Singleton { public static final Singleton.Companion Companion = new Singleton.Companion((DefaultConstructorMarker)null); private Singleton() { } @JvmStatic @NotNull public static final Singleton getInstance() { return Companion.getInstance(); } private static final class SingletonHolder { @NotNull private static final Singleton instance; public static final Singleton.SingletonHolder INSTANCE; @NotNull public static final Singleton getInstance() { return instance; } static { Singleton.SingletonHolder var0 = new Singleton.SingletonHolder(); INSTANCE = var0; instance = new Singleton((DefaultConstructorMarker)null); } } public static final class Companion { @JvmStatic @NotNull public final Singleton getInstance() { return Singleton.SingletonHolder.getInstance(); } private Companion() { } } }
|