Double checked locking Article

后端 未结 10 670
别那么骄傲
别那么骄傲 2020-12-09 20:06

I was reading this article about \"Double-Checked locking\" and out of the main topic of the article I was wondering why at some point of the article the author uses the nex

10条回答
  •  生来不讨喜
    2020-12-09 21:03

    All right, but the article said that

    The code in Listing 7 doesn't work because of the current definition of the memory model. The Java Language Specification (JLS) demands that code within a synchronized block not be moved out of a synchronized block. However, it does not say that code not in a synchronized block cannot be moved into a synchronized block.

    And also seems like the JVM makes the next translation to "pseudo-code" in ASM:

    public static Singleton getInstance()
    {
      if (instance == null)
      {
        synchronized(Singleton.class) {      //1
          Singleton inst = instance;         //2
          if (inst == null)
          {
            synchronized(Singleton.class) {  //3
              //inst = new Singleton();      //4
              instance = new Singleton();               
            }
            //instance = inst;               //5
          }
        }
      }
      return instance;
    }
    

    So far, the point of no writes after the "instance=inst" is not accomplished?

    I will read now the article, thanks for the link.

提交回复
热议问题