The problem: ConcurrentHashMap's constructors have a number of weaknesses. The only way to create a map with a non-default concurrency level is to use the constructor ConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) Unfortunately, there is no guidance for what the value of loadFactor might mean exactly, and no way to determine the default value. A user who wants to use a concurrencyLevel of 1 would have to read the source to discover that the default initialCapacity and loadFactor are 16 and .75, respectively. The javadoc for constructor ConcurrentHashMap(Map<? extends K, ? extends V> t) states * The * map is created with a capacity of twice the number of mappings in * the given map or 11 (whichever is greater) First of all, this is a bad idea, since an access pattern that repeatedly "copies" maps using ConcurrentHashMap m = new ConcurrentHashMap(oldmap) will cause exponential waste of space. Secondly, the implementation does the more sensible thing of using a more rational initial capacity, just enough to hold the elements of the source map. The magic number 11 should be replaced by the default initial capacity of 16 since it is counterintuitive that in the sequence ConcurrentHashMap m1 = new ConcurrentHashMap(); ConcurrentHashMap m2 = new ConcurrentHashMap(m1); m2 would have a smaller capacity than m1. ###@###.### 2004-07-13
|