Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Volatile HashMap vs ConcurrentHashMap

Tags:

I have a cache class which contains a volatile HashMap<T> to store cache items.

I'm curious what would be the consequences of changing volatile HashMap to ConcurrentHashMap?

Would i gain performance increase? This cache is readonly cache.

What would be the best option to use? just HashMap? Cache is being populated on a interval.

like image 326
DarthVader Avatar asked Apr 27 '12 21:04

DarthVader


2 Answers

First, it appears you don't understand what the volatile keyword does. It makes sure that if the reference value held by the variable declared volatile changes, other threads will see it rather than having a cached copy. It has nothing to do with thread-safety in regard to accessing the HashMap

Given that, and the fact that you say the HashMap is read-only ... you certainly don't need to use anything that provides thread-safety including a ConcurrentHashMap

Edit to add: Your last edit you now say "The cache is being populated on a interval"

That's not read-only then, is it?

If you're going to have threads reading from it while you are writing (updating the existing HashMap) then you should use a ConcurrentHashMap, yes.

If you are populating an entirely new HashMap then assigning it to the existing variable, then you use volatile

like image 97
Brian Roach Avatar answered Sep 24 '22 05:09

Brian Roach


You say the cache is read-only, but also being updated on an interval which seems contradictory.

If the whole cache gets updated on an interval, I'd keep using the volatile. The volatile will make sure that the updated map is safely published.

public final class  Cache {    private volatile Map<?,?> cache;     private void mapUpdate() {       Map<?,?> newCache = new HashMap<>();        // populate the map        // update the reference with an immutable collection       cache = Collections.unmodifiableMap(newCache);    } } 

If the interval update is modifying the same cache, then you probably want to use a ConcurrentHashMap, or copy the map, update the copy, and update the reference.

public final class  Cache {    private volatile Map<?,?> cache;     private void mapUpdate() {       Map<?,?> newCache = new HashMap<>(cache);        // update the map        // update the reference with an immutable collection       cache = Collections.unmodifiableMap(newCache);    } } 
like image 43
Michael Krussel Avatar answered Sep 23 '22 05:09

Michael Krussel