Questions tagged [concurrenthashmap]

The Java ConcurrentHashMap data structure. The ConcurrentHashmap is a hash table supporting full concurrency of retrievals and adjustable expected concurrency for updates. It allows concurrent modification of the Map from several threads without the need to block them

The Java ConcurrentHashMap data structure.

This class provides a thread-safe variant of HashMap.

811 questions
171
votes
5 answers

Is iterating ConcurrentHashMap values thread safe?

In javadoc for ConcurrentHashMap is the following: Retrieval operations (including get) generally do not block, so may overlap with update operations (including put and remove). Retrievals reflect the results of the most recently completed update…
Palo
  • 10,591
  • 7
  • 28
  • 34
162
votes
7 answers

Why does ConcurrentHashMap prevent null keys and values?

The JavaDoc of ConcurrentHashMap says this: Like Hashtable but unlike HashMap, this class does not allow null to be used as a key or value. My question: Why? 2nd question: Why doesn't Hashtable allow null? I've used a lot of HashMaps for storing…
Marcel
  • 3,749
  • 6
  • 29
  • 35
102
votes
3 answers

Is gcc std::unordered_map implementation slow? If so - why?

We are developing a highly performance critical software in C++. There we need a concurrent hash map and implemented one. So we wrote a benchmark to figure out, how much slower our concurrent hash map is compared with std::unordered_map. But,…
Markus Pilman
  • 3,104
  • 3
  • 22
  • 31
93
votes
6 answers

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?
DKSRathore
  • 3,043
  • 6
  • 27
  • 36
79
votes
3 answers

Recursive ConcurrentHashMap.computeIfAbsent() call never terminates. Bug or "feature"?

Some time ago, I've blogged about a Java 8 functional way of calculating fibonacci numbers recursively, with a ConcurrentHashMap cache and the new, useful computeIfAbsent() method: import java.util.Map; import…
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
76
votes
6 answers

Should you check if the map containsKey before using ConcurrentMap's putIfAbsent

I have been using Java's ConcurrentMap for a map that can be used from multiple threads. The putIfAbsent is a great method and is much easier to read/write than using standard map operations. I have some code that looks like…
Chris Dail
  • 25,715
  • 9
  • 65
  • 74
63
votes
6 answers

ConcurrentHashMap in Java?

What is the use of ConcurrentHashMap in Java? What are its benefits? How does it work? Sample code would be useful too.
Praveen
  • 90,477
  • 74
  • 177
  • 219
48
votes
3 answers

Understanding code of ConcurrentHashMap compute method

Just found this strange code in ConcurrentHashMap compute method: (line 1847) public V compute(K key, BiFunction remappingFunction) { ... Node r = new ReservationNode(); …
AdamSkywalker
  • 11,408
  • 3
  • 38
  • 76
47
votes
2 answers

Does a ConcurrentHashMap need to be wrapped in a synchronized block?

Do all non-retreival operations on a ConcurrentHashMap (put(), remove() etc.) need to be wrapped in a synchronized(this) block? I understand that all of these operations are thread-safe, so is there any real benefit/need in doing so? The only…
MeanwhileInHell
  • 6,780
  • 17
  • 57
  • 106
45
votes
2 answers

Why doesn't Java ship with a CopyOnWriteMap?

The JDK ships with CopyOnWrite* implementations for Set and List, but none for Map and I've often lamented this fact. I know there are other collections implementations out there that have them, but it would be nice if one shipped as standard. It…
sgargan
  • 12,208
  • 9
  • 32
  • 38
43
votes
7 answers

ConcurrentHashMap: avoid extra object creation with "putIfAbsent"?

I am aggregating multiple values for keys in a multi-threaded environment. The keys are not known in advance. I thought I would do something like this: class Aggregator { protected ConcurrentHashMap> entries = …
Gene Golovchinsky
  • 6,101
  • 7
  • 53
  • 81
42
votes
6 answers

Atomically incrementing counters stored in ConcurrentHashMap

I would like to collect some metrics from various places in a web app. To keep it simple, all these will be counters and therefore the only modifier operation is to increment them by 1. The increments will be concurrent and often. The reads…
wishihadabettername
  • 14,231
  • 21
  • 68
  • 85
33
votes
8 answers

Is ConcurrentHashMap.get() guaranteed to see a previous ConcurrentHashMap.put() by different thread?

Is ConcurrentHashMap.get() guaranteed to see a previous ConcurrentHashMap.put() by different thread? My expectation is that is is, and reading the JavaDocs seems to indicate so, but I am 99% convinced that reality is different. On my production…
Stu Thompson
  • 38,370
  • 19
  • 110
  • 156
31
votes
2 answers

ConcurrentHashMap read and write locks

I am trying to find answer to these, but not able to find it on Google or in Java docs. Case 1: in ConcurrentHashMap, suppose a thread t1 is reading from segment n, and at same another thread t2 want to write on the same segment n: Question 1:…
lowLatency
  • 5,534
  • 12
  • 44
  • 70
29
votes
1 answer

Synchronizing on local variable

I noticed a weird construct in ConcurrentHashMap's compute and computeIfAbsent methods: Node r = new ReservationNode(); synchronized (r) { //... } What is the point of synchronizing on a local object considering that the JIT will most…
assylias
  • 321,522
  • 82
  • 660
  • 783
1
2 3
54 55