HashMap and concurrency are different keys

Let's say I have a hash map and multiple threads. If I have a synchronized method that adds to the hashmap, how would I make it possible for two different threads to put different keys (at the same time) into the hashmap at the same time?

My current implementation is a synchronized method. Could this allow two different threads to put two different keys at the same time in the hashmap?

I am using a regular hashmap, not a parallel Java hashmap. I am not allowed to use parallel hashmap.

EDIT: I think I found a solution! I think I may have written this post wrong. Let's say that the hashmap is initialized as an integer as its key and LinkedList as its value. To put in a completely new key, I understand that the entire hashmap must be in sync (i.e. locked). However, if I try to add another String to the already contained key corresponding to the LinkedList, I can simply synchronize the geth map get method. I think this will allow multiple threads to simultaneously (concurrently) add different, already contained keys to the LinkedLists. Please let me know if I am wrong.

Here's a concrete example. I have a hashMap that uses Integer as its key and LinkedList as its value. Keys 5 and 10 are already in the hashmap. Key 5 contains a LinkedList from Joey, Joe, Kerry. Key 10 contains a LinkedList from 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 added to the hashmap at the same time, since the hashmap value is only locked.

+3


source to share


4 answers


My current implementation is a synchronized method. Could this allow two different threads to put two different keys at the same time in the hashmap?

Not. Only ConcurrentHashMap

or a specially designed parallel card will support this securely, so you won't be able to put two keys on one card from two streams at the same time.



How would I make it possible for two different threads to be able to put different keys at the same time (at the same time) into the hashmap?

You cannot, without using ConcurrentHashMap

, another implementation ConcurrentMap

or implement your own.

+2


source


The simplest answer is to use ConcurrentHashMap

which does exactly what you are looking for.



If you cannot do this (I see that you edited the post after I answered) then you will have to duplicate the same as ConcurrentHashMap

. No, just synchronizing the HashMap method will prevent two threads from adding a key-value pair at the same time, they must wait and take turns.

0


source


The answer to your question is NO. Because the synchronized block makes each protector wait in a single queue. With ConcurrentHashMap

you are more likely to add concurrently because it only locks the cart into which the item will be inserted instead of locking the entire HashMap.

0


source


However, if I try to add another String to the already contained key corresponding to the LinkedList, I can just synchronize the geth map get method. I think this will allow multiple threads to simultaneously (concurrently) add different, already contained keys to the LinkedLists.

Read-only access HashMap

is safe: you can have multiple threads calling a method get

without syncing at all and nothing will break. If the linked lists are not shared between threads, then you don't need synchronization either. To prevent streams from ever sharing the list, the map key must be something specific to the stream, such as a local object or a stream ID.

It is unsafe for another thread to modify the map or list while reading or writing. This is a use case for blocking read and write : it allows multiple concurrent reads, but writes must be exclusive.

0


source







All Articles