Vaithilingam K
Vaithilingam K

Reputation: 25

Reason for default concurrency level of ConcurrentHashMap

Why is the default concurrency level of ConcurrentHashMap 16, why not 18?

Upvotes: 0

Views: 1020

Answers (4)

Pradeep
Pradeep

Reputation: 12675

Concurrency Level denotes the number of shards. It is used to divide the ConcurrentHashMap internally into this number of partitions and equal number of threads are created to maintain thread safety maintained at shard level. The default value of “concurrencyLevel” is

16

It means 16 shards whenever we create an instance of ConcurrentHashMap using default constructor, before even adding first key-value pair. It also means the creation of instances for various inner classes like ConcurrentHashMap$Segment, ConcurrentHashMap$HashEntry[] and ReentrantLock$NonfairSync.

In most cases in normal application,

a single shard is able to handle multiple threads with reasonable count of key-value pairs. And performance will be also optimal. Having multiple shards just makes the things complex internally and introduces a lot of un-necessary objects for garbage collection, and all this for no performance improvement.

It is better practice to keep the concurrencyLevel minimal. Please note that if you are working on very high concurrent application with very high frequency of updates in ConcurrentHashMap, then only you should consider increasing the concurrencyLevel, but again it should be a well calculated number to get the best results.

Upvotes: 0

Andy Turner
Andy Turner

Reputation: 140329

The basic answer to the question "why is a default value not the value I want" is usually: because your use case is specific to you, and is not necessarily good the general case.

(Another possible answer is: they picked an arbitrary but reasonable default).

Irrespective of the reason, it has that value, so you can either use that class with the default value or its API to specify a different value; or you can use a different class; or you can provide a justification to the maintainers of the class as to why it should have a different value.

In this case, there is a constructor which allows you to specify the concurrency level, so just specify the value you want.

Upvotes: 0

Veselin Davidov
Veselin Davidov

Reputation: 7081

Prior to JAVA 8

The concurrencyLevel meant how many internal hashmaps will the concurrent hashmap need in order to work properly. This basically means that you should write a value pointing out how many threads will use that concurrent hashmap. Writing more than the actual threads is not much of a performance loss more a memory loss. Why java developers decided to go for exactly 16 I can only guess but it might be due to counting cores on processors etc. You don't want 18 because you probably won't have 18 core/threads CPU and you can have 16. It still a default number so for maximum performance you need to set it.

After JAVA 8:

This concurrent level is not used anymore ;) It is left in the constructor just for compatibility reasons with older code.

Upvotes: 1

R.yan
R.yan

Reputation: 2372

I just checked the implementation of concurrency map, I found the following:

/**
 * The default concurrency level for this table. Unused but
 * defined for compatibility with previous versions of this class.
 */
private static final int DEFAULT_CONCURRENCY_LEVEL = 16;

It seems just defined for compatibility with older version.

And for the question why it's 16 not 18. I guess it's because it better be power of 2 due to the bitwise operation and better memory management in implementation.

Upvotes: 2

Related Questions