Is it possible to have more than 32 locks in ConcurrentHashMap

I read that ConcurrentHashMap works better in multi-threaded streams than Hashtable, due to the presence of locks at the bucket level, and not on the map. A maximum of 32 locks can be set on the map. Want to know why 32 and why not more than 32 locks.

+5
source share
4 answers

If you are talking about Java ConcurrentHashMap, then the limit is arbitrary :

, . 1,5 16 ( , ) (0.75) concurrencyLevel (16).

, 2 ^ 16, .

, , :

concurrency 32. 32 / .

, , , , 32 ConcurrentHashMap.

+7

32, 16. concurrency level:

public ConcurrentHashMap(int initialCapacity,
                         float loadFactor,
                         int concurrencyLevel)

:

Map<String, String> map = new ConcurrentHashmap<String, String)(128, 0.75f, 64);

64. ( Java 6u17):

  • initialCapacity: 16;
  • loadFactory: 0.75f;
  • concurrencyLevel: 16.
+4

ConcurrentHashMap, 65536:

/**
 * The maximum number of segments to allow; used to bound
 * constructor arguments.
 */
static final int MAX_SEGMENTS = 1 << 16; // slightly conservative

public ConcurrentHashMap(int initialCapacity,
                         float loadFactor, int concurrencyLevel) {
    if (concurrencyLevel > MAX_SEGMENTS)
        concurrencyLevel = MAX_SEGMENTS;
+3

To use all of the default concurrency levels of 16, you need to have 16 cores using the card at the same time. If you have 32 cores, only using the card 25% of the time, then only 8 out of 16 segments will be used immediately.

Thus, you need to have many cores that use the same card and do nothing. Real programs usually do something other than access to one card.

+2
source

All Articles