package design.singletonLazy.demo;
public class Main {
public static void main (String[] args) {
Thread t1 = new Thread() {
@Override
public void run () {
SingletonLazyUnsafetyEntity s = SingletonLazyUnsafetyEntity.getSingletonEntity();
System.out .println("t1线程获取对象的hashcode: " + s.hashCode());
}
};
Thread t2 = new Thread() {
@Override
public void run () {
SingletonLazyUnsafetyEntity s = SingletonLazyUnsafetyEntity.getSingletonEntity();
System.out .println("t2线程获取对象的hashcode: " + s.hashCode());
}
};
Thread t3 = new Thread() {
@Override
public void run () {
SingletonLazyUnsafetyEntity s = SingletonLazyUnsafetyEntity.getSingletonEntity();
System.out .println("t3线程获取对象的hashcode: " + s.hashCode());
}
};
t1.start();
t2.start();
t3.start();
}
}
package design.singletonLazy.demo;
public class SingletonLazyUnsafetyEntity {
private static SingletonLazyUnsafetyEntity s = null ;
public static SingletonLazyUnsafetyEntity getSingletonEntity () {
if (s == null ) {
s = new SingletonLazyUnsafetyEntity();
}
return s;
}
private SingletonLazyUnsafetyEntity () {
}
}