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