Update:
When I first posted this, I was sure the code was broken. Now I am no longer sure of what I was observing. The biggest problem that I am facing is that I cannot apply 17.4. Memory Model and indicate whether it should work or not.
This code is broken.
He is too complex for what he is trying to achieve, but also he is unsafe because I noticed that he can wait forever in c. I am not worried about the former (can be used ReentrantLockeither CountDownLatchfor the fishfinder code), but I wonder what is the reason for the latter?
static final ConcurrentHashMap<Integer, Object> mutex = new ConcurrentHashMap<>();
public static brokenFoo() {
Object ourLock = new Object();
for (;;) {
Object theirLock = mutex.putIfAbsent(0, ourLock);
if (theirLock == null) {
break;
}
synchronized (theirLock) {
if (mutex.get(0) != theirLock) {
continue;
}
theirLock.wait();
}
}
try {
} finally {
synchronized (ourLock) {
mutex.remove(0);
ourLock.notifyAll();
}
}
}
I thought in terms of what happens-befores :
- hb (f, h) hb (h, a), hb (f, a)
- hb (c, d) hb (d, e), hb (c, e)
, , .
: ( , .)
:
brokenFoo() , // critical section.brokenFoo() , // critical section, - .// critical section .
Actual: