FAQ Database Discussion Community

How to populate concurrenthashmap from multiple threads?

I have a ConcurrentHashMap which I am populating from multiple threads. private static Map<DataCode, Long> errorMap = new ConcurrentHashMap<DataCode, Long>(); public static void addError(DataCode error) { if (errorMap.keySet().contains(error)) { errorMap.put(error, errorMap.get(error) + 1); } else { errorMap.put(error, 1L); } } My above addError method is called from multiple threads which...

Recursive ConcurrentHashMap.computeIfAbsent() call never terminates. Bug or “feature”?

Some time ago, I've blogged about a Java 8 functional way of calculating fibonacci numbers recursively, with a ConcurrentHashMap cache and the new, useful computeIfAbsent() method: import java.util.Map; import java.util.concurrent.ConcurrentHashMap; public class Test { static Map<Integer, Integer> cache = new ConcurrentHashMap<>(); public static void main(String[] args) { System.out.println( "f(" +...

accessing data from concurrentHashMap while it gets updated in background android

I have a static concurrentHashMap object which is been updated in background. While it is getting updated, I want to access the values from it in another thread. I am using concurrentHashMap which I understand from the documentation and thinks that it would suit this scenrio Here is what I...

ConcurrentMap#putIfAbsent with Scala Int's

java.util.concurrent.ConcurrentMap's putIfAbsent docs say the following about the return type: the previous value associated with the specified key, or null if there was no mapping for the key. On Scala 2.10.4, I tried to call putIfAbsent(Int, Int), i.e. with Scala's Int type. scala> import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap scala> new ConcurrentHashMap[Int,...

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 to do further analysis, like...

Closing over java.util.concurrent.ConcurrentHashMap inside a Future of Actor's receive method?

I've an actor where I want to store my mutable state inside a map. Clients can send Get(key:String) and Put(key:String,value:String) messages to this actor. I'm considering the following options. Don't use futures inside the Actor's receive method. In this may have a negative impact on both latency as well as...

In memory cache using concurrentHashmap

6 currently. I am creating a map in my java memory to keep last excessed data. I used concurrenthashmap for the purpose. Use case is as follows: static ConcurrentHashMap<String, Map<Long, Map<String, Object>>> concurrentCache = new ConcurrentHashMap<String, Map<Long, Map<String, Object>>>(); As you see concurrent hash map contains a key n map....

Behavior of entrySet().removeIf in ConcurrentHashMap

I would like to use ConcurrentHashMap to let one thread delete some items from the map periodically and other threads to put and get items from the map at the same time. I'm using map.entrySet().removeIf(lambda) in the removing thread. I'm wondering what assumptions I can make about its behavior. I...

Should I use a ConcurrentHashMap?

A quick question about ConcurrentHashMap: public Map<String, String> getA(){ get something from db in a HashMap lets call it x .... do some operations in on x .... put the result in ConcurrentHashMap lets call it A ..... return A } Does it make sense to have a ConcurrentHashMap or...

Lock+HasMap or ConcurrentHashMap in my case?

I have a Map<String, Queue<?>> and each time I have to put a couple (key, value) I need to get the not thread-safe Queue associated with that key and add a value to it (if key exist). Because I need to update an existing value (queue) I think that the...

Best way to pass a ConcurrentHashMap to another class?

I have a handler class that contains a private ConcurrentHashMap which other classes want, but I'm not sure the best way to give it to them. My apparent options are.. Return the entire ConcurrentHashMap (sample code below--this sounds dangerous to me, given that CHMs are meant to be used by...

Concurrent Hash Map get and put overlapping

I have read that get method is fully concurrent in ConcurrentHashMap(Jdk 7 ) and so it can overlap with all update operations. What will happen if two threads run put(Key,V) and Get (Key) concurrently if Key is not already present?

Java web service seemingly not storing variable?

EDIT: The problem was two-fold, first dictionary should be static and also i was using .contains() where i should have used .containsKey() I'm trying to do a simple java client and server set up, this is what i have got and i can't seem to find anything wrong with the...