Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: changed method names from one(), two()

...

In this noncompliant code example, if one thread repeatedly calls the method oneassignValue(), and another thread repeatedly calls the method twoprintLong(), then method twoprintLong() could occasionally print a value of i that is neither zero nor the value of the argument j.

Code Block
bgColor#FFcccc
class LongContainer {
  private long i = 0;

  void oneassignValue(long j) {
    i = j; 
  }

  void twoprintLong() {
    System.out.println("i = " + i);
  }
}

...

Code Block
bgColor#ccccff
class LongContainer {
  private volatile long i = 0;
 
  void oneassignValue(long j) { 
    i = j; 
  }
 
  void twoprintLong() {
    System.out.println("i = " + i);
  }
}

It is important to ensure that the argument to method oneassignValue() is obtained from a volatile variable or as a result of explicitly passing an integer value. Otherwise, a read of the variable argument may itself expose a vulnerability.

...