Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use ConcurrentSkipListMap?

In Java, ConcurrentHashMap is there for better multithreading solution. Then when should I use ConcurrentSkipListMap? Is it a redundancy?

Does multithreading aspects between these two are common?

like image 242
DKSRathore Avatar asked Nov 28 '09 06:11

DKSRathore


People also ask

How ConcurrentSkipListMap works?

Methods of ConcurrentSkipListMap. Returns a key-value mapping associated with the least key greater than or equal to the given key, or null if there is no such entry. Returns the least key greater than or equal to the given key, or null if there is no such key. Removes all of the mappings from this map.

What is concurrent skip list?

The ConcurrentSkipListSet class in Java is a part of the Java Collection Framework and implements the Collection interface and the AbstractSet class. It provides a scalable and concurrent version of NavigableSet in Java.

Is ConcurrentHashMap ordered?

ConcurrentHashMap are sorted, not ordered like LinkedHashMap are and, I believe, can't thus be the required concurent alternative to a LinkedHashMap.


1 Answers

These two classes vary in a few ways.

ConcurrentHashMap does not guarantee* the runtime of its operations as part of its contract. It also allows tuning for certain load factors (roughly, the number of threads concurrently modifying it).

ConcurrentSkipListMap, on the other hand, guarantees average O(log(n)) performance on a wide variety of operations. It also does not support tuning for concurrency's sake. ConcurrentSkipListMap also has a number of operations that ConcurrentHashMap doesn't: ceilingEntry/Key, floorEntry/Key, etc. It also maintains a sort order, which would otherwise have to be calculated (at notable expense) if you were using a ConcurrentHashMap.

Basically, different implementations are provided for different use cases. If you need quick single key/value pair addition and quick single key lookup, use the HashMap. If you need faster in-order traversal, and can afford the extra cost for insertion, use the SkipListMap.

*Though I expect the implementation is roughly in line with the general hash-map guarantees of O(1) insertion/lookup; ignoring re-hashing

like image 75
Kevin Montrose Avatar answered Sep 25 '22 02:09

Kevin Montrose