79178170

Date: 2024-11-11 15:16:25
Score: 1
Natty:
Report link

Why tempWrapper is required for correctness? Can't I just remove it and replace with helperWrapper.

Let's compare the following 2 versions:

Original version:

   public Helper getHelper() {
      var localVar = helperWrapper; // read #1 of helperWrapper

      if (localVar == null) {
          synchronized (this) {
              ...
          }
      }
      return localVar.value;
   }

Version where tempWrapper is replaced with helperWrapper:

   public Helper getHelper() {
      if (helperWrapper == null) {  // read #1 of helperWrapper
          synchronized (this) {
              ...
          }
      }
      return helperWrapper.value;  // read #2 of helperWrapper
   }

Notice the number of reads of the shared variable helperWrapper:

If the write to helperWrapper happened in another thread, then the JMM treats such reads as kind of independent, i.e. it permits executions where the 1st read returns a non-null value and the 2nd read returns null (in this example this will throw NPE).
Therefore the local variable is used to avoid the 2nd read of the shared variable helperWrapper.

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Unregistered user (0.5):
  • Starts with a question (0.5): Whyis
  • Low reputation (1):
Posted by: user28242597