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
8
votes
5 answers

Java ConcurrentHashMap not thread safe.. wth?

I was using HashMap before like public Map clients = new HashMap(); now I've switched to ConcurrentHashMap to avoid synchronized blocks and now i'm experiencing problems my server is…
SSpoke
  • 5,656
  • 10
  • 72
  • 124
8
votes
1 answer

ConcurrentHashmap in JDK8 code explanation

I have been trying to understand the ConcurrentHashMap functions in JDK8, in contrast of how it was in JDK7 (which, in addition to the source code, can be found explained quite well by some nice folks out there such as Richard…
Stochastika
  • 305
  • 1
  • 2
  • 14
8
votes
2 answers

Different `next` entry of ConcurrentHashMap in JDK 1.6 and JDK 1.7

In JDK 1.6, Doug Lea uses final preceding the next field. static final class HashEntry { final K key; final int hash; volatile V value; final HashEntry next; Whereas in JDK 1.7, the next field is preceded by volatile. I…
8
votes
4 answers

Is it possible to have more than 32 locks in ConcurrentHashMap

I read ConcurrentHashMap works better in multi threading than Hashtable due to having locks at bucket level rather than map wide lock. It is at max 32 locks possible per map. Want to know why 32 and why not more than 32 locks.
DKSRathore
  • 3,043
  • 6
  • 27
  • 36
7
votes
3 answers

How does computeIfAbsent fail ConcurrentHashMap randomly?

I have the following code, it is a toy code but makes possible to reproduce the problem: import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import…
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
7
votes
1 answer

What is the difference between Segment of ConcurrentHashMap and buckets of HashMap theoretically?

I understand that in HashMap, the entries (Key, Value) are placed in buckets based on hash(Key.hashCode)--> The index that denotes the bucket location. In case an entry is already placed at that location, there is a linked list created and the new…
chepaiytrath
  • 678
  • 1
  • 9
  • 20
7
votes
3 answers

Java Concurrency : Volatile vs final in "cascaded" variables?

is final Map> status = new ConcurrentHashMap>(); Map> statusInner = new ConcurrentHashMap>(); status.put(key,statusInner); the same…
Tom
  • 165
  • 2
  • 7
7
votes
1 answer

No ConcurrentModificationException for CHM. Why?

I recently did an example where I added the element to ConcurrentHashMap while iterating over it. Code snippet - Map map = new ConcurrentHashMap(); map.put("ABC", "abc"); map.put("XYZ", "xyz"); map.put("MNO",…
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
7
votes
1 answer

Java - ThreadLocal vs ConcurrentHashMap

I have a very simple question regarding a performance difference between ThreadLocal and ConcurrentHashMap. In some places in my code I need to maintain a mapping from a Thread to some Object, which has to be thread safe. One option is to use…
Bober02
  • 15,034
  • 31
  • 92
  • 178
7
votes
1 answer

Using redis to cache java objects: why it should be better than a ConcurrentHashMap?

When profiling a java application currently under development at work, we have detected a few bottle necks that we can get rid of using caching. The application process requests and it should be as fast as possible doing so. We are considering Redis…
joanlofe
  • 3,360
  • 2
  • 26
  • 44
7
votes
3 answers

What is the preferred way to modify a value in ConcurrentHashMap?

Let's say I have a Concurrent Map that is high-read, low-write, and needs to store application data: ConcurrentMap map = new ConcurrentHashMap(); Then, during startup and through user input, data is added to the map: public…
oberger
  • 1,217
  • 2
  • 16
  • 31
6
votes
2 answers

Fast MultiMap in Multi-Thread Environments

I am currently using Guava Multimap implementation. map = Multimaps.synchronizedSetMultimap(HashMultimap. create()); However I found that my program performance is now bounded by the synchronization block synchronized (mutex) used in…
CHANist
  • 1,302
  • 11
  • 36
6
votes
1 answer

Why is the computeIfAbsent() method in ConcurrentHashMap behaving inconsistently?

I have a Java 8 web application running on Apache Tomcat 9. The invocation of ConcurrentHashMap's computeIfAbsent() method is not returning or is taking too long to return. In the code given below, the line 'Adding to Map' is printed and the line…
Nick
  • 222
  • 1
  • 2
  • 10
6
votes
2 answers

Thread-safe map with null-key capability

I need a multi-threaded Map object to use in my web server's caching, and I need to have null keys. HashMap allows me to have null keys, but ConcurrentHashMap doesn't. I tried to create a synchronized version of HashMap using…
Iravanchi
  • 5,139
  • 9
  • 40
  • 56
6
votes
2 answers

How to lock on key in a ConcurrentHashMap

I am caching an object, which is created by a thread, into a map. The creation of the object is expensive, so I don't want multiple threads running to create the object because the put() hasn't returned. Once a thread tries to create an object for…
dt94
  • 87
  • 1
  • 3
  • 9