雪落千寒 隐藏在风雪中的宫殿

2017-08-13  本文已影响0人  李汪汪汪侠

Kotlin日常

给单例模式上锁

//java
public class LazyNotThreadSafe1 {
    private static  LazyNotThreadSafe1 INSTANCE;
    private LazyNotThreadSafe1(){}
    public static synchronized LazyNotThreadSafe1 getINSTANCE(){
        if (INSTANCE == null){
            INSTANCE = new LazyNotThreadSafe1();
        }
        return INSTANCE;
    }
}
//kotlin
class LazyNotThreadSafe{

    companion object {
        val instance by lazy { LazyThreadSafetyMode.NONE }
    }

    //或者
    private var instance2:LazyNotThreadSafe? = null

    @Synchronized
    fun get():LazyNotThreadSafe{
        if (instance2 == null){
            instance2 =  LazyNotThreadSafe()
        }
        return instance2!!
    }

}

Over 事到如今 终于让自己属于我自己

哈哈哈.png
上一篇下一篇

猜你喜欢

热点阅读