Menu
  • HOME
  • TAGS

ConcurrentMap#putIfAbsent with Scala Int's

scala,concurrenthashmap

Int cannot be null, nor can any AnyVal. From the scaladoc: Null is a subtype of all reference types; its only instance is the null reference. Since Null is not a subtype of value types, null is not a member of any such type. For instance, it is not possible...

Java 8 ConcurrentHashMap merge vs computeIfAbsent

multithreading,concurrency,merge,java-8,concurrenthashmap

Focus on what have to be done, if the value is absent. What you have to do, is to create a new Set value for the absent entry. Of course, if you use an operation which has an atomicity guaranty for the creation of the Set only, adding to the...

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

java,recursion,java-8,concurrenthashmap

This is fixed in JDK-8062841. In the 2011 proposal, I identified this issue during the code review. The JavaDoc was updated and a temporary fix was added. It was removed in a further rewrite due to performance issues. In the 2014 discussion, we explored ways to better detect and fail....

Lock+HasMap or ConcurrentHashMap in my case?

java,concurrency,thread-safety,java-7,concurrenthashmap

So you have two separate atomic instructions which need to be synchronized with. Putting a new queue into the Map Putting an object into the Queue Here are my suggestions. Continue to use a ConcurrentHashMap. You still need to put into the map and you may as well do it...

Java Remove Specific Item From ConcurrentHashMap

java,thread-safety,java.util.concurrent,concurrenthashmap

The remove method does synchronize on a lock. Indeed checking the code of ConcurrentHashMap#remove(), there is a call to a lock method that acquires the lock: public V remove(Object key) { int hash = hash(key.hashCode()); return segmentFor(hash).remove(key, hash, null); } where ConcurrentHashMap.Segment#remove(key, hash, null) is defined as: V remove(Object key,...

In memory cache using concurrentHashmap

java,caching,concurrenthashmap,linkedhashmap

Have a look at ExpiringMap. You can place inner map objects inside, and they will be automatically expired as you desire.

Java web service seemingly not storing variable?

java,server,concurrenthashmap

Your problem has noting to with webservice. Issue is with you logic Modify your methods as follows : public String addWord(String word, String definition) { if (dictionary.containsKey(word.toLowerCase())) { return "This word is already in the dictionary, " + "please use the update function."; } else { dictionary.put(word.toLowerCase(), definition); return "Added";...

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

performance,scala,thread-safety,akka,concurrenthashmap

In general, java.util.concurrent.ConcurrentHashMap is made for concurrent use. As long as you don't try to transport the closure to another machine, and you think through the implications of it being used concurrently (e.g. if you read a value, use a function to modify it, and then put it back, do...

Implementation independent way to see if a map contains null key

java,collections,null,hashmap,concurrenthashmap

I think this would do the trick: private static Map<String, String> m = new ConcurrentHashMap<>(); public static void main(String[] args) { boolean hasNullKey = false; try { if (m != null && m.containsKey(null)) { hasNullKey = true; } } catch (NullPointerException npe) { // Relies on the fact that you...

Generic HashMap with Lists and putIfAbsent

java,generics,hashmap,concurrenthashmap

Instance of your class will be also HashMap so you don't need to, or even shouldn't add another field just to support getOrAdd method because other inherited and not overridden methods will not be referring to map field but to this instance. So instead of adding separate field private HashMap<K,...

Double mapping with HashMap

java,synchronization,hashmap,concurrenthashmap,double-checked-locking

Ok, so I did some research. According to this document the put operation has a happens-before relation with contains keys. Hence if we use a ConcurrentHashMap the double-checked locking should work

Behavior of entrySet().removeIf in ConcurrentHashMap

java,multithreading,java.util.concurrent,concurrenthashmap,concurrentmodification

I also managed to reproduce such case on my machine. I think, the problem is that EntrySetView (which is returned by ConcurrentHashMap.entrySet()) inherits its removeIf implementation from Collection, and it looks like: default boolean removeIf(Predicate<? super E> filter) { Objects.requireNonNull(filter); boolean removed = false; final Iterator<E> each = iterator(); while...

Concurrent Hash Map get and put overlapping

concurrency,concurrenthashmap,thread-synchronization

According to JDK7 Javadoc: Retrievals reflect the results of the most recently completed update operations holding upon their onset. This means get() will return null if it was invoked before put() completed. The short answer to your question is: put() will return null....

Java Concurrency

java,multithreading,hashmap,concurrenthashmap

Java 8: private final Map<String, AtomicInteger> cnts = new ConcurrentHashMap<>(); private void accumulate(String name) { cnts.computeIfAbsent(name, k -> new AtomicInteger()).incrementAndGet(); } The ConcurrentHashMap can be freely accessed from multiple threads. The computeIfAbsent method takes a lambda to evaluate to get a value for the key if the key is not...

ConcurrentHashMap memory allocation

java,java.util.concurrent,concurrenthashmap

From the source code, freely available and coming with the JDK: if (initialCapacity < concurrencyLevel) // Use at least as many bins initialCapacity = concurrencyLevel; // as estimated threads So, in short, if you pass arguments that don't make sense, it fixes it for you....

ConcurrentHashMap changes visible to all the threads?

java,multithreading,thread-safety,volatile,concurrenthashmap

As far as I can see, you're using a CountDownLatch in the read method to wait until the count reaches zero. The count is apparently the number of enum constants in ProcessType. If this CountDownLatch is properly implemented, there shouldn't be a problem since according to the javadocs, there should...

Concurrenthashmap Iterator data structure

java,collections,concurrenthashmap

As mentioned in the comments, I'm not convinced that ConcurrentHashMap behaviour is "well understood". The assertion made in your unattributed quote is not true for ConcurrentHashMap. The iterator for a ConcurrentHashMap is weakly consistent... the javadoc states: Iterators, Spliterators and Enumerations return elements reflecting the state of the hash table...

Best way to pass a ConcurrentHashMap to another class?

java,thread-safety,singleton,concurrenthashmap

There is no such thing as the best way to pass a ConcurrentHashMap to another class. It all depends on your use case, i.e. on what you want your classes to do. You have three options: Return the map as is: return myStuff; This will let your other classes modify...

Calculating average and percentiles from a histogram map?

java,multithreading,timer,thread-safety,concurrenthashmap

The mean is straightforward to implement. Median is the 50th percentile, so you just need a single percentile method that works, and create a utility method for the median. There are several variations of Percentile calculation, but this one should generate the same results as the Microsoft Excel PERCENTILE.INC function....

Should I use a ConcurrentHashMap?

java,hashmap,concurrenthashmap

If you are on different threads or otherwise the data will be operated on at the same time (multithreaded delegate or the alike) , yes, use ConcurrentHashMap. Otherwise, HashMap should do (given the information you've provided). Based on reading your pseudo code, I get the impression that you are not...

How to populate concurrenthashmap from multiple threads?

java,multithreading,thread-safety,concurrenthashmap

Whether this is safe depends on what you mean. It won't throw exceptions or corrupt the map, but it can skip updates. Consider: Thread1: errorMap.get(error) returns 1 Thread2: errorMap.get(error) returns 1 Thread1: errorMap.put(error, 1+1); Thread2: errorMap.put(error, 1+1); A similar race exists around the keySet().contains(error) operation. To fix this you'll need...

accessing data from concurrentHashMap while it gets updated in background android

android,hashmap,java.util.concurrent,concurrenthashmap

If you are using ConcurrentHashMap, then there is not point it will not work. I strongly doubt that you are trying to access wrong index or you do not have synchronization between inserting and accessing the particular index. For that just check the size of udata and value of xcor...

Java wordcount: a mediocre implementation

java,multithreading,concurrenthashmap,word-count,worker-pool

Some thoughts on a little of most .. .. I believe some sort of worker pool or executor service could solve this problem (I have not learned the syntax for this yet). If all the threads take about the same time to process the same amount of data, then there...