心若在,梦就在

Lazy
initialization
- It decreases the cost of initializing a class or
creating an instance, at the expense of increasing the cost of accessing the
lazily initialized field. Depending on what fraction of lazily initialized
fields eventually require initialization, how expensive it is to initialize
them, and how often each field is accessed, lazy initialization can (like many
"optimizations") actually harm performance.

The only way to know
for sure is to measure the performance of the class with and without lazy
initialization.

Principle

  • Normal initialization of an instance
    field -
    Under most circumstances, normal initialization is preferable
    to lazy initialization.

    // Normal initialization of an instance
    field

    private final FieldType field =
    computeFieldValue();


  • synchronized accessor - If
    you use lazy initialization to break an initialization circularity, use a
    synchronized accessor, as it is the simplest, clearest
    alternative:

    // Lazy initialization of instance
    field - synchronized accessor

    private FieldType field;

    synchronized FieldType
    getField() {

    if (field == null)

    field =
    computeFieldValue();

    return field;

    }

    Both of these idioms (normal initialization and
    lazy initialization with a synchronized accessor ) are unchanged when applied
    to static fields, except that you add the static modifier to the field and
    accessor declarations.


  • lazy initialization holder class
    idiom
    - If you need to use lazy initialization for performance on a
    static field, use the lazy initialization holder class idiom .

    // Lazy initialization holder class
    idiom for static fields

    private static class
    FieldHolder {

    static
    final
    FieldType field = computeFieldValue();

    }

    static FieldType getField() {
    return FieldHolder.field; }


  • Double-check idiom - If you
    need to use lazy initialization for performance on an instance field, use the
    double-check idiom.

    // 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;

    }


  • Single-check idiom -
    Occasionally, you may need to lazily initialize an instance field that can
    tolerate repeated initialization.

    // Single-check idiom - can cause repeated
    initialization!

    private volatile FieldType
    field;

    private FieldType getField() {

    FieldType result =
    field;

    if (result == null)

    field = result =
    computeFieldValue();

    return result;

    }

    Note

    When the double- check or single-check idiom is
    applied to a numerical primitive field, the field‘s value is checked against 0
    (the default value for numerical primitive variables) rather than
    null.


  • Racy single-check idiom - If
    you don‘t care whether every thread recalculates the value of a field, and the
    type of the field is a primitive other than long or double , then you may
    choose to remove the volatile modifier from the field declaration in the
    single-check idiom(e.g. String instances to cache their hash codes).

    // racy single-check idiom - can cause repeated
    initialization!

    private FieldType field;

    private FieldType getField() {

    FieldType result =
    field;

    if (result == null)

    field = result =
    computeFieldValue();

    return result;

    }

    Summary

    You should initialize most fields normally, not
    lazily. If you must initialize a field lazily in order to achieve your
    performance goals, or to break a harmful initialization circularity, then use
    the appropriate lazy initialization technique. For instance fields, it is the
    double-check idiom; for static fields, the lazy initialization holder class
    idiom. For instance fields that can tolerate repeated initialization, you may
    also consider the single-check idiom.


心若在,梦就在,布布扣,bubuko.com

时间: 2025-01-01 09:05:05

心若在,梦就在的相关文章

追梦软件路,愿不忘初心

第一部分:结缘软件工程 你为什么选择软件工程专业?你认为你的条件如何?(必答)软工是你喜欢的领域吗?是你擅长的领域吗?你热爱这一专业吗?你对软工的热爱是怎样的? 刚开始上高三的我,每时每刻都在刷卷子刷错题,一遍一遍的稳固知识点,强化记忆,以备即将到来的人生第一个分支点--高考. 直到2015年12月份的一天晚上,因腹部剧痛导致失眠,一大早被家人带着去了医院,经过仔细的核查之后确定了不立刻动手术不行的残酷现实. 小毛病总会在关键时刻引发巨大问题,简直就是活生生的bug.明明那么多次都忍了下来没有说

青春无罪 梦想有毒

一颗奋斗的心——青春的梦 一转眼,自己即将结束大学的课程,还有半个月要和这个爱恨交加的校园说再见了.误打误撞的选了软件技术这个专业,莫名其妙的喜欢上代码. 在大学,我和幸运遇到一群“疯子”.对我的生活,有了质的影响.我很感激你们不断间的喷我,让我现在有着超出同龄人的承受能力和责任感.很开心没有受室友的影响,沉迷于游戏.我还是挺欣慰的(postscript:我会一点,但是不代表我会把时间浪费在游戏上)还记得当初你们的嘲笑,我看你装到什么时候?不是我不合群,是你们聊的东西,我都不懂.可能,我们在大学

游戏服务器框架概括分析

