Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-5073546

Minor ConcurrentHashMap constructor spec tweaks

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 6
    • 5.0
    • core-libs

      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

            martin Martin Buchholz
            martin Martin Buchholz
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: