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
17
votes
6 answers

Concurrent Map with fixed size

I need a map with the following requirements : It should be highly concurrent. The put(), get() and remove() methods may be called by multiple threads simultaneously. It should be of fixed size. If the size of the HashMap reaches to the max value…
17
votes
2 answers

How does ConcurrentHashMap handle rehashing?

I am wondering how does ConcurrentHashMap handle rehashing while another thread is still writing on another segment/partition. As far as I understand, ConcurrentHashMap locks the segment independently, so for example: If Thread1 writes to the…
peter
  • 8,333
  • 17
  • 71
  • 94
16
votes
4 answers

ConcurrentHashMap and Fibonacci Numbers - Inconsistent result

I wrote a program for calculating fibonacci numbers recursively, with a ConcurrentHashMap and computeIfAbsent() method: Program works absolutely fine when i used small values like 8,9,10 but stuck in endless loop when value increased from 10 to…
T-Bag
  • 10,916
  • 3
  • 54
  • 118
16
votes
10 answers

Lock handler for arbitrary keys

I have code which implements a "lock handler" for arbitrary keys. Given a key, it ensures that only one thread at a time can process that(or equals) key (which here means calling the externalSystem.process(key) call). So far, I have code like…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
15
votes
1 answer

Is it possible to get a deadlock with ConcurrentHashMap in this circumstance?

I'm reading the source code of ConcurrentHashMap in JDK8, notice that TreeBin uses a 'read-write' lock to prevent concurrent read and write. Read threads will go through TreeNodes if there's no concurrent write thread trying to modify the tree…
15
votes
6 answers

Print all key/value pairs in a Java ConcurrentHashMap

I am trying to simply print all key/value pair(s) in a ConcurrentHashMap. I found this code online that I thought would do it, but it seems to be getting information about the buckets/hashcode. Actually to be honest the output it quite strange, its…
DanGordon
  • 671
  • 3
  • 8
  • 26
15
votes
6 answers

Using ConcurrentHashMap, when is synchronizing necessary?

I have a ConcurrentHashMap where I do the following: sequences = new ConcurrentHashMap, AtomicLong>(); if(!sequences.containsKey(table)) { synchronized (sequences) { if(!sequences.containsKey(table)) …
Per Stilling
  • 868
  • 2
  • 9
  • 19
14
votes
3 answers

Java ConcurrentHashMap.computeIfPresent value modification visibility

Let's say I have a concurrent map with collections as value: Map map = new ConcurrentHashMap<>(); map.putIfAbsent(8, new ArrayList<>()); and I update the value as follows: map.computeIfPresent(8, (i, c) -> { c.add(5); …
14
votes
2 answers

"Undefined reference: .. ConcurrentHashMap.keySet()" when building in Java 8

i have a project, and i am build this project with jdk 6,7,8 and my target is 1.6 when i build jdk 8 i get this error: Undefined reference: java.util.concurrent.ConcurrentHashMap.KeySetView …
Bilal Yasar
  • 947
  • 2
  • 11
  • 24
13
votes
2 answers

Can we achieve read on mutable data after write atomically in ConcurrentHashMap in Java?

I'm trying to find an answer to these, but not able to understand(or confirm) it on Google or in Java docs. My implementation looks like as this: Map map = new ConcurrentHashMap(); if I do value1 =…
Mitesh Joshi
  • 177
  • 1
  • 9
13
votes
3 answers

Java ConcurrentHashMap actions atomicity

This may be a duplicate question, but I've found this part of code in a book about concurrency. This is said to be thread-safe: ConcurrentHashMap counts = new ...; private void countThing(String thing) { while (true) { …
13
votes
3 answers

Iterate over ConcurrentHashMap while deleting entries

I want to periodically iterate over a ConcurrentHashMap while removing entries, like this: for (Iterator> iter = map.entrySet().iterator(); iter.hasNext(); ) { Entry entry = iter.next(); // do…
shmosel
  • 49,289
  • 6
  • 73
  • 138
13
votes
4 answers

ConcurrentHashMap vs ConcurrentSkipListMap clarification

I'd like to clarify something about ConcurrentHashMap vs ConcurrentSkipListMap based on the API documentation. From my understanding ConcurrentHashMap gaurantees thread safety for insertions by multiple threads. So if you have a map that will only…
13
votes
2 answers

How to add null values to ConcurrentHashMap

I have a ConcurrentHashMap which is called from different threads to put values in it. I have to insert null values, but ConcurrentHashMap doesn't allow null values. Is there a way to do that or an alternate option to do this in Java?
Khawar Ali
  • 3,462
  • 4
  • 27
  • 55
13
votes
3 answers

Calculating average and percentiles from a histogram map?

I have written a timer which will measure the performance of a particular code in any multithreaded application. In the below timer, it will also populate the map with how many calls took x milliseconds. I will use this map as part of my histogram…
john
  • 11,311
  • 40
  • 131
  • 251
1 2
3
54 55