gpt4 book ai didi

Java 双重检查锁定,这段代码有效吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:52:22 27 4
gpt4 key购买 nike

<分区>

我已阅读 The "Double-Checked Locking is Broken" Declaration .但我想知道如果我通过函数创建对象,这样可以吗?

    class Foo { 
private Helper helper = null;
public Helper getHelper() {
if (helper == null)
synchronized(this) {
if (helper == null)
helper = createHelper();
}
return helper;
}
private Helper createHelper() {
return new Helper();
}
// other functions and members...
}

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