美文网首页
雪落千寒 隐藏在风雪中的宫殿

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

作者: 李汪汪汪侠 | 来源:发表于2017-08-13 00:34 被阅读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

    相关文章

      网友评论

          本文标题:雪落千寒 隐藏在风雪中的宫殿

          本文链接:https://www.haomeiwen.com/subject/ydrsrxtx.html