首页 > TAG信息列表 > SingletonHolder
Kotlin-单例模式
双重校验锁式(Double Check) //Java实现 public class SingletonDemo { private volatile static SingletonDemo instance; private SingletonDemo(){} public static SingletonDemo getInstance(){ if(instance==null){ synchronized (Sing单例模式最终推荐写法-线程安全
最终推荐写法 public class Singleton{ private static class SingletonHolder{ public static Singleton instance = new Singleton(); } private Singleton(){} //访问静态内部类的静态字段 从而触发类的加载 public static Singlet设置模式之 -> 单例模式在序列化时注意的问题
package com.dp; public class Singleton implements java.io.Serializable{ private static final long serialVersionUID = 2741243454530543604L; private Singleton() { // TODO Auto-generated constructor stub } private static class SingletonHolder {