简体   繁体   English

本地哈希图的并发问题

[英]Concurrency issue with a local hashmap

I have a method that receives an Object ob as parameter. 我有一个接收Object ob作为参数的方法。
This method has a local HashMap<String, String> mp which is filled with the fields of ob , it also calls other methods passing ob and mp as parameters. 此方法有一个本地HashMap<String, String> mp ,其中填充了ob的字段,它还调用其他将obmp作为参数传递的方法。

The problem is that sometimes mp gets filled with fields of distinct ob objects. 问题在于,有时mp充满了不同ob对象的字段。

I think mp is experiencing concurrency problems, 我认为mp遇到并发问题,
What is the best way to synchronize mp knowing that it's being filled locally and being passed as parameter to various methods to get filled as well. 知道本地进行填充并作为参数传递给各种方法以进行填充的方法,同步mp的最佳方法是什么。

I don't have or control threads, the application is deployed in weblogic server, so it gets called through webservices, so a lot of calls 我没有或没有控制线程,该应用程序已部署在weblogic服务器中,因此它是通过webservices进行调用的,因此有很多调用

For such cases, you have 在这种情况下,

java.util.concurrent.ConcurrentHashMap<K,V>

Details here . 详细信息在这里

In short, ConcurrentHashMap works by having separated blocks that would be locked independently, so no other threads can access it until work is done (but can access other blocks at that time). 简而言之,ConcurrentHashMap的工作原理是具有独立锁定的独立块,因此在工作完成之前,没有其他线程可以访问它(但那时可以访问其他块)。 Default block size is 16. More about it here . 默认的块大小为16。更多关于它在这里

Now, you must understand map sizes in order to talk about filling it up. 现在,您必须了解地图的大小才能谈论将其填满。 Default size is 16. But, there is a load factor of 0.75, that means, when map is 75% full, it will resize to double of previous size, that means, creating new array new Set of keys and then copy everything to it (when I say new Set, I mean same logic for resize in it). 默认大小为16。但是,加载因子为0.75,这意味着当地图已满75%时,它将调整为先前大小的两倍,这意味着创建新数组新的键集,然后将所有内容复制到其中(当我说新Set时,我的意思是在其中调整大小的逻辑相同)。 Now, if you fill it up fast, that means a lot of copying and for larger sizes, it takes time, so its better to give it a large initial size at the beginning, like: 现在,如果您将其快速填满,这意味着需要大量复制并且需要较大的尺寸,这需要时间,因此最好在开始时为其设置较大的初始尺寸,例如:

ConcurrentHashMap<String, Object> mp = new ConcurrentHashMapy<>(someLargeSize);

Let someLargeSize be the order of something you expect or at least near it, so you decrease resizing to minimum. someLargeSize为您期望的顺序或至少接近它的顺序,以便将调整大小减小到最小。 Dont touch load factor, it will just decrease performance, 75% is ok. 不要触摸负载因子,它只会降低性能,可以使用75%。

And btw., max size of HashMap and ConcurrentHashMap (and HashSet, and ArrayList, or any array based Data Structure) in Java is 1,073,741,824 , since the array[] is in background, DS in Java are mostly just wrappers with specific functions for it, but I doubt you will ever reach this limit, since for objects that contains some strings or anything else, you need a lot of GB of RAM for it. 顺便说一句,Java中HashMap和ConcurrentHashMap(以及HashSet和ArrayList或任何基于数组的数据结构)的最大大小为1,073,741,824 ,因为array[]在后台,所以Java中的DS大多只是具有特定功能的包装器,但我怀疑您是否会达到此限制,因为对于包含某些字符串或其他内容的对象,您需要大量GB的RAM。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM