thread safe map operation

thread-safe map c++
thread-safe map java
concurrenthashmap thread-safe
how to make hashmap thread-safe
java concurrenthashmap example
concurrentmap vs concurrenthashmap
concurrenthashmap compute
concurrenthashmap vs hashmap

I came across the following piece of code ,and noted a few inconsistencies - for a multi-thread safe code.

    Map<String,Map<String,Set<String>> clusters = new HashMap<.........>;
    Map<String,Set<String>> servers = clusters.get(clusterkey);
    if(servers==null){
      synchronized(clusterkey){
       servers = clusters.get(clusterkey);
       if(servers==null){....initialize new hashmap and put...}
      }
    }
    Set<String> users=servers.get(serverkey);
    if(users==null){
      synchronized(serverkey){
       users=servers.get(serverkey);
       if(users==null){ ... initialize new hashset and put...}
      }
    }
    users.add(userid);
  1. Why would map be synchronized on clusterkey- shouldnt it be on the map as monitor itself?
  2. Shouldnt the last users.add... be synchronized too?
  3. This seems to be a lot of code to add a single user in a thread-safe manner.What would be a smarter implementation?

Here just some observations:

  1. Synchronizing on a String is a very bad idea -> sync on clusterKey and serverKey will probably not work the way intended.
  2. Better would be to use ConcurrentHashMaps and ConcurrentHashSets.

Though without more context it is not really possible to answer this question. It seems the code-author wanted to safely create just 1 mapping per clusterKey and serverKey so the user can be added just once.

A (probably better) way would be to just synchronize on the clusters map itself and then you're safe as only one thread can read and/or write to said map.

Another way would be to use custom Locks, maybe one for reading, and another one for writing, though this may lead again to inconsistencies if one thread is writing to the Map while another is reading that exact value from it.

thread safe map operation, It accepts reading operations but disallows writing operations: synchronizedMap(), the map is thread-safe for every method invoked on the� These collections, which include the commonly used ArrayList and Hashtable, provide some thread-safety through the Synchronized property, which returns a thread-safe wrapper around the collection. The wrapper works by locking the entire collection on every add or remove operation.


The code looks like a non-thought through version of the Double checked locking idiom that sometimes is used for lazy initialisation. Read the provided link for why this is a really bad implementation of it.

The problem with the given code is that it fails intermittently. There is a race condition when there are several threads trying to work on the map using the same key (or keys with the same hashcode) which means that the map created first might be replaced by the second hashmap.

Java Map and thread-safety, It is thread safe without synchronizing the whole map . Reads can Every read/ write operation needs to acquire lock. Locking the entire� Atomic classes allow us to perform atomic operations, which are thread-safe, without using synchronization. An atomic operation is executed in one single machine level operation. To understand the problem this solves, let's look at the following Counter class:


1 -The synch is trying to avoid that two threads, at the same time, create a new Entry in that Map. The second one must wait so his (servers==null) doesn't also return true.

2 - That users list seems to be out of scope, but seems like it doesn't need a synch. Maybe the programmer knows there is no duplicated userIds, or maybe he doesn't care about resetting the same user again and again.

3- ConcurrentHashMap maybe?

HashMap Vs. ConcurrentHashMap Vs. SynchronizedMap, will help you understand ConcurrentHashMap - a thread-safe map defined in A ConcurrentMap defines the following atomic operations:. thread safe map operation. Ask Question Asked 9 months ago. Active 9 months ago. Viewed 67 times 1. 1. I came across the following piece of code ,and noted a few


Java Concurrent Collection, ConcurrentHashMap class is thread-safe i.e. multiple thread can operate on a Creates a new, empty map with the specified initial capacity and load factor and� When Thread 1 accesses the method at t1, Thread 2 may not be done with the method. So the value returned to Thread 1 is the value that has not been increased. Make a method thread-safe - Method 1. Adding synchronized to this method will makes it thread-safe. When synchronized is added to a static method, the Class object is the object which is


ConcurrentHashMap in java, If we need to perform thread-safe operations on it then we must need to synchronizedMap(hashmap) it returns a thread-safe map backed up by the specified� An object is thread-safe for reading from multiple threads. For example, given an object A, it is safe to read A from thread 1 and from thread 2 simultaneously. If an object is being written to by one thread, then all reads and writes to that object on the same or other threads must be protected.


How to synchronize HashMap in Java with example, This is better than old tactical solutions like using a concurrent hash map with dummy also add new elements along with performing other set operations e.g. contains(), Even though, CopyOnWriteArraySet is thread-safe it is not suitable for� The main thread runs the BackgroundWorker.ProgressChanged and BackgroundWorker.RunWorkerCompleted event handlers, which can call the main thread's controls. To make a thread-safe call by using BackgroundWorker, create a method in the background thread to do the work, and bind it to the DoWork event.