I think this might an easy thing but wanted to know if there is a pattern for this or an elegant way. I took a look at Guava. I have a class level List, a method that refers to this listOfObjects on scheduler, and a method that updates it on scheduler. The updater method collects all Objects that should be in this list and has a new list ready to reinitilize listOfObjects. However should I just set it even if it is being used by the method that is referring to it, or is there a more safe way to do it.
private List<Object> listOfObjects = new ArrayList<Object>();
@Scheduled
public referToList(){
for(Object o : listOfObjects){
doSomething(o);
}
}
@Scheduled
public updateList(){
List<Object> tempList = new ArrayList<Object>();
tempList = doSomethingToPopulateList();
this.listOfObjects = tempList;
}
So its possible updateList can be updating list when referToList can be in the middle of iterating through. I can create a temp List in referToList as well so its working on a copy of listOfObjects but not sure how efficient that is