HashMap和并发 - 不同的键 [英] HashMap and concurrency- different keys

查看:92
本文介绍了HashMap和并发 - 不同的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个哈希映射和多个线程。如果我有一个同步方法添加到哈希映射中,我将如何使两个不同的线程可以同时(同时)将不同的键放入哈希映射?



我目前的实现是一个同步方法。这将允许两个不同的线程将两个不同的键同时放入哈希映射中吗?我使用的是常规哈希映射,而不是Java的并发哈希映射。我不允许使用并发散列图。



编辑:
我想我找到了一个解决方案!我想我可能误导了这篇文章。假设哈希映射被初始化为Integer作为其键,并将LinkedList作为其值。为了建立一个全新的密钥,我意识到整个哈希映射必须被同步(即锁定)。但是,如果我想将另一个字符串添加到已经包含的键的相应LinkedList中,我只能同步哈希映射的get方法。我认为这将允许多个线程同时(并发地)向LinkedLists添加不同的,已经包含的密钥。请让我知道,如果我错了。



以下是一个具体的例子。我有一个哈希映射hashMap,它使用Integer作为其键和LinkedList作为其值。密钥5和10已经在哈希映射中。关键5包含Joey,Joe,Kerry的LinkedList。关键10包含Jerry,Mary,Tim的LinkedList。我有两个线程t1和t2。 t1想要将Moe添加到对应于键5的LinkedList中.t2希望将Harry添加到与键10相对应的LinkedList中。由于哈希映射的值仅锁定,因此两者将同时添加到哈希映射中。

解决方案


我目前的实现是一种同步方法。这是否允许两个不同的线程同时将两个不同的键放入散列映射中?


否。只有一个 ConcurrentHashMap 或一个专门设计的并发映射可以安全地支持这个,所以你不可能同时把两个键从两个线程放到同一个映射中。


如何使两个不同的线程可以同时(同时)将不同的键放入哈希映射中?


blockquote>

如果不使用 ConcurrentHashMap ,另一个 ConcurrentMap 实现,或者实施你自己的。

Say I have a hash map and multiple threads. If I have a synchronized method that adds to the hash map, how would I make it possible that two different threads can put different keys simultaneously (concurrently) into the hash map?

My current implementation is a synchronized method. Would this allow two different threads to put two different keys simultaneously into the hash map?

I am using a regular hash map, not Java's concurrent hash map. I am not allowed to use a concurrent hash map.

EDIT: I think I found a solution! I think I may have miswrote this post. Let's say that the hash map is initialized as a Integer as its key and a LinkedList as its value. In order to put a totally new key, I realize that the whole hash map has to be synchronized (i.e. locked). However, if I am trying to add another String into an already contained key's corresponding LinkedList, I can just synchronize the hash map's get method. I think this will allow multiple threads to simultaneously (concurrently) add to the LinkedLists of different, already contained keys. Please let me know if I'm wrong.

Here's a concrete example. I have a hash map hashMap that uses an Integer as its key and a LinkedList as its value. The keys 5, and 10 are already in the hash map. The key 5 contains a LinkedList of Joey, Joe, Kerry. The key 10 contains the LinkedList of Jerry, Mary, Tim. I have two threads t1 and t2. t1 wants to add Moe to the LinkedList corresponding to key 5. t2 wants to add Harry to the LinkedList corresponding to key 10. Both will be concurrently added to the hash map, since the hash map's value is only locked.

解决方案

My current implementation is a synchronized method. Would this allow two different threads to put two different keys simultaneously into the hash map?

No. Only a ConcurrentHashMap or a specifically designed concurrent map would support this safely, so it's impossible for you to put two keys simultaneously into the same map from two threads.

how would I make it possible that two different threads can put different keys simultaneously (concurrently) into the hash map?

You cannot, without using ConcurrentHashMap, another ConcurrentMap implementation, or implementing your own.

这篇关于HashMap和并发 - 不同的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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