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