With ConcurrentHashMap, when is synchronization required?


For each position:

I have a ConcurrentHashMap where I do the following:

sequences = new ConcurrentHashMap<Class<?>, AtomicLong>();

if(!sequences.containsKey(table)) {
    synchronized (sequences) {
        if(!sequences.containsKey(table))
            initializeHashMapKeyValue(table);
    }
}

My question is - is it necessary to do the extra

if(!sequences.containsKey(table))

Check inside a synchronized block so that other threads don't initialize the same hashmap value?

Maybe the check is necessary, but am I doing it wrong? It seems a little stupid what I'm doing, but I think it's necessary.

João Fernandes:

All operations on ConcurrentHashMap are thread-safe, but thread-safe operations are not composable. You're trying to make an atomic operation a pair of operations: check for something in the map, and if it's not there, put something in there (I think). So the answer to the question is yes , you need to double check, the code looks ok.

Related


With ConcurrentHashMap, when is synchronization required?

For each position: I have a ConcurrentHashMap where I do the following: sequences = new ConcurrentHashMap<Class<?>, AtomicLong>(); if(!sequences.containsKey(table)) { synchronized (sequences) { if(!sequences.containsKey(table)) initial

With ConcurrentHashMap, when is synchronization required?

For each position: I have a ConcurrentHashMap where I do the following: sequences = new ConcurrentHashMap<Class<?>, AtomicLong>(); if(!sequences.containsKey(table)) { synchronized (sequences) { if(!sequences.containsKey(table)) initial

ConcurrentHashMap and synchronization

Eli I'm maintaining some legacy code and found some synchronizedimplementations of the keyword on ConcurrentHashMap. It doesn't seem necessary to me: public class MyClass{ private final Map<MyObj, Map<String, List<String>>> conMap = new ConcurrentHashMap<

ConcurrentHashMap and synchronization

Eli I'm maintaining some legacy code and found some synchronizedimplementations of the keyword on ConcurrentHashMap. It doesn't seem necessary to me: public class MyClass{ private final Map<MyObj, Map<String, List<String>>> conMap = new ConcurrentHashMap<

When using synchronization, atomic references are not required

Maxim Dmitriev The code is from the Java Concurrency Guidelines by Fred Long . I understand that a set of atomic operations is not atomic. Therefore, the following code does not meet the requirements. To find the code, see page 23. public class Adder { pr

When using synchronization, atomic references are not required

Maxim Dmitriev The code is from the Java Concurrency Guidelines by Fred Long . I understand that a set of atomic operations is not atomic. Therefore, the following code does not meet the requirements. To find the code, see page 23. public class Adder { pr

Is synchronization with multiprocessor required in python?

Bobo When using code like this def execute_run(list_out): ... do something pool = ThreadPoolExecutor(6) for i in list1: for j in list2: pool.submit(myfunc, list_out) pool.join() Assuming threads modify list_out, do they operate in a sync

Synchronization is required in getters and setters

snow leopard Probably a very stupid question. Just wanted to confirm my understanding. class Test { private volatile String id; public void setID(String id) { this.id = id; } public String getID() {

Synchronization is required in asynchronous code

Cyrillic I'm trying to create a factory for an angular module that returns a JSON object received through angular $http.get(). In the for's callback function success(), I am trying to assign an object to a variable products. It seems to productsbe returned fro

Synchronization is required in getters and setters

snow leopard Probably a very stupid question. Just wanted to confirm my understanding. class Test { private volatile String id; public void setID(String id) { this.id = id; } public String getID() {

Is synchronization with multiprocessor required in python?

Bobo When using code like this def execute_run(list_out): ... do something pool = ThreadPoolExecutor(6) for i in list1: for j in list2: pool.submit(myfunc, list_out) pool.join() Assuming threads modify list_out, do they operate in a sync

Synchronization is required in getters and setters

snow leopard Probably a very stupid question. Just wanted to confirm my understanding. class Test { private volatile String id; public void setID(String id) { this.id = id; } public String getID() {

Only one thread is required for synchronization

Chapter 135 If I have a class that extends Thread with static methods (this is very simplified): public class MyThread extends Thread { private static long SLEEP_INT = 30000; private static Map<Integer, String> myData; //every 30 seconds, update

Concurrency issues when using ConcurrentHashMap

user4759317: I've been working on a REST API as part of some tricks. The current implementation has a small concurrency issue when inserting objects into the ConcurrentHashMap. My code checks to see if the consumed JSON contains an ID. If not, create a new uni

Concurrency issues when using ConcurrentHashMap

user4759317: I've been working on a REST API as part of some tricks. The current implementation has a small concurrency issue when inserting objects into the ConcurrentHashMap. My code checks to see if the consumed JSON contains an ID. If not, create a new uni

Concurrency issues when using ConcurrentHashMap

user4759317: I've been working on a REST API as part of some tricks. The current implementation has a small concurrency issue when inserting objects into the ConcurrentHashMap. My code checks to see if the consumed JSON contains an ID. If not, create a new uni

When is AtomicInteger preferable to synchronization?

ef2011: Why would I use an AtomicInteger since AtomicIntegerit's at least an order of magnitude slower than the intprotected object ?synchronized For example, if I just want to increment a value in a intthread-safe way , why not always use: synchronized(thread

When is AtomicInteger preferable to synchronization?

ef2011: Why would I use an AtomicInteger since AtomicIntegerit's at least an order of magnitude slower than the intprotected object ?synchronized For example, if I just want to increment a value in a intthread-safe way , why not always use: synchronized(thread