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
28
votes
4 answers

Since Java 9 HashMap.computeIfAbsent() throws ConcurrentModificationException on attempt to memoize recursive function results

Today I learned from some JS course what memoization is and tried to implement it in Java. I had a simple recursive function to evaluate n-th Fibonacci number: long fib(long n) { if (n < 2) { return n; } return fib(n - 1) +…
Kirill
  • 6,762
  • 4
  • 51
  • 81
28
votes
3 answers

Need simple explanation how "lock striping" works with ConcurrentHashMap

According to Java Concurrency in Practice, chapter 11.4.3 says: Lock splitting can sometimes be extended to partition locking on a variablesized set of independent objects, in which case it is called lock striping. For example, the…
GedankenNebel
  • 2,437
  • 5
  • 29
  • 39
27
votes
3 answers

ConcurrentHashMap JDK 8 when to use computeIfPresent

The new version of Concurrent Hash Map of jdk 8 has two new Methods. computeIfAbsent computeIfPresent putIfAbsent - Old method I understand the use cases of putIfAbsent and computeIfAbsent. But i am not sure of the scenarios when i will use…
veritas
  • 2,444
  • 1
  • 21
  • 30
25
votes
2 answers

Behavior of entrySet().removeIf in ConcurrentHashMap

I would like to use ConcurrentHashMap to let one thread delete some items from the map periodically and other threads to put and get items from the map at the same time. I'm using map.entrySet().removeIf(lambda) in the removing thread. I'm…
24
votes
3 answers

Is it possible for ConcurrentHashMap to "deadlock"?

We have come across a strange issue with ConcurrentHashMap, where two threads appears to be calling put(), and then waiting forever inside the method Unsafe.park(). From the outside, it looks like a deadlock inside ConcurrentHashMap. We have only…
Simon Nickerson
  • 42,159
  • 20
  • 102
  • 127
23
votes
4 answers

ConcurrentHashMap constructor parameters?

I am wondering about the parameters for constructing a ConcurrentHashMap: initialCapacity is 16 by default (understood). loadFactor is 0.75 by default. concurrencyLevel is 16 by default. My questions are: What criteria should be used to adjust…
non sequitor
  • 18,296
  • 9
  • 45
  • 64
23
votes
4 answers

ConcurrentHashMap with weak keys and identity hash?

How do I get a ConcurrentHashMap with weak keys and identity hashes in Java? I think Google Guava Collections can give such a thing, but can I get it from the standard library? What other options do I have?
r.v
  • 4,697
  • 6
  • 35
  • 57
21
votes
5 answers

ConcurrentHashMap stuck in infinite loop - Why?

While doing some in-depth analysis of ConcurrentHashMap, Found a blog post on the internet which says even ConcurrentHashMap may get stuck in an infinite loop. It gives this example. When I ran this code - it got stuck: public class Test { …
T-Bag
  • 10,916
  • 3
  • 54
  • 118
21
votes
2 answers

ConcurrentHashMap.newKeySet() vs Collections.newSetFromMap()

Java 8 introduced new way to obtain a concurrent Set implementation // Pre-Java-8 way to create a concurrent set Set oldStyle = Collections.newSetFromMap(new ConcurrentHashMap<>()); // New method in Java 8 Set newStyle =…
turbanoff
  • 2,439
  • 6
  • 42
  • 99
20
votes
3 answers

Why ConcurrentHashMap cannot have a lock for each bucket?

As we know, java's ConcurrentHashMap has a number of internal locks, and each of them guards some region of the bucket's array. A question is: why cannot we create a lock for each bucket? A similair question was already asked: Disadvantage of…
MiamiBeach
  • 3,261
  • 6
  • 28
  • 54
20
votes
4 answers

combine putIfAbsent and replace with ConcurrentMap

I have a usecase where I have to insert a new value if the key does not exist in the ConcurrentHashMap replace the old value with a new value if the key already exists in the ConcurrentHashMap, where the new value is derived from the old value (not…
Holger
  • 407
  • 4
  • 12
19
votes
5 answers

Implementing a cache using a java ConcurrentHashMap

I'd like to implement a simple caching of heavyweight objects in a web java application. But I can't figure out how to do it properly. Am I missing something or ConcurrentHashMap methods (putIfAbsent, ...) are not enough and additional…
Paolo1976
  • 193
  • 1
  • 1
  • 4
19
votes
3 answers

ConcurrentHashMap locking

I have read somewhere that in ConcurrentHashMap, the whole map object is not locked and instead a lock is made on a portion of the Map. Can somebody elaborate when does locking come into the picture? Is it right that while reading the Map there is…
Anand
  • 20,708
  • 48
  • 131
  • 198
18
votes
4 answers

Android app building with the wrong JDK(?) somehow

I've recently updated my Android project for Android Studio 3. I wanted to support Java 8 language features, so added the following to build.gradle: compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility…
18
votes
6 answers

How to implement ConcurrentHashMap with features similar in LinkedHashMap?

I have used LinkedHashMap with accessOrder true along with allowing a maximum of 500 entries at any time as the LRU cache for data. But due to scalability issues I want to move on to some thread-safe alternative. ConcurrentHashMap seems good in that…
1
2
3
54 55