Java keyed locks and registry lifetime
EntityLocker is a small public Java experiment. Its timeout test checks what a caller can do while a key is held, and a separate invariant governs when the key’s lock can be retired.
By Denis Stepulenok · Published · Revised
What the registry coordinates
A caller supplies an entity key and an action. ConcurrentHashMap.computeIfAbsent looks up or creates that key’s lock in one EntityLocker instance. A thread-local check rejects nested acquisition of a smaller key. This ordering rule only covers acquisitions made through the utility.
Run the timed-acquisition test
The test uses a latch to wait until one thread holds key 1. A second attempt on that key has a 50 ms timeout while the holder pauses for 500 ms. The assertions check that the second action never runs and the holder finishes. With Java and Maven installed, run this single test from the pinned revision.
git clone https://github.com/stden/EntityLocker.git
cd EntityLocker
git checkout d3de8482cd2f7d24c688217ccc51c4eabde5373b
mvn -Dtest=EntityLockerTest#testTimeouts test
What a passing test leaves open
The registry must keep a mapping alive while a caller has reserved its lock, even if that caller is still waiting. The reviewed code attempts removal during cleanup and uses a counter that the blocking lock() path increments. The inherited timed tryLock path does not increment that counter. The timeout test covers an acquisition outcome and gives no proof about registry retirement.
For a registry that reclaims entries, I would make reservation and retirement part of one per-key lifecycle. Every acquisition path would reserve an entry before waiting and release that reservation on timeout, interruption or completion. Removal would require both zero reservations and a match to the exact entry still in the registry. I would test overlapping acquisition and retirement with a deterministic schedule before relying on the reclamation policy.