/**
* 描述:
* 双重校验
*
* @author 小纸人
* @create 2019-03-10 22:40
*/
public class DoubleCheckSingleton {
private volatile static DoubleCheckSingleton instance;
private DoubleCheckSingleton(){
}
public static DoubleCheckSingleton getInstance(){
if (instance == null){
synchronized (DoubleCheckSingleton.class){
if (instance == null){
instance = new DoubleCheckSingleton();
}
}
}
return instance;
}
}
class MainDoubleCheckSingleton{
public static void main(String[] args){
TestDoubleCheckSingleton thread1 = new TestDoubleCheckSingleton();
TestDoubleCheckSingleton thread2 = new TestDoubleCheckSingleton();
thread1.start();
thread2.start();
}
}
class TestDoubleCheckSingleton extends Thread{
@Override
public void run() {
System.out.println(DoubleCheckSingleton.getInstance());
}
}
单例双重校验
最新推荐文章于 2024-06-18 22:05:15 发布