Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: multi-threaded maps: how do the implementations compare?

I'm looking for a good hash map implementation. Specifically, one that's good for creating a large number of maps, most of them small. So memory is an issue. It should be thread-safe (though losing the odd put might be an OK compromise in return for better performance), and fast for both get and put. And I'd also like the moon on a stick, please, with a side-order of justice.

The options I know are:

  • HashMap. Disastrously un-thread safe.

  • ConcurrentHashMap. My first choice, but this has a hefty memory footprint - about 2k per instance.

  • Collections.sychronizedMap(HashMap). That's working OK for me, but I'm sure there must be faster alternatives.

  • Trove or Colt - I think neither of these are thread-safe, but perhaps the code could be adapted to be thread safe.

Any others? Any advice on what beats what when? Any really good new hash map algorithms that Java could use an implementation of?

Thanks in advance for your input!

like image 451
Daniel Winterstein Avatar asked May 20 '10 23:05

Daniel Winterstein


People also ask

Which map implementation is safe for modification in a multi-threaded program?

ConcurrentHashMap is ideal for high-performance, multi-threaded applications. If you need to access and update a map from multiple threads, ConcurrentHashMap is the best option. It provides all the operations of a HashMap and additionally allows concurrent access for read, write, and update.

Why HashMap should not be used for multi-threaded environments?

The HashMap is non-thread-safe and can not be used in a Concurrent multi-threaded environment. Comparatively, ConcurrentHashMap is a thread-safe and specially designed for use in multi-threaded and Concurrent environment.

Which map is best suited to a multi-threaded environment?

In a multi-threading environment, where multiple threads are expected to access a common Map, the ConcurrentHashMap is clearly preferable. However, when the Map is only accessible to a single thread, HashMap can be a better choice for its simplicity and solid performance.

How does Java handle multiple threads?

Multithreading is a Java feature that allows concurrent execution of two or more parts of a program for maximum utilization of CPU. Each part of such program is called a thread. So, threads are light-weight processes within a process.


2 Answers

Collections.synchronizedMap() simply makes all the Map methods synchronized.

ConcurrentMap is really the interface you want and there are several implementations (eg ConcurrentHashMap, ConcurrentSkipList). It has several operations that Map doesn't that are important for threadsafe operations. Plus it is more granular than a synchronized Map as an operation will only lock a slice of the backing data structure rather than the entire thing.

like image 76
cletus Avatar answered Oct 07 '22 13:10

cletus


I have no experience of the following, but I worked with a project once who swore by Javolution for real time and memory sensitive tasks.

I notice in the API there is FastMap that claims to be thread safe. As I say, I've no idea if it's any good for you, but worth a look:

API for FastMap

Javolution Home

like image 32
Dick Chesterwood Avatar answered Oct 07 '22 12:10

Dick Chesterwood