public class Singleton {
private static Singleton instance;
private static Object syncRoot = new Object();
private Singleton() {}
public static Singleton getInstance(){
if(instance == null){
synchronized (syncRoot) {
if(instance == null){
instance = new Singleton();
}
}
}
return instance;
}
线程安全下的单例类 ,双重锁定哦
本文介绍了一种使用双重检查锁定实现的线程安全单例模式。该模式通过同步对象syncRoot确保实例创建过程中的线程安全性,同时避免了每次获取实例时都进行同步操作带来的性能开销。
23

被折叠的 条评论
为什么被折叠?



