Object (@NotNull String className) { WeakLockReference lockReference = myMap.get(className); if (lockReference != null) { Object lock = lockReference.get(); if (lock != null) { return lock; } } Object newLock = new Object(); WeakLockReference newRef = new WeakLockReference(className, newLock, myQueue); while (true) { processQueue(); WeakLockReference oldRef = myMap.putIfAbsent(className, newRef); if (oldRef == null) { return newLock; } Object oldLock = oldRef.get(); if (oldLock != null) { return oldLock; } else if (myMap.replace(className, oldRef, newRef)) { return newLock; } } }