lockMap.put(i, new ReentrantLock(fair));
}
}
public void lock(T key) {
ReentrantLock lock = lockMap.get((key.hashCode()>>>1) % segments);
lock.lock();
}
public void unlock(T key) {
ReentrantLock lock = lockMap.get((key.hashCode()>>>1) % segments);
lock.unlock();
}
}
2. 哈希锁
上述分段锁的基础上发展起来的第二种锁策略,目的是实现真正意义上的细粒度锁。每个哈希值不同的对象都能获得自己独立的锁。在测试中,在被锁住的代码执行速度飞快的情况下,效率比分段锁慢 30% 左右。如果有长耗时操作,感觉表现应该会更好。代码如下:
public class HashLock {
private boolean isFair = false;
private final SegmentLock segmentLock &#61; new SegmentLock<>();//分段锁
private final ConcurrentHashMap lockMap &#61; new ConcurrentHashMap<>();
public HashLock() {
}
public HashLock(boolean fair) {
isFair &#61; fair;
}
public void lock(T key) {
LockInfo lockInfo;
segmentLock.lock(key);
try {
lockInfo &#61; lockMap.get(key);
if (lockInfo &#61;&#61; null) {
lockInfo &#61; new LockInfo(isFair);
lockMap.put(key, lockInfo);
} else {
lockInfo.count.incrementAndGet();
}
} finally {
segmentLock.unlock(key);
}
lockInfo.lock.lock();
}
public void unlock(T key) {
LockInfo lockInfo &#61; lockMap.get(key);
if (lockInfo.count.get() &#61;&#61; 1) {
segmentLock.lock(key);
try {
if (lockInfo.count.get() &#61;&#61; 1) {
lockMap.remove(key);
}
} finally {
segmentLock.unlock(key);
}
}
lockInfo.count.decrementAndGet();
lockInfo.unlock();
}
private static class LockInfo {
public ReentrantLock lock;
public AtomicInteger count &#61; new AtomicInteger(1);
private LockInfo(boolean fair) {
this.lock &#61; new ReentrantLock(fair);
}
public void lock() {
this.lock.lock();
}
public void unlock() {
this.lock.unlock();
}
}
}
3. 弱引用锁
哈希锁因为引入的分段锁来保证锁创建和销毁的同步&#xff0c;总感觉有点瑕疵&#xff0c;所以写了第三个锁来寻求更好的性能和更细粒度的锁。这个锁的思想是借助java的弱引用来创建锁&#xff0c;把锁的销毁交给jvm的垃圾回收&#xff0c;来避免额外的消耗。
有点遗憾的是因为使用了ConcurrentHashMap作为锁的容器&#xff0c;所以没能真正意义上的摆脱分段锁。这个锁的性能比 HashLock 快10% 左右。锁代码&#xff1a;
/**
* 弱引用锁&#xff0c;为每个独立的哈希值提供独立的锁功能
*/
public class WeakHashLock {
private ConcurrentHashMap> lockMap &#61; new ConcurrentHashMap<>();
private ReferenceQueue queue &#61; new ReferenceQueue<>();
public ReentrantLock get(T key) {
if (lockMap.size() > 1000) {
clearEmptyRef();
}
WeakReference lockRef &#61; lockMap.get(key);
ReentrantLock lock &#61; (lockRef &#61;&#61; null ? null : lockRef.get());
while (lock &#61;&#61; null) {
lockMap.putIfAbsent(key, new WeakLockRef<>(new ReentrantLock(), queue, key));
lockRef &#61; lockMap.get(key);
lock &#61; (lockRef &#61;&#61; null ? null : lockRef.get());
if (lock !&#61; null) {
return lock;
}
clearEmptyRef();
}
return lock;
}
&#64;SuppressWarnings("unchecked")
private void clearEmptyRef() {
Reference extends ReentrantLock> ref;
while ((ref &#61; queue.poll()) !&#61; null) {
WeakLockRef weakLockRef &#61; (WeakLockRef) ref;
lockMap.remove(weakLockRef.key);
}
}
private static final class WeakLockRef extends WeakReference {
final T key;
private WeakLockRef(K referent, ReferenceQueue super K> q, T key) {
super(referent, q);
this.key &#61; key;
}
}
}
后记
最开始想借助 locksupport 和 AQS 来实现细粒度锁&#xff0c;写着写着发现正在实现的东西和java 原生的锁区别不大&#xff0c;于是放弃改为对java自带锁的封装&#xff0c;浪费了不少时间。
实际上在实现了这些细粒度锁之后&#xff0c;又有了新的想法&#xff0c;比如可以通过分段思想将数据提交给专门的线程来处理&#xff0c;可以减少大量线程的阻塞时间&#xff0c;留待日后探索...