详情请看代码
public class SingletonDemo {
private static volatile SingletonDemo instance = null;
public SingletonDemo(){
System.out.println(Thread.currentThread().getName() +"我是构造方法SingletonDemo()");
}
public static SingletonDemo getInstance(){
if (instance==null){
synchronized (SingletonDemo.class){
if (instance==null){
instance = new SingletonDemo();
}
}
}
return instance;
}
public static void main(String[] args) {
for (int i = 0; i <10 ; i++) {
new Thread(()->{
SingletonDemo.getInstance();
}).start();
}
}
}