单例模式双重校验锁

/*
*
* 单例模式双重校验锁
*
* */
public class SingleModel {
	//创建 SingleModel 的一个对象
	private static volatile SingleModel instance;

	// 让构造函数为 private,这样该类就不会被实例化
	private SingleModel() {
	}

	// 获取唯一可用的对象
	public static SingleModel getInstance() {
		if (instance == null){
			synchronized (SingleModel.class){
				if (instance == null){
					instance = new SingleModel();
				}
			}
		}
		return instance;
	}

	public void useMessage() {
		System.out.println("Single Model!");
	}
}

猜你喜欢

转载自blog.csdn.net/qq_41520636/article/details/115028339