I want to be able to release the lock of an atomic execution like compute() to wait for a condition, how can I do that?
(Edited) Is there something that is like wait() (wait for a condition) just for the current key within compute() of ConcurrentHashMap (or getAnd*() functions of AtomicReference), but will actually release the lock in Java 8? I'm also fine with using a totally different API.
I know I can do what I want if I have a separate list of objects/locks for each key, and use a plain old synchronized block, but I am looking for a less clunky way.
Pseudocode to illustrate:
public class Test {
ConcurrentHashMap<String, Integer> map;
public void illustration(String key) {
map.computeIfPresent(key, (k, v) -> {
Integer new_v = v;
if (!/* Condition on v */) {
// Pretend this will release the lock held by compute()
k.wait(timeout);
new_v = map.get(k);
}
if (/* Same condition on new_v */) {
return /* Result of operation on new_v */;
} else
throw new RuntimeException();
});
}
}