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

Tracking the progress between Queues in a Map

I have currently two queues and items traveling between them. Initially, an item gets put into firstQueue, then one of three dedicated thread moves it to secondQueue and finally another dedicated thread removes it. These moves obviously include some…
maaartinus
  • 44,714
  • 32
  • 161
  • 320
12
votes
2 answers

Wrong implementation of Oracle Java ConcurrentHashMap?

I am testing ConcurrentHashMap on Oracle's Java 8 implementation: ConcurrentMap concurrentMap = new ConcurrentHashMap<>(); String result = concurrentMap.computeIfAbsent("A", k -> "B"); System.out.println(result); // "B" result =…
user1589188
  • 5,316
  • 17
  • 67
  • 130
12
votes
7 answers

Java concurrent locks on the Map key level

There are writer which updates prices by calling putPrice method. Reader is using getPrice to get a latest price. hasChangedMethod returns a boolean identifying if price has been changed since last time getPrice has been invoked. I am looking for…
Wild Goat
  • 3,509
  • 12
  • 46
  • 87
12
votes
2 answers

ConcurrentHashMap crashing application compiled with JDK 8 but targeting JRE 7

I ran into a very unexpected error today and while I was able to find a way to fix the problem as a whole I'm not sure I completely understand why it did what it did. The code I'm working with was originally written with a JDK 7 environment of…
JRSofty
  • 1,216
  • 1
  • 24
  • 49
12
votes
2 answers

ConcurrentHashMap, which concurrent features improved in JDK8

Can any concurrent expert explain in ConcurrentHashMap, which concurrent features improved comparing with which in previous JDKs
taigetco
  • 560
  • 3
  • 13
11
votes
3 answers

ConcurrentHashMap does not work as expected

I am counting votes for electronic election and I have only one party in my initial version. There will be different threads per voter and the threads will update the votes count of a given party. I decided to use ConcurrentHashMap, but the results…
Vallerious
  • 570
  • 6
  • 13
11
votes
1 answer

Filter ConcurrentHashMap by value

I am trying to filter a ConcurrentHashMap> by the size of the LinkedList. In other words, I want to filter out elements in ConcurrentHashMap where the size of LinkedList is greater than 4. How would I get…
g0c00l.g33k
  • 2,458
  • 2
  • 31
  • 41
11
votes
2 answers

How can I perform a thread-safe get then remove with ConcurrentHashMap?

In an interview, I was asked to check whether following code works as intended. ConcurrentHashMap chm = new ConcurrentHashMap<>(); if (chm.get(key) != null) { chm.get(key).doSomething(); chm.remove(key); } According to…
Rohit Manglik
  • 347
  • 1
  • 3
  • 17
11
votes
1 answer

How to guarantee get() of ConcurrentHashMap to always return the latest actual value?

Introduction Suppose I have a ConcurrentHashMap singleton: public class RecordsMapSingleton { private static final ConcurrentHashMap payments = new ConcurrentHashMap<>(); public static ConcurrentHashMap
mkrakhin
  • 3,386
  • 1
  • 21
  • 34
11
votes
3 answers

ConcurrentHashMap returns a weakly consistent iterator, why should we use it anyhow?

I am reading the book Java Concurrecny in practice. On page 85 section 5.2.1 it talks about the ConcurrentHashMap and its advantages. However, in one part, the books claims that the iterators returned by ConcurrentHashMap is weakly consistent.…
Hossein
  • 40,161
  • 57
  • 141
  • 175
11
votes
3 answers

ConcurrentHashMap memory overhead

Does somebody know what is the memory overhead of a ConcurrentHashMap (compared to a "classical" HashMap) ? At construction ? At insertion of an element ?
Maxime
  • 1,776
  • 1
  • 16
  • 29
10
votes
4 answers

Is "ConcurrentHashMap.putAll(...)" atomic?

Is the method ConcurrentHashMap.putAll(Map) supposed to be atomic? I cannot find it in the documentation and it is not mentioned in the ConcurrentMap interface, so I guess the answer is no. I am asking it to be sure, since it wouldn't make sense if…
Japer D.
  • 754
  • 1
  • 9
  • 18
10
votes
6 answers

Does re-putting an object into a ConcurrentHashMap cause a "happens-before" memory relation?

I'm working with existing code that has an object store in the form of a ConcurrentHashMap. Within the map are stored mutable objects, use by multiple threads. No two threads try to modify an object at once by design. My concern is regarding the…
ryanlb1000
  • 198
  • 4
  • 8
10
votes
4 answers

Why ConcurrentHashMap.putifAbsent is safe?

I have been reading for concurency since yesterday and i dont know much things... However some things are starting to getting clear... I understand why double check locking isnt safe (i wonder what is the propability the rare condition to occur)…
GorillaApe
  • 3,611
  • 10
  • 63
  • 106
10
votes
3 answers

Consequences of updating other key(s) in ConcurrentHashMap#computeIfAbsent

Javadoc from ConcurrentHashMap#computeIfAbsent says The computation should be short and simple, and must not attempt to update any other mappings of this map. But, from what I see, using remove() and clear() methods inside mappingFunction works…
Vitalii Vitrenko
  • 9,763
  • 4
  • 43
  • 62