单例模式

public class Singleton {
//单例设计模式,禁止指令重排可以加volatile
private static Singleton singleton = null;

private void Singleton() {
System.out.println(Thread.currentThread().getName() + "构造方法执行");
}
//DCL(Double Check lock双端检索机制)
public static Singleton getInstance() {
if (singleton == null) {
synchronized (Singleton.class) {
if (singleton == null) {
singleton=new Singleton();
}
}
}
return singleton;
}
}
posted @ 2019-10-04 17:26  你是暖光-x  阅读(80)  评论(0编辑  收藏  举报