Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is static initialized unmodifiableCollection.get guaranteed immutable?

Is static initialized unmodifiableCollection.get guaranteed immutable?

For:

static final Map FOO = Collections.unmodifiableMap(new HashMap());

Can multiple threads use method get and not run into problems?

Even through items in FOO cannot be added/removed, what's stopping the get method from manipulating FOO's internal state for caching purposes, etc. If the internal state is modified in any way then FOO can't be used concurrently. If this is the case, where are the true immutable collections in java?

like image 522
Phil Avatar asked Jan 15 '10 20:01

Phil


2 Answers

Given the specific example:

static final Map FOO = Collections.unmodifiableMap(new HashMap());

Then FOO will be immutable. It will also never have any elements. Given the more general case of:

static final Map BAR = Collections.unmodifiableMap(getMap());

Then whether or not this is immutable is entirely dependent on whether or not someone else can get to the underlying Map, and what type of Map it is. For example, if it is a LinkedHashMap then the underlying linked list could be modified by access order, and could change by calling get(). The safest way (using non-concurrent classes) to do this would be:

static final Map BAR = Collections.unmodifiableMap(new HashMap(getMap()));

The javadocs for HashMap imply that so long as you make no structural changes to the map, then it is safe to use it concurrently, so this should be safe for any of the accessors that you can use, that is getting the various sets and iterating over them and get() should then be safe.

If you can use the concurrent classes, then you could also do:

static final Map BAR = Collections.unmodifiableMap(new ConcurrentHashMap(getMap());

This will be explicitly safe to use from multiple threads, since ConcurrentHashMap is explicitly multi-thread access safe. The internal state might be mutable, but the externally visible state will not be, and since the class is guaranteed to be threadsafe, we can safely consider it to be externally immutable.

like image 50
Paul Wagland Avatar answered Sep 30 '22 20:09

Paul Wagland


At the risk of sounding like I'm on an advertising spree, use the Google Immutable Collections and be done with it.

like image 32
Carl Avatar answered Sep 30 '22 18:09

Carl