1 为了保证原子性,对相同 key 进行修改时,可能造成线程阻塞。
(详细参见:MyBatis 3.5.X 在 JDK8 中的性能问题)
The entire method invocation is performed atomically, so the function is applied at most once per key. Some attempted update operations on this map by other threads may be blocked while computation is in progress, so the computation should be short and simple, and must not attempt to update any other mappings of this map.
[JDK-8161372] ConcurrentHashMap.computeIfAbsent(k,f) locks bin when k present
官方在 JDK9 中修复了这个问题。
影响案例:MyBatis DefaultReflectorFactory.java 线程阻塞
public Reflector findForClass(Class<?> type) {
if (classCacheEnabled) {
// synchronized (type) removed see issue #461
return reflectorMap.computeIfAbsent(type, Reflector::new);
} else {
return new Reflector(type);
}
}