public KeyLocker<K> getKeyLocker(K key) {return new KeyLocker<K>(sexyLocker, key); }
public static>private UUID uuid = UUID.randomUUID();private SexyLocker sexyLocker;private K key;
public KeyLocker(SexyLocker sexyLocker, K key) {this.sexyLocker = sexyLocker;this.key = key; }
public boolean lockWithRetry(int expireTime, int retryTimes) { Random random
= new Random();for (int i = 0; i < retryTimes; i++) {if (this.lock()) {return true; }
else {try { Thread.sleep(random.nextInt(
50)); }
catch (InterruptedException e) {
private final ConcurrentMap<K, Semaphore> map = new ConcurrentHashMap<K, Semaphore>();private final ThreadLocal<Map<K, LockInfo>> local = new ThreadLocal<Map<K, LockInfo>>() { @Override
protected Map<K, LockInfo> initialValue() {return new HashMap<K, LockInfo>(); }
};
public void lock(K key) {if (key == null)return; LockInfo info
= local.get().get(key);if (info == null) { Semaphore current
= new Semaphore(1); current.acquireUninterruptibly();
Semaphore previous
= map.put(key, current);if (previous != null) previous.acquireUninterruptibly();
local.get().put(key,
new LockInfo(current)); }
else { info.lockCount
++; }
}