Object (@NotNull String className) { Object lock = map.computeIfAbsent(className, it -> new WeakLockReference(it, new Object(), queue)).get(); if (lock != null) { return lock; } Object newLock = new Object(); WeakLockReference newRef = new WeakLockReference(className, newLock, queue); while (true) { removeExpired(); WeakLockReference oldRef = map.putIfAbsent(className, newRef); if (oldRef == null) { return newLock; } Object oldLock = oldRef.get(); if (oldLock != null) { return oldLock; } else if (map.replace(className, oldRef, newRef)) { return newLock; } } }