如何使用和设置ConcurrentHashMap的适当并发级别? [英] How to use and set appropriately concurrency level for ConcurrentHashMap?

查看:697
本文介绍了如何使用和设置ConcurrentHashMap的适当并发级别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用大约1000个元素在concurrenthashmap。默认并发级别为16。任何人都可以帮助我一些算法或因素,从中我可以识别适合我的场景的并发级别或以什么方式并发级别影响多线程的处理。

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是我的并发级别(虚拟值)。需要有效设置

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

推荐答案

根据文档:


操作由
指导可选的 concurrencyLevel 构造函数参数
(默认 16 ),用作内部大小调整的提示
表在内部被分区以尝试允许指定的
数量的并发更新而没有争用。因为在散列表中放置
本质上是随机的,实际的并发将
变化。
理想情况下,你应该选择一个值来容纳许多
线程将永远同时修改表。使用
比您所需的值高得多,可能会浪费空间和时间,
和明显较低的值可能导致线程争用。

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.

所以你需要回答1个问题:

So you need to answer 1 question:

修改表?

这篇关于如何使用和设置ConcurrentHashMap的适当并发级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