public class MyObject {
private volatile static MyObject myObject;
private MyObject() {
}
public static MyObject getInstance() {
try {
if (myObject!=null) {
}else {
Thread.sleep(3000);
synchronized (MyObject.class) {
if (myObject==null) {
myObject=new MyObject();
}
}
}
} catch (Exception e) {
// TODO: handle exception
}return myObject;
}
}
public class MyThread extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
System.out.println(MyObject.getInstance().hashCode());
}
}
public class Run {
public static void main(String[] args) {
MyThread t1=new MyThread();
MyThread t2=new MyThread();
t1.start();
t2.start();
}
}
懒汉模式/延时加载,是在需要的时候创建