Member-only story
Some Bugs in ConcurrentHashMap You Should Know
Yes, you read that right. In JDK-8, there are some bugs in ConcurrentHashMap, that may cause your trouble.

ConcurrentHashMap is one of the most frequently used collection classes in our daily work, Its characteristic is high performance and thread-safety. However, there are two bugs that impact the performance of this familiar map. The problem is in computeIfAbsent
.
computeIfAbsent
blocks when multiple threads obtain the same key.concurrentHashMap
maybe stuck in an endless loop incomputeIfAbsent
.
The Performance Bug
We all know that the computeIfAbsent
can combine the following two operations into one and ensure its thread safety.
- Determine whether a key is null.
- If not, the execute logic is like the put method.
To do this is use synchronized, but if you repeat invoke computeIfAbsent
method with the same key, it always gets blocked. in other words, computeIfAbsent
blocks when multiple threads obtain the same key and it will impact performance(https://bugs.opnejdk.java.net/browse/JDK-8161372). Fortunately, this issue has been fixed in JDK-9. If you still use JDK-8, we find a workaround.