`
wangyanlong0107
  • 浏览: 481449 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

java double checked locking broken

    博客分类:
  • java
 
阅读更多

// Double-check idiom for lazy initialization of instance fields

private volatile FieldType field;

FieldType getField() {

    FieldType result = field;

    if (result == null) { // First check (no locking)

        synchronized(this) {

            result = field;

            if (result == null) // Second check (with locking)

                field = result = computeFieldValue();

        }

    }

    return result;

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics