Java ConcurrentHashMap中分区越来越多的缺点? [英] Disadvantage of increasing number of partition in Java ConcurrentHashMap?

查看:154
本文介绍了Java ConcurrentHashMap中分区越来越多的缺点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java ConcurrentHashMap内部维护分区。每个分区都可以分开锁定。
可能存在多个线程访问的所有密钥落入同一分区和分区的情况可能无效。
进一步增加分区数量可以提高并发性。



为什么Java为分区计数提供16的默认值,而不是非常高的值?
地图中大量分区的性能是多少?

解决方案


为什么Java提供的分区计数的默认值为16而非非常高的值?


这些很多CPU线程数不是很重要)同时使用相同的CHM。如果您真的需要这样做,通常会有更好的方式来编写您的应用程序,以避免这种情况。



例如,您有1000个线程,但只有8个CPU。这意味着最多只有8个线程将运行并访问CHM,假设您的程序没有任何有用的功能。其他任何东西。



在实际程序中,一个集合的使用时间少于10%是很少的。这是因为通常涉及到一些IO,或者重组线程使用它们自己的集合副本是有意义的,并将它们一起收集在一起。地图缩小


地图中大量分区的性能是多少?




你浪费了一点无关紧要的记忆,但是大多数人浪费了一些限制在32 KB的L1缓存和相对宝贵的资源。


Java ConcurrentHashMap maintains partitions internally. Each partition can have locking separately. There can be scenarios where all the keys accessed by multiple thread fall into the same partition and partitions may not be helpful. Increasing the number of partitions further should improve the concurrency.

Why does Java provides default value for partition count as 16 instead of very high value? What is the performance overhear with large number of partitions in the Map?

解决方案

Why does Java provides default value for partition count as 16 instead of very high value?

It is very rare to have these many CPUs (The number of threads is not so important) using the same CHM at a the same time. If you really need this, there is usually a better way to write your application which avoids this.

For example say you have 1000 threads but only 8 CPUs. This means only 8 threads at most will be running and accessing the CHM, assuming your program doesn't do anything useful e.g. anything else.

In real programs, it is rare for one collection to be used more than 10% of time. This is because there is usually some IO involved, or it makes sense to restructure threads to use there own copies of collections and collect them together at the end e.g. Map-Reduce

What is the performance overhear with large number of partitions in the Map?

You waste a bit of memory which doesn't matter, but mostly you waste some L1 cache which is limited to 32 KB and a relatively precious resources.

这篇关于Java ConcurrentHashMap中分区越来越多的缺点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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