If I have an instance of an HashSet after I ran it through Collections.unmodifiableSet(), is it thread-safe?
I\'m asking this since Set documentation states that it\'s n
HashSet will be threadsafe if used in a read-only manner. That doesn't mean that any Set that you pass to Collections.unmodifiableSet() will be threadsafe.
Imagine this naive implementation of contains that caches the last value checked:
Object lastKey;
boolean lastContains;
public boolean contains(Object key) {
if ( key == lastKey ) {
return lastContains;
} else {
lastKey = key;
lastContains = doContains(key);
return lastContains;
}
}
Clearly this wouldn't be threadsafe.