Double checked locking in Android

后端 未结 2 778
情深已故
情深已故 2021-02-01 20:23

According to many, the somewhat common Double-Checked Locking idiom is broken for java unless you\'re running 1.5 or later and use the volatile keyword.

A b

相关标签:
2条回答
  • 2021-02-01 20:52

    I found a very good article about that question : http://www.javamex.com/tutorials/double_checked_locking_fixing.shtml

    It clearly states 3 ways to fix DCL. And it looks like in your question, the Helper field should be declared volatile, otherwise it doesn't work.

    When it comes to usage, i.e. RoboGucie in your case, I think I would favor the class loader method mentionned in the article. It's more clear to me and as efficient.

    0 讨论(0)
  • 2021-02-01 20:53

    The answer to this question implies that the memory models should be the same, and that the new double checked locking idiom will work.

    0 讨论(0)
提交回复
热议问题