package com.lu;
public class Test {
private volatile static Test instance = null;
public static Test getInstance() {
if (null == instance) {
synchronized (Test.class) {
if (null == instance) {
instance = new Test();
}
}
}
return instance;
}
public static void main(String[] args) {
new GetThread().start();
new GetThread().start();
new GetThread().start();
}
}
class GetThread extends Thread {
@Override
public void run() {
System.out.println(Test.getInstance());
}
}
参考书籍:《Head First 设计模式》