精选圈子榜单优站
编程综合
编程综合
技术
20关注
编程技术记录、分享 ,记录你的编程生活点点滴滴!

Kotlin 单例模式


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()
    }
}



  • 若文章侵犯了您的权益,请联系我们进行处理。

  • 2017-07-11
  • 1903阅读
评论