I would like to find out if following usage of an AtomicBoolean as a flag variable is correct and good for a multi-threaded application. It is expected that data is going to be loaded only once and when the application is at very low load. It is also expected that the list of SomeObjects is going to be read quite frequently by multiple threads at peak load, probably thousands of time in an hour so that is why I want to avoid hitting the database. The list is not going to be very large so keeping it in memory is not going to be a problem.
public class HibernateDao extends SomeOtherClass implements Dao {
//Need suggestions for correct usage of following variable
private AtomicBoolean dataLoaded = new AtomicBoolean();
private List<SomeObject> someObjects;
@Override
public List<SomeObject> getAllSomeObjects() {
if (dataLoaded.getAndSet(false)) {
//aim is to atomically load the list only once and avoid calling loadAll
//every time list is used
someObjects= (List<SomeObject>) getHibernateTemplate().loadAll(SomeObject.class);
}
return someObjects;
}
@Override
public void updateAllSomeObjects(Collection<SomeObject> someObjects) {
//replaceAll is part of parent class and it removed and reloads
//data for the entity i.e. SomeObject
replaceAll(SomeObject.class, someObjects);
//set this flag atomically so that data is fetched from DB next time
dataLoaded.getAndSet(true);
}
}
replaceAllmethod does? I imagine that the answer can be a bit different depending on what that does. Or how is the two methods of this class used in your application? \$\endgroup\$