A common pattern that I come across is the need to update a collection, such as List or HashMap, from a database, but you do not want the values ββto be added or removed from it between updates.
I recently discovered ImmutableList and ImmutableMap in the Google Guava library, which I really like. But I cannot do "clearAll ()" on these types and not rewrite them. But I like that they are unchanged than this.
So, if I wanted to apply this βonly make mutable when updating the databaseβ template, then I probably had to use the volatile variable every time? Or is there a better picture? This can be in a multi-threaded environment, and I would not guarantee the absence of race conditions when updating ();
public class MarketManager {
private volatile ImmutableList<Market> markets = null;
private MarketManager() {
}
public void refresh() {
marketList =
}
public static MarketManager getInstance() {
MarketManager marketManager = new MarketManager();
marketManager.refresh();
return marketManager;
}
}