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
5
votes
1 answer

Kotlin Concurrency with ConcurrentHashMap while retrieving and removing without using locks

I am trying to understand ConcurrentHashMap and see if I can leverage it w/o adding any locks on my side. I have a ConcurrentHashMap with number of books at the beginning of a day. class Z { val books: ConcurrentHashMap =…
5
votes
3 answers

Why does ConcurrentHashMap use a local variable `tab` to reference the table?

In ConcurrentHashMap.putVal() (JDK Version: 11; ConcurrentHashMap.java; line 1010) final V putVal(K key, V value, boolean onlyIfAbsent) { if (key == null || value == null) throw new NullPointerException(); int hash = spread(key.hashCode()); …
5
votes
1 answer

Does Java ConcurrentHashMap computeIfAbsent() method support key-based "locking"?

https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html#computeIfAbsent-K-java.util.function.Function- Assume we have 10 threads calling the following codes with different key value. does the "Function" argument…
Rocketstella
  • 75
  • 1
  • 4
5
votes
2 answers

Whats the best way to obtain a concurrent hash set when write operations are excess than read operations?

I found that we can obtain a concurrent hash set using newKeySet(); or with keySet(default value) from a ConcurrentHashMap. Is this the best way to create a thread-safe set when write operations are excess than the read operation. I read about…
Renjith K N
  • 2,613
  • 2
  • 31
  • 53
5
votes
2 answers

Java 8+ ConcurrentHashMap lock striping

I have been reading through Concurency in Practice by Brian Goetz. In the chapter about Lock Striping it is written that ConcurrentHashMap uses 16 buckets to improve multithreaded access by many threads : Lock splitting can sometimes be extended to…
Michał Krzywański
  • 15,659
  • 4
  • 36
  • 63
5
votes
2 answers

Performance of ConcurrentHashMap.putIfAbsent

In his talk about Effective Java at 54:15 Joshua Bloch recommends to use get before putIfAbsent in order to improve performance and concurrency. This leads me to the question why this optimization is already not build in like public V…
maaartinus
  • 44,714
  • 32
  • 161
  • 320
5
votes
1 answer

Java Map merge vs computeIfAbsent and computeIfPresent

I have used compute method as well as merge method. However I am still not sure how merge method differs from compute methods. I was asked a question in interview to maintain a counter of hits for given list of IP addresses. It was a basic…
Eager
  • 225
  • 1
  • 8
5
votes
2 answers

When to use concurrenthashmap vs hashmap for put and get

In an interview question I was asked to explain a situation when using a concurrenthashmap would be the right way vs using a hashmap. On the board there were two columns t1 and t2 (corresponding to thread1 and thread2), and I was supposed to write a…
apadana
  • 13,456
  • 15
  • 82
  • 98
5
votes
1 answer

Do I need to synchronize ConcurrentMap when adding key only if needed?

I have a ConcurrentMap object. I want to write a method that would return the SomeObject value if it exists, or create a new SomeObject, put it in the Map, and return it if it doesn't exist. Ideally, I could use ConcurrentMap's…
isapir
  • 21,295
  • 13
  • 115
  • 116
5
votes
4 answers

Are there any drawbacks with ConcurrentHashMap?

I need a HashMap that is accessible from multiple threads. There are two simple options, using a normal HashMap and synchronizing on it or using a ConcurrentHashMap. Since ConcurrentHashMap does not block on read operations it seems much better…
Thilo
  • 257,207
  • 101
  • 511
  • 656
5
votes
2 answers

Deduplication for String intern method in ConcurrentHashMap

I watched a code from JavaDays, author said that this approach with probability is very effective for storing Strings like analogue to String intern method public class CHMDeduplicator { private final int prob; private final Map
pacman
  • 797
  • 10
  • 28
5
votes
1 answer

Thread-safe access to multiple concurrent data structures

How can we access multiple concurrent data structures while preserving thread safety? Is it possible to do this without synchronization? As a simple example: ConcurrentHashmap m; CopyOnWriteArrayList l; public bool enterListNode(int elem) { …
5
votes
1 answer

Use of bit wise shift operator in ConcurrentHashMap

As I was going through the ConcurrentHashMap source code, I have encountered so many bit wise shift operator. Some are applied on to create constants and some are on variables. static final int MAXIMUM_CAPACITY = 1 << 30; static final int…
arham
  • 167
  • 1
  • 3
5
votes
1 answer

Java ConcurrentHashMap putIfAbsent slow down after running for about a day or more

I have a java web application deployed under tomcat, and suddenly the response time of a API slowed down as snapshot shows (sorry, I cannot post images as lack of reputations.). It would get back to normal after restarted tomcat. The only suspect…
Ivan
  • 61
  • 2
5
votes
3 answers

ConcurrentHashMap changes visible to all the threads?

I have a CHM defined as below. I am calling setDataProcess method from a single background thread whenever there is any update. And I am calling getDataMapping from multiple reader threads always. private static final ConcurrentHashMap
john
  • 11,311
  • 40
  • 131
  • 251