Given the following code:
public class FooBar {
public static volatile ConcurrentHashMap myConfigData = new ConcurrentHashMap();
}
public class UpdaterThread implements Runnable {
public void run() {
//Query the Data from the DB and Update the FooBar config Data
FooBar.myConfigData = ConfigDataDAO.getLatestConfigFromDB();
}
}
The Thread-Class will update the myConfigData Membervariable regularly (via an Executor every 5 minutes). Is the setting of of myConfigData in the "external" Thread threadsafe (atomic), or do I have to synchronize every Read and Write operation to the myConfigData Variable?
EDIT: The question is not whether ConcurrentHashMap is threadsafe (it is according to javadoc) but rather the setting of the ConcurrentHashMap itself in the myConfigData Member variable. This variable might be read and written "at once" by several threads so the question is whether the setting is atomic or not. I think this can be generalized to "Is the setting of a Java reference variable atomic or not?".
(I also made it volatile. This is a different issue and has nothing to do with atomicity - my question - but rather "visibility in other threads" and the happens-before relationship.)