精选圈子榜单优站
跟我一起AI炒ETF
赚大钱赚大钱赚大钱

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



  • 若文章侵犯了您的权益,请联系站长处理:nemo@link-nemo.com

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