这篇blog题目涉及的范围真大!以至于在这里需要先写一篇前言把范围缩小.选择写这样一个系列的文章,主要是想给工作了两年的自己一个交代,或者说是一个阶段性的总结.两年时间里,房价依然再涨,工资依然跑不赢CPI,某人依然在仰望星空.期间很多梦碎了,很多还在坚持着,生活过得波澜不惊.而我也从刚毕业是的青涩逐步蜕变为"老油条".不知道是一种悲哀.还是一种悲哀.还是一种悲哀....... 庆幸的是梦还在继续,一颗倔强的心还在坚持.希望明天的明天被束缚的心能回到梦开始的地方! ==========

春雨缠绵,思念已成殇

引言于(造句大全):我的思念,是你前世遗忘采摘而无法成熟的青果,不舍那未了的情缘,我才执意轮回世间. 宿命纷飞,夜末央,碎在何方?流年似水,难定格,悠悠流散. 痴守千年,月老红残誰为我牵?独恋万疆,情煎寸心尚复何言? 万般无奈也说前缘.三生石上,故步可寻:人间今世,良缘可定.芳名作枕,星月照眠:溺水惊心,映梦前缘.天地有情,永护同心之石:川岳不改,终圆并枕之盟. 誰念誰,一生浮华殇?誰情誰,一世离别愁? 红尘滚滚几千年,湮没了太多的悲情凯歌.难忘却,几度红颜损伤.依然灯火岷州,月夜魂,萦阁楼,无

一丝风.十年面壁

一丝风(诉衷情令).十年面壁 十年面壁唱春风,今古两牵情.年年佳节安度?毫素伴孤灯. 心未了,梦常萦,愿难成.古人嘉言:学而时习,可慰平生.

最快丰胸产品(咨询Q:541858080)

18-30岁的女性多数拥有光鲜亮丽的外表,坚挺傲人的“双峰”,在这个女性一生最美好的年华如花般绽放.可是就有那么一些女性也许老天嫉妒她们如画的容颜,偏偏让她们胸部发育不良,成了“太平公主”,她们只能大感遗憾吗?最快丰胸产品?还在为你平平的飞机场发愁吗?还是没有办法挺胸找回自信吗?『亦姿佳美乳霜』让你靓丽自信! 最快丰胸产品 扫叶煎茶摘叶书,心闲无梦夜窗虚.从小我就是被茶文化熏陶,爷爷和父亲都爱喝茶,到后来我被中国的茶文化所吸引.所以长大后我想从事与茶有关的工作,而且还考了茶艺师的资格证.茶楼的工

一言一情,一语一感

1.等着等着云散了,雾也散了:盼着盼着花儿盛放了,草儿也萌芽了:守着守着鸟儿回巢了,田间也空寂了.等--盼--守 勿--忘--本.即使,走着走着,物是人非.烦请,莫恐莫慌,珍惜眼前.寻找一个落脚点,一人一物方可,觅到了港湾,勇气信心自会燃起. 2.爱,没有谁对谁错,不论成败.感觉对了,趣味相投,便成了爱人:彼此了解,爱好形似,便成了朋友.唯独亲情,天意所赐,骨肉相连,血浓于水.爱,需要心有灵犀,需要默契,需要机缘,更需要大声说出来. 3.人 ,矛盾体,多面体 .很多时候 ,随性生活: 很多时候,

士艺朴仝难次狙阎厩谐蔽lsqq

IEEE Spectrum 杂志发布了一年一度的编程语言排行榜,这也是他们发布的第四届编程语言 Top 榜. 据介绍,IEEE Spectrum 的排序是来自 10 个重要线上数据源的综合,例如 Stack Overflow.Twitter.Reddit.IEEE Xplore.GitHub.CareerBuilder 等,对 48 种语言进行排行. 与其他排行榜不同的是,IEEE Spectrum 可以让读者自己选择参数组合时的权重,得到不同的排序结果.考虑到典型的 Spectrum 读者需求

天上掉砖块砸死老人 法院判业主共同担责冤不冤?

但就在这时前面有了动静只见一个团队的玩家正在练级徽记很明显赫然是永恒的玩家似乎是一个千人团正在清理月光森林空地上的狼群 大将军的身后级法师上将军级刺客偏将军级牧师运粮将军级的狂战士隐藏进阶职业破冰战狂骁骑将一身的暗金器装备泛着暗金色光泽显然八百骑的王牌玩家就是他了一个隐藏职业的狂战士配合着一身的暗金器想必爆发力不可小觑点但是怪物多大概有十个左右如果不是我闪得快恐怕真的就要被秒了凌雪眨着眼睛说道 对啊我恍然大悟凌月果然心细如尘啊不过幽暗套装虽然等级低了一些却也让我的防御加了不少直接从的防御飞跃至点

命片起原采高空当看都前

Jonas nodded, thanked her, and walked down the long hallway. He glanced into the rooms on either side. The Old were sitting quietly, some visiting and talking with one another, others doing handwork and simple crafts. A few were asleep. Each room was