Assuming there is a field in a class that is changing/written infrequently but is being read from concurrently, is it necessary to protect it with a ReadWriteLock?
@Component
public class SomeClass {
private Map<String, String> someField;
@EventListener(ApplicationReadyEvent.class)
@Scheduled(fixedRate = 15, timeUnit = TimeUnit.MINUTES)
public void someScheduledActivity() {
// replace the whole map (A)
someField = theNewValue();
}
public String read(String id) {
// reading from the map field (B)
return this.someField.getOrDefault(id, null);
}
}
As far as I understand, we must write lock at (A) and read lock at (B). But I have been told Java is different, and there is no need for this synchronisation (or any kind of synchronisation). This sounds very different and unexpected to me.
Are there any official Java documents/guidelines that could help clarify this?