简体   繁体   English

如何为ConcurrentHashMap使用并设置适当的并发级别?

[英]How to use and set appropriately concurrency level for ConcurrentHashMap?

I am working with around 1000 elements in concurrenthashmap . 我正在并发hashmap中处理大约1000个元素。 Default concurrency level is 16 . 默认并发级别为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 我的并发级别(虚拟值)为20。需要进行有效设置

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 . 更新操作之间允许的并发性由可选的concurrencyLevel构造函数参数(默认为16 )指导,该参数用作内部大小调整的提示 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: 因此,您需要回答1个问题:

What is the number of threads that will ever concurrently modify the table? 可以同时修改表的线程数是多少?

Java 8: Java 8:

Now the ConcurrentHashMap does not use a fixed lock striping scheme at all, instead each bucket serves as a “stripe” using intrinsic synchronization. 现在, ConcurrentHashMap根本不使用固定的锁定条带化方案,而是使用内部同步将每个存储桶用作“条带”。

Code from source: 源代码:

/** Implementation for put and putIfAbsent */
final V putVal(K key, V value, boolean onlyIfAbsent) {
    ...
    Node<K,V> f; int n, i, fh;
    ...
    else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
    ...
       synchronized (f) {
           ...
       }
}

And the constructor has the parameter just use it as a size hint as docs say. 并且构造函数具有该参数,只是将其用作文档说明的大小提示。

concurrencyLevel - the estimated number of concurrently updating threads. concurrencyLevel-并发更新线程的估计数量。 The implementation may use this value as a sizing hint. 该实现可以使用该值作为调整提示。

And the source: 以及来源:

public ConcurrentHashMap(int initialCapacity,
                         float loadFactor, int concurrencyLevel) {
    if (!(loadFactor > 0.0f) || initialCapacity < 0 || concurrencyLevel <= 0)
        throw new IllegalArgumentException();
    if (initialCapacity < concurrencyLevel)   // Use at least as many bins
        initialCapacity = concurrencyLevel;   // as estimated threads
    long size = (long)(1.0 + (long)initialCapacity / loadFactor);
    int cap = (size >= (long)MAXIMUM_CAPACITY) ?
        MAXIMUM_CAPACITY : tableSizeFor((int)size);
    this.sizeCtl = cap;
}

So you don't need to consider it by yourself, ConcurrentHashMap will handle it for you. 因此,您无需自己考虑, ConcurrentHashMap将为您处理。

ConcurrentHashMap allows multiple readers to read concurrently without any blocking. ConcurrentHashMap允许多个读取器同时读取而没有任何阻塞。 This is achieved by partitioning Map into different parts based on concurrency level and locking only a portion of Map during updates. 这是通过根据并发级别将Map划分为不同的部分并在更新过程中仅锁定Map的一部分来实现的。 Default concurrency level is 16, and accordingly Map is divided into 16 part and each part is governed with different lock. 默认的并发级别为16,因此Map分为16个部分,每个部分由不同的锁控制。 This means, 16 thread can operate on Map simultaneously, until they are operating on different part of Map. 这意味着16个线程可以同时在Map上运行,直到它们在Map的不同部分上运行为止。 This makes ConcurrentHashMap high performance despite keeping thread-safety intact. 尽管保持线程安全性不变,但这仍使ConcurrentHashMap高性能。

16 is the default number of regions that your map will be split into. 地图将被分割为默认区域数16。 ConcurrentHashMap, in case of reader threads, is done (in almost all cases) without locking at all. 在读取器线程的情况下,ConcurrentHashMap完成(几乎在所有情况下)而无需锁定。 The number of writer threads is the thing you need to worry. 编写者线程数是您需要担心的事情。 And this number should be equal to the number of regions you have. 并且此数字应等于您拥有的区域数。

因此,并发级别等于写程序线程。并且映射也将分段等于并发级别的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在java中的concurrentHashMap中设置并发级别 - How to set concurrency level in concurrentHashMap in java 我们如何正确设置并发哈希图的LoadFactor和并发级别? - How do we properly set LoadFactor & concurrency level of our concurrenthashmap? ConcurrentHashMap的默认并发级别的原因 - Reason for default concurrency level of ConcurrentHashMap 同步方法中ConcurrentHashMap的并发级别 - Concurrency level for ConcurrentHashMap in synchronized method 如果并发级别高于桶数,ConcurrenthashMap如何管理? - How ConcurrenthashMap manages if Concurrency level is HIGHER than the number of Buckets? 是否可以允许比ConcurrentHashMap中的并发级别更多的线程数? - Is it possible to allow more number of threads than concurrency level in ConcurrentHashMap? 为ConcurrentHashMap选择并发级别的最佳方法是什么? - What's the best way to choose the concurrency level for a ConcurrentHashMap? 如何在 executorCompletionService 中使用 concurrentHashMap? - How to use concurrentHashMap in executorCompletionService? 是否可以将ConcurrentHashmap计算与java 7源代码级别一起使用? - Is it possible to use ConcurrentHashmap compute with java 7 source level? ConcurrentHashMap 的 Kotlin 并发 - Kotlin concurrency for ConcurrentHashMap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM