Lock handler for arbitrary keys
I have code which implements a "lock handler" for arbitrary keys. Given a key , it ensures that only one thread at a time can process that(or equals) key (which here means calling the externalSystem.process(key) call). So far, I have code like this: public class MyHandler { private final SomeWorkExecutor someWorkExecutor; private final ConcurrentHashMap<Key, Lock> lockMap = new ConcurrentHashMap<>(); public void handle(Key key) { // This can lead to OOM as it creates locks without removing them Lock keyLock = lockMap.computeIfAbsent( key, (k) -> new ReentrantLock() ); keyLock.lock(); try {