csk csk - 6 months ago 96
Java Question

How to use and set appropriately concurrency level for ConcurrentHashMap?

I am working with around 1000 elements in concurrenthashmap . Default concurrency level is 16 . can anyone help me with some algorithm or factors from which i can identify the concurrency level suitable for my scenario or in what way a concurrency level affects the processing of multiple threads .

ConcurrentHashMap<String, String> map=new ConcurrentHashMap<String, String>(500,1,20);


20 is my concurrency level (dummy value) .Need to set this efficiently

Answer

According to docs:

The allowed concurrency among update operations is guided by the optional concurrencyLevel constructor argument (default 16), which is used as a hint for internal sizing. The table is internally partitioned to try to permit the indicated number of concurrent updates without contention. Because placement in hash tables is essentially random, the actual concurrency will vary. Ideally, you should choose a value to accommodate as many threads as will ever concurrently modify the table. Using a significantly higher value than you need can waste space and time, and a significantly lower value can lead to thread contention.

So you need to answer 1 question:

What is the number of threads that will ever concurrently modify the table?

Comments