Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using HashMap in multithreaded environment

I was going through an interview question on JavaRevisited and I'm having difficulty understanding this question :

What’s wrong with using a HashMap in a multithreaded environment? When get() method go into an infinite loop?

In my opinion, it's not a problem to use HashMap inside a multi-threaded environment, as long as our application is not accessing/reading threads which are modifying the created HashMap, rather than simply accessing the HashMap.

So, as I see it, there's not a problem as long as in the application we are just accessing the HashMap in a multi-threaded environment.

Please let me know if my understanding is correct.

like image 451
Pawan Avatar asked Jun 15 '12 12:06

Pawan


People also ask

Can HashMap be used in multithreaded environment?

No. As mentioned above the main difference between both of this collection is of concurrency HashMap is no thread safe. On other hand ConcurrentHashMap is thread safe and fit for use in a multi-threaded environment.

Why HashMap should not be used in multithreaded environment can it cause infinite loop as well?

The default capacity of HashMap is 16 and Load factor is 0.75, which means HashMap will double its capacity when 12th Key-Value pair enters in the map (16 * 0.75 = 12). When 2 thread tries to access HashMap simultaneously, then you may encounter infinite loop.

Is HashMap in Java thread-safe?

And, importantly, HashMap is not a thread-safe implementation, while Hashtable does provide thread-safety by synchronizing operations. Even though Hashtable is thread safe, it is not very efficient.

Which collection will use in multithreaded environment?

The problems which occurs while using Collections in Multi-threaded application: Most of the Collections classes objects (like ArrayList, LinkedList, HashMap etc) are non-synchronized in nature i.e. multiple threads can perform on a object at a time simultaneously.


2 Answers

What’s wrong using HashMap in multithreaded environment? When get() method go to infinite loop?

It is a bug to have multiple threads use a non-synchronized collection (really any mutable class) in an unprotected manner. Certain if each thread had their own HashMap instance then this is not an issue. It is a problem if multiple threads are adding to the same HashMap instance without it being synchronized. Even if just 1 thread is modifying a HashMap and other threads are reading from that same map without synchronization, you will run into problems.

If you need to use the same hash table object in multiple threads then you should consider using ConcurrentHashMap, wrapping each of the accesses to the HashMap in a synchronized {} block, or making use of the Collections.synchronizedMap(new HashMap<...>()) construct.

Chances are that the get() goes to an infinite loop because one of the threads has only a partially updated view of the HashMap in memory and there must be some sort of object reference loop. That's the peril of using an unsynchronized collection with multiple threads.

So in my understanding, it's not a problem as long as in the application we are just accessing the HashMap in a multi-threaded environment?

If by "accessing" you mean "reading", then this is true with qualifications. You must make sure:

  • All of the updates to the HashMap are completed before the threads are instantiated and the thread that creates the map also forks the threads
  • The threads are only using the HashMap in read-only mode – either get() or iteration without remove
  • There are no threads updating the map

If any of these conditions are not true then you will need to use a synchronized map instead.

like image 195
Gray Avatar answered Sep 24 '22 06:09

Gray


This is a classical question. ArrayList and HashMap are not synchronized, while Vector and HashTable are. You should therefore use HashTable unless you are very careful defining mutexes yourself.

In other words, the methods in e.g. HashTable will ensure that no other thread is working with the HashTable at any given time. If you use a HashMap, you'd have to do that manually by ensuring that you synchronize on HashMap before you call the method.

Update: checkout @Gray's comment. It looks like wrapping HashMap with Collections.synchronizedMap(new HashMap()) is the way to go now.

EDIT: other posters have answered way better than I did. My answer, however, generated an interesting discussion on the use of the soon to be deprecated Vector, Stack, Hashtable and Dictionary classes, so I'm leaving the question here, as a head to the comments below. Thanks guys!

like image 38
Miquel Avatar answered Sep 22 '22 06:09

Miquel