gpt4 book ai didi

java - 为什么静态内部类单例线程安全

转载 作者:搜寻专家 更新时间:2023-11-01 03:57:40 27 4
gpt4 key购买 nike

<分区>

正如标题所说,静态嵌套类单例为什么是线程安全的?

public class Singleton {
private static class SingletonHolder {
public static Singleton instance;

public static Singleton getInstance() {
if (null == instance) {
instance = new Singleton();
}
}
}

public static Singleton getInstance() {
return SingletonHolder.getInstance();
}
}

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com