...
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 | ||
|---|---|---|
| ||
class LongContainer {
private long i = 0;
void oneassignValue(long j) {
i = j;
}
void twoprintLong() {
System.out.println("i = " + i);
}
}
|
...
| Code Block | ||
|---|---|---|
| ||
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.
...