DeadLock In ConcurrentHashMap

concurrenthashmap, java

Solution

`"AaAa"` and `"BBBB"` have the same `hashCode()` - 2031744. Therefore both keys are mapped to the same bin of the Map.

The outer `map.computeIfAbsent` locks that bin, and the inner `map.computeIfAbsent` tries to lock it before the lock is released - hence the deadlock.

Problem

Following code when run, never terminates and stuck in endless loop. I am not sure where it is getting stuck. Interesting thing is when I change `AaAa` to `AAAA` every thing works fine as expected. ``` public class Test { public static void main(String[] args) { Map<String, Integer> map = new ConcurrentHashMap<>(16); map.computeIfAbsent( "AaAa", key -> { return map.computeIfAbsent( "BBBB", key2 -> 42); } ); } } ``` Can some one help me understanding this behavior.

Original source

Related problems