How to use and set appropriately concurrency level for ConcurrentHashMap?

后端 未结 5 1743
遥遥无期
遥遥无期 2020-12-17 09:20

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 t

5条回答
  •  死守一世寂寞
    2020-12-17 10:08

    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?

提交回复
热议问题