PMD-jPinpoint-rules
PMD-jPinpoint-rules copied to clipboard
Rule Request: AvoidNonAtomicIfPutOnConcurrentMap
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
Note it needs support for local var (used in parallelStream), formal param and field.
Not just put:
Utilize an atomic if-combined-with-modify operation provided by the ConcurrentMap: putIfAbsent, computeIfAbsent, computeIfPresent, getOrDefault, remove and replace.