多线程单例模式之立即加载(饿汉模式)
package com.wz.thread.immediately;
/**
* 立即加载/饿汉模式 单例设计模式
* @author Administrator
*
*/
public class MyObject {
private static MyObject myObject = new MyObject();
private MyObject() {}
public static MyObject getInstance() {
return myObject;
}
}
package com.wz.thread.immediately;
public class MyThread extends Thread {
@Override
public void run() {
super.run();
System.out.println(MyObject.getInstance().hashCode());
}
}
package com.wz.thread.immediately;
/**
* 输出的hascode值相同,说明是同一个对象
* @author Administrator
*
*/
public class Run {
public static void main(String[] args) {
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
MyThread t3 = new MyThread();
t1.start();
t2.start();
t3.start();
}
}