I wrote a simple class that uses AbstractQueuedSynchronizer. I wrote a class that represents a “Gate”, that can be passed if open, or is blocking if closed. Here is the code:
public class GateBlocking {
final class Sync extends AbstractQueuedSynchronizer {
public Sync() {
setState(0);
}
@Override
protected int tryAcquireShared(int ignored) {
return getState() == 1 ? 1 : -1;
}
public void reset(int newState) {
setState(newState);
}
};
private Sync sync = new Sync();
public void open() {
sync.reset(1);
}
public void close() {
sync.reset(0);
}
public void pass() throws InterruptedException {
sync.acquireShared(1);
}
};
Unfortunately, if a thread blocks on pass method because gate is closed and some other thread opens the gate in meantime, the blocked one doesn’t get interrupted – It blocks infinitely.
Here is a test that shows it:
public class GateBlockingTest {
@Test
public void parallelPassClosedAndOpenGate() throws Exception{
final GateBlocking g = new GateBlocking();
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
g.open();
} catch (InterruptedException e) {
}
}
});
t.start();
g.pass();
}
}
Please help, what should I change to make the gate passing thread acquire the lock successfully.
It looks like
setState()only changes the state, but doesn’t notify blocked threads about the change.Therefore you should use acquire/release methods instead:
So, overall workflow of
AbstractQueuedSynchronizerlooks like follows:Clients call
publicacquire/release methodsThese methods arrange all synchronization functionality and delegate actual locking policy to
protected try*()methodsYou define your locking policy in
protected try*()methods usinggetState()/setState()/compareAndSetState()