This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// N.B. Draining the queue is only necessary to ensure that we don't accumulate empty references | |
// in the array. We could skip this if we decide we don't care about holding on to Reference | |
// objects indefinitely. | |
private void drainQueue() { | |
Reference<? extends L> ref; | |
while ((ref = queue.poll()) != null) { | |
// We only ever register ArrayReferences with the queue so this is always safe. | |
ArrayReference<? extends L> arrayRef = (ArrayReference<? extends L>) ref; | |
// Try to clear out the array slot, n.b. if we fail that is fine, in either case the | |
// arrayRef will be out of the array after this step. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override final int indexFor(Object key) { | |
int hash = smear(key.hashCode()); | |
return hash & mask; | |
} | |
/* | |
* This method was written by Doug Lea with assistance from members of JCP | |
* JSR-166 Expert Group and released to the public domain, as explained at | |
* http://creativecommons.org/licenses/publicdomain | |
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Locker { | |
private static final Striped<Lock> sLock = Striped.lazyWeakLock(100); | |
public static Lock getLock(String lockId) { | |
return sLock.get(lockId); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.nipunsaggar.locker; | |
import java.util.*; | |
import java.util.concurrent.ConcurrentHashMap; | |
import java.util.concurrent.locks.ReentrantLock; | |
public class Locker { | |
private static final Map <String, ReentrantLock> sLockStore = new ConcurrentHashMap<>(); | |
private static LockPool sLockPool = new LockPool(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Usage: | |
* Lock lock = Locker.getLock(); | |
* try { | |
* lock.lock(); | |
* //do something | |
* } finally { | |
* lock.unlock(); | |
* } | |
} |