Why is volatile used in double checked locking

后端 未结 7 831
太阳男子
太阳男子 2020-11-22 05:01

From Head First design patterns book, the singleton pattern with double checked locking has been implemented as below:

public class Singleton {
            


        
7条回答
  •  再見小時候
    2020-11-22 05:25

    Well, there's no double-checked locking for performance. It is a broken pattern.

    Leaving emotions aside, volatile is here because without it by the time second thread passes instance == null, first thread might not construct new Singleton() yet: no one promises that creation of the object happens-before assignment to instance for any thread but the one actually creating the object.

    volatile in turn establishes happens-before relation between reads and writes, and fixes the broken pattern.

    If you are looking for performance, use holder inner static class instead.

提交回复
热议问题