马老师Java高级互联网架构p5p6p7p8
private static World SINGLETON_WORLD;
public static World getInstance() {
if (SINGLETON_WORLD == null) {
// 问题:多线程的情况下可能会同时进入
SINGLETON_WORLD = new World();
}
return SINGLETON_WORLD;
}
// 双锁检测,在工作中上述两种情况就能够满足了
public static World getInstance() {
if (SINGLETON_WORLD == null) {
synchronized (World.class) {
if (SINGLETON_WORLD == null) {
SINGLETON_WORLD = new World();
}
}
}