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 unique ID and insert the object. If it is, I go on to check if the ID already exists in the map. If no object with ID exists, insert the object.

When making multiple concurrent POST requests, the time period between checking if the HashMap contains a matching ID and inserting the object proved to be a problem. A request with a generated ID can potentially be written in the line of code of the second request gcdMap.get(obj.getId()) == nullby where the ID of the first request has been specified if the request is executed between . I've been using Thread.Sleep() to reproduce this issue.gcdMap.put(obj.getId(), obj);

public static ConcurrentMap<Long, GCDObject> gcdMap = new ConcurrentHashMap<Long, GCDObject>();
@POST

@Consumes(MediaType.APPLICATION_JSON)
public GCDObject create(GCDObject obj) throws GCDRequestException {
    obj.setTimestamp(LocalDateTime.now());
    obj.setResult(GCD.calculate(obj.getX(), obj.getY()));

    if (obj.getId() != null) { // JSON contains ID
        if (gcdMap.get(obj.getId()) == null) { // If map does not contain obj with ID already,
            Thread.sleep(1000);
            gcdMap.put(obj.getId(), obj); // Put obj into map.
            return obj;
        } else { // else map already contains ID,
            throw new GCDRequestException();
        }
    } else { // JSON contains no ID
        obj.setId(buildId()); // Build ID
        gcdMap.put(obj.getId(), obj); // Put into map
        return obj;
    }
}

I've seen suggestions for using locks, but can't implement them in a way that solves this problem. Any examples, documentation or articles that can help me develop a solution would be greatly appreciated.

Edit: I misspelled it about three times in the comments below. I can't edit them right now, but I noticed!

shmosel:

For conditional insertion:putIfAbsent()

if (gcdMap.putIfAbsent(obj.getId(), obj) == null) { // If map did not contain obj with ID already,
    return obj;
} else { // else map already contained ID,
    throw new GCDRequestException();
}

Related


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 dealing with webhooks

Sean Hudson Our application creates/updates database entries based on webhooks of external services. The webhook sends the external ID of the object so we can get more data to process. Webhook processing with round trips to get more data is 400-1200ms. Sometim

Concurrency issues when dealing with webhooks

Sean Hudson Our application creates/updates database entries based on webhooks of external services. The webhook sends the external ID of the object so we can get more data to process. Webhook processing with round trips to get more data is 400-1200ms. Sometim

Concurrency issues when dealing with webhooks

Sean Hudson Our application creates/updates database entries based on webhooks of external services. The webhook sends the external ID of the object so we can get more data to process. Webhook processing with round trips to get more data is 400-1200ms. Sometim

Concurrency issues when dealing with webhooks

Sean Hudson Our application creates/updates database entries based on webhooks of external services. The webhook sends the external ID of the object so we can get more data to process. Webhook processing with round trips to get more data is 400-1200ms. Sometim

Concurrency issues when dealing with webhooks

Sean Hudson Our application creates/updates database entries based on webhooks of external services. The webhook sends the external ID of the object so we can get more data to process. Webhook processing with round trips to get more data is 400-1200ms. Sometim

Concurrency issues when dealing with webhooks

Sean Hudson Our application creates/updates database entries based on webhooks of external services. The webhook sends the external ID of the object so we can get more data to process. Webhook processing with round trips to get more data is 400-1200ms. Sometim

Concurrency issues when coordinating two threads

Jainesh Kumar I am trying to split the work of the loop into two threads. I am using the ExecutorService to create a second thread and the main thread as another thread. I'm using a counter to stop the loop when it reaches a certain value, but I can't synchron

Concurrency issues when coordinating two threads

Jainesh Kumar I am trying to split the work of the loop into two threads. I am using the ExecutorService to create a second thread and the main thread as another thread. I'm using a counter to stop the loop when it reaches a certain value, but I can't synchron

Using SQLAlchemy sessions with flask and concurrency issues

Kirill I'm developing an API with Flask and SQLAlchemy and here's what I want to do: I have a client application that works on multiple tablets and has to send several requests to add content to the server. But I don't want to use automatic rollback at the end

Netty: Concurrency issues using multiple event loops

Ohas I have a client connecting to n different servers. So, I am creating n different channels. Because I have more than 5000 servers. I used 10 event loops and only one event loop group. Also, each channel has a separate pipe. I already know there won't be an

How to specify type when using ConcurrentHashMap in Clojure

Wu Yanzu In Java I need to know ConcurrentHashMapwhat type the key or value is and then how to do it in clojure. I could create a new type ConcurrentHashMapwithout specifying the key or value, but I think it would perform better if the type could be specified.

How to specify type when using ConcurrentHashMap in Clojure

Wu Yanzu In Java I need to know ConcurrentHashMapwhat type the key or value is and then how to do it in clojure. I could create a new type ConcurrentHashMapwithout specifying the key or value, but I think it would perform better if the type could be specified.

Syntax issues when using []

who cares So, when working on a test project (for learning purposes), I found that I could use a syntax like this: throw new Exception($Query->errorInfo()[2]); Note the use of [2] (array index) after $Query->errorInfo(). This thing also seems to work on other

Syntax issues when using []

who cares So, when working on a test project (for learning purposes), I found that I could use a syntax like this: throw new Exception($Query->errorInfo()[2]); Note the use of [2] (array index) after $Query->errorInfo(). This thing also seems to work on other