直接上代码
public class Test05 {
private volatile static Test05 test;
//volatile关键字保证线程之间可见性,并防止指令重排序
private Test05() {}
public static Test05 getSingleton() {
if(test==null) {
synchronized (Test05.class) {
if(test==null) {
test = new Test05();
}
}
}
return test;
}
}