A custom synchronizer extends AQS and overrides tryAcquire/tryRelease but stores its lock count in an extra instance field rather than calling setState/getState. Acquire and release appear to work in single-threaded tests but corrupt under contention. What is the core internals reason?
class BadLock extends AbstractQueuedSynchronizer {
private int count; // NOT the AQS state
protected boolean tryAcquire(int arg) {
if (count == 0) { count = 1; setExclusiveOwnerThread(Thread.currentThread()); return true; }
return false;
}
protected boolean tryRelease(int arg) { count = 0; return true; }
}