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

I want to have both computeIfPresent and putIfAbsent into one atomic function

workObjectMap.computeIfPresent(key, (k,v) -> { v.memberIdSet.addAll(memberIdSet); v.memberPositionSet.addAll(memberPositionSet); return v; }); // If it.remove() in run() is called at this point, // there is a risk of the same work being…
Camilla
  • 160
  • 9
6
votes
2 answers

ConcurrentHashMap in Java locking mechanism for computeIfPresent

I'm using Java 8 and would like to know if the computeIfPresent operation of the ConcurrentHashMap does lock the whole table/map or just the bin containing the key. From the documentation of the computeIfPresent method: Some attempted update…
6
votes
3 answers

examples of ConcurrentHashMap

I was reading the article "Java theory and practice: Building a better HashMap" that gives an excellent overview about the implementation of ConcurrentHashMap. I also found some discussions over it on Stackoverflow here. I question though I had…
daydreamer
  • 87,243
  • 191
  • 450
  • 722
6
votes
3 answers

ConcurrentHashmap simultaneous write and get operations

I have a question about ConcurrentHashMaps. Lets say I have 2 threads. Thread A tries to get an object from a shared ConcurrentHashMap. Thread B clears the shared map. What happens if Thread A and Thread B access the shared resource simultaneously,…
Bilbo
  • 63
  • 1
  • 5
6
votes
4 answers

implementing remove on a ConcurrentMultimap without races

I've been looking at the problem of writing a concurrent Multimap, and I have an implementation backed by the Google Guava AbstractSetMultimap and a MapMaker computing map that creates on demand the values-collections as a set view over a…
Joe Kearney
  • 7,397
  • 6
  • 34
  • 45
6
votes
2 answers

How to use ConcurrentHashMap computeIfAbsent() in Scala

I'm using a ConcurrentHashMap in Scala and I would like to use the computeIfAbsent() method but can't figure out the syntax for the second argument. Can someone show me what would be the proper syntax? When running the following code val data =…
Francis
  • 379
  • 5
  • 21
6
votes
3 answers

Is read operation in ConcurrentHashMap reliable regarding the return value?

I read in a book that read in ConcurrentHashmap does not guarantee the most recently updated state and it can sometimes gives the closer value. Is this correct? I have read its javadocs and many blogs which seems to say otherwise (i.e. it is…
Popeye
  • 1,548
  • 3
  • 25
  • 39
6
votes
2 answers

tbb concurrent hash map find & insert

I am currently using tbb's concurrent hash map to perform concurrent insertions into a hash map. Each key is a string and a value is a vector of integers. I would like to achieve the following: during insertions, if the key does not exist, I insert…
NewToAndroid
  • 581
  • 7
  • 25
6
votes
4 answers

Concurrent read-only HashMap

I am writing a web-service that heavily relies on a single large Map that is completely updated once every hour. The rest of the time many threads concurrently read the table. My question is: What is the most efficient construct to realize such a…
xgb84j
  • 519
  • 2
  • 8
  • 19
6
votes
8 answers

Is this code a thread-safe one?

I want to process a flow of client requests. Each request has its special type. First I need to initialize some data for that type, and after this I can start processing the requests. When the client type comes for the first time, I just initialize…
KutaBeach
  • 1,445
  • 21
  • 43
5
votes
2 answers

Multithreaded usage of `ConcurrentHashMap` iterators

I need to write a somewhat specific implementation of a cache, which has unique keys but can contain duplicate values, e.g.: "/path/to/one" -> 1 "/path/to/two" -> 2 "/path/to/vienas" -> 1 "/path/to/du" -> 2 The class needs to offer non blocking…
mindas
  • 26,463
  • 15
  • 97
  • 154
5
votes
4 answers

ConcurrentHashMap putIfAbsent : atomicity when followed by a get() call

I wanted to discuss a specific use I have of a concurrent map to sense check my logic... If I used ConcurrentHashMap, I can do the familar private final ConcurrentHashMap map = new ConcurrentHashMap(); public V getExampleOne(K key) { …
Toby
  • 9,523
  • 8
  • 36
  • 59
5
votes
1 answer

ConcurrentHashMap non-blocking reads and memory visibility issue

ConcurrentHashMap in Java offers reads to proceed concurrently with updates. The trade-off in this is that the results of the read is limited to reflect only the last completed update when the reading began, so it is not specified to reflect the…
Bhaskar
  • 7,443
  • 5
  • 39
  • 51
5
votes
1 answer

Should we use computeIfAbsent instead of getOrPut?

Kotlin has had an extension function ConcurrentMap.getOrPut from the beginning (1.0). This has the same signature as Java's computeIfAbsent and does a similar thing, but with some subtle differences: getOrPut can put a null value into the entry,…
k314159
  • 5,051
  • 10
  • 32
5
votes
1 answer

ConcurrentHashMap.initTable(), why check table is null twice?

I'm learning the java source code, when i reading the ConcurrentHashMap source code, i'm confused with the initTable() method, why check (tab = table) == null || tab.length == 0 twice, first in the while(), then in the if(). I can't imagine in what…
peter Char
  • 75
  • 5