标签:let 延迟 vat sync null span 单例 class tin
1、饿汉式单例模式
//饿汉式单例 public class Singleton { private Singleton(){} private Singleton uniqueInstance = new Singleton(); public static Singleton getInstance(){ return uniqueInstance; } }
2、懒汉式单例模式
//懒汉式单例,延迟实例化 public class Singleton { private Singleton uniqueInstance; private Singleton(){} public static Singleton getInstance(){ if(uniqueInstance==null){ uniqueInstance = new Singleton(); } return uniqueInstance; } }
3、双重加锁单例模式
//双重加锁,应对多线程 public class Singleton { private volatile static Singelton uniqueInstance; private Singleton(){} public static Singleton getInstance(){ if(uniqueInstance==null){ synchronized (Singleton.class){ if(uniqueInstance==null){ uniqueInstance = new Singleton(); } } } return uniqueInstance; } }
标签:let 延迟 vat sync null span 单例 class tin
原文地址:https://www.cnblogs.com/xiaoxli/p/9560648.html