跨段死锁
https://github.com/spring-projects/spring-framework/issues/35944
@Test
void deadlock() throws Exception {
var map = new org.springframework.util.ConcurrentReferenceHashMap<String, String>();
var deadLockSync = new CountDownLatch(2);
ExecutorService executor = Executors.newFixedThreadPool(2);
// Thread 1: key1 -> key2
executor.submit(() -> {
try {
map.computeIfAbsent("key1", k -> {
deadLockSync.countDown();
return map.computeIfAbsent("key2", k2 -> "value2");
});
} catch (Exception e) {
throw new RuntimeException(e);
}
});
// Thread 2: key2 -> key1
executor.submit(() -> {
try {
map.computeIfAbsent("key2", k -> {
deadLockSync.countDown();
return map.computeIfAbsent("key1", k2 -> "value1");
});
} catch (Exception e) {
throw new RuntimeException(e);
}
});
executor.shutdown();
boolean completed = executor.awaitTermination(5, TimeUnit.SECONDS);
if (!completed) fail("AB-BA Deadlock detected");
}