Given this method on LocalNodeRecordStore:
public void onCustomFieldValueChanged(String fieldName, Bytes value) {
NodeRecord oldRecord = this.latestRecord;
NodeRecord newRecord = oldRecord.withUpdatedCustomField(fieldName, value, this.secretKey);
this.latestRecord = newRecord;
this.recordListener.recordUpdated(oldRecord, newRecord);
}
There can be a concurrency issue even though latestRecord is declared volatile:
NodeRecord oldRecord = this.latestRecord;
// ...create newRecord based on oldRecord...
this.latestRecord = newRecord;
If two threads call this method at the same time, they may:
Both read the same oldRecord.
Both compute a different newRecord.
Both assign a latestRecord, but the second thread silently overwrites the first one's update.
Suggestions:
- Use synchronized block
- Use AtomicReference
Given this method on LocalNodeRecordStore:
There can be a concurrency issue even though latestRecord is declared volatile:
If two threads call this method at the same time, they may:
Both read the same oldRecord.
Both compute a different newRecord.
Both assign a latestRecord, but the second thread silently overwrites the first one's update.
Suggestions: