PMD-jPinpoint-rules icon indicating copy to clipboard operation
PMD-jPinpoint-rules copied to clipboard

Rule Request: AvoidNonAtomicIfPutOnConcurrentMap

Open jborgers opened this issue 1 year ago • 1 comments

Check-Modify constructs are the most subtle and most occurring concurrency bugs. When using a ConcurrentMap, it is a multi-threading environment, so separate check and modify is a concurrency bug.

Example:

void foo(ConcurrentMap<String, Account> accountMap, String accKey) {
     if(!accountMap.containsKey(accKey)){
            accountMap.put(accKey, account);
      }
}

accountMap.putIfAbsent(accKey, account) should be used

jborgers avatar Feb 11 '25 14:02 jborgers

Note it needs support for local var (used in parallelStream), formal param and field.

jborgers avatar Feb 11 '25 15:02 jborgers

Not just put: Utilize an atomic if-combined-with-modify operation provided by the ConcurrentMap: putIfAbsent, computeIfAbsent, computeIfPresent, getOrDefault, remove and replace.

jborgers avatar Nov 12 '25 16:11 jborgers