标签:public 安全 饿汉 == 懒汉 tin 懒汉模式 线程 zed
//1.懒汉模式 public class SingletonDemo { private static SingletonDemo instance; private SingletonDemo(){ } public static SingletonDemo getInstance(){ if(instance==null){ instance=new SingletonDemo(); } return instance; } } //2. 线程安全的懒汉模式 public class SingletonDemo { private static SingletonDemo instance; private SingletonDemo(){ } public static synchronized SingletonDemo getInstance(){ if(instance==null){ instance=new SingletonDemo(); } return instance; } } //3.饿汉 public class SingletonDemo { private static SingletonDemo instance=new SingletonDemo(); private SingletonDemo(){ } public static SingletonDemo getInstance(){ return instance; } }
标签:public 安全 饿汉 == 懒汉 tin 懒汉模式 线程 zed
原文地址:https://www.cnblogs.com/WPF0414/p/10098560.html