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() {
}
}