java 双层锁
public class LogUtil {
//私有化构造方法
private LogUtil() {
}
private volatile static LogUtil instance;
public static LogUtil getInstance() {
if (instance == null) {
synchronized (LogUtil.class) {
if (instance == null) {
instance = new LogUtil();
}
}
}
return instance;
}
}
kotlin 静态加载 优点 简单 线程安全
class LogUtil private constructor(){
companion object{
fun getInstance()=Holder.instance
}
private object Holder{
var instance=LogUtil()
}
}