Is it possible for ConcurrentHashMap to “deadlock”?

雨燕双飞 提交于 2019-11-28 06:22:46

Maybe not the answer you want, but this may be a JVM bug. See

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6865591

I don't think this is what's happening in your case, but it is possible to write a deadlock with a single instance of ConcurrentHashMap, and it only needs one thread! Kept me stuck for quite a while.

Let's say you're using a ConcurrentHashMap<String, Integer> to calculate a histogram. You might do something like this:

int count = map.compute(key, (k, oldValue) -> {
    return oldValue == null ? 1 : oldValue + 1;
});

Which works just fine.

But let's say you decide instead to write it like this:

int count = map.compute(key, (k, oldValue) -> {
    return map.putIfAbsent(k, 0) + 1;
});

You will now get a 1-thread deadlock with a stack like this:

Thread [main] (Suspended)   
    owns: ConcurrentHashMap$ReservationNode<K,V>  (id=25)   
    ConcurrentHashMap<K,V>.putVal(K, V, boolean) line: not available    
    ConcurrentHashMap<K,V>.putIfAbsent(K, V) line: not available    
    ConcurrentHashMapDeadlock.lambda$0(ConcurrentHashMap, String, Integer) line: 32 
    1613255205.apply(Object, Object) line: not available    
    ConcurrentHashMap<K,V>.compute(K, BiFunction<? super K,? super V,? extends V>) line: not available  

In the example above, it's easy to see that we're attempting to modify the map inside of an atomic modification, which seems like a bad idea. However, if there are a hundred stack frames of event-callbacks between the call to map.compute and map.putIfAbsent, then it can be quite difficult to track down.

Package Unsafe is native, an implementation depends on a platform.

Abrupt termination of third thread (on platform level, excepion is not a problem) which acquired a lock on map can cause such situation - state of lock is broken, two other threads are disabled and waiting for someone to call Unsafe.unpark() (And that will never happen).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!