java锁的细化

分段锁:系统提供一定数量的原始锁,根据传入对象的哈希值获取对应的锁并加锁

public class SegmentLock {
private Integer segments = 16;//默认分段数量
private final HashMap lockMap = new HashMap<>();

public SegmentLock() {

init(null, false);

}

public SegmentLock(Integer counts, boolean fair) {

init(counts, fair);

}

private void init(Integer counts, boolean fair) {

if (counts != null) {
  segments = counts;
}
for (int i = 0; i < segments; i++) {
  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();

}
}

哈希锁:上述分段锁的基础上发展起来的第二种锁策略,目的是实现真正意义上的细粒度锁。每个哈希值不同的对象都能获得自己独立的锁。

public class HashLock {
private boolean isFair = false;
private final SegmentLock segmentLock = new SegmentLock<>();//分段锁
private final ConcurrentHashMap lockMap = new ConcurrentHashMap<>();

public HashLock() {
}

public HashLock(boolean fair) {

isFair = fair;

}

public void lock(T key) {

LockInfo lockInfo;
segmentLock.lock(key);
try {
  lockInfo = lockMap.get(key);
  if (lockInfo == null) {
    lockInfo = 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 = lockMap.get(key);
if (lockInfo.count.get() == 1) {
  segmentLock.lock(key);
  try {
    if (lockInfo.count.get() == 1) {
      lockMap.remove(key);
    }
  } finally {
    segmentLock.unlock(key);
  }
}
lockInfo.count.decrementAndGet();
lockInfo.unlock();

}

private static class LockInfo {

public ReentrantLock lock;
public AtomicInteger count = new AtomicInteger(1);

private LockInfo(boolean fair) {
  this.lock = new ReentrantLock(fair);
}

public void lock() {
  this.lock.lock();
}

public void unlock() {
  this.lock.unlock();
}

}
}
弱引用锁:哈希锁因为引入的分段锁来保证锁创建和销毁的同步,总感觉有点瑕疵,所以写了第三个锁来寻求更好的性能和更细粒度的锁。这个锁的思想是借助java的弱引用来创建锁,把锁的销毁交给jvm的垃圾回收,来避免额外的消耗。

public class WeakHashLock {
private ConcurrentHashMap> lockMap = new ConcurrentHashMap<>();
private ReferenceQueue queue = new ReferenceQueue<>();

public ReentrantLock get(T key) {

if (lockMap.size() > 1000) {
  clearEmptyRef();
}
WeakReference lockRef = lockMap.get(key);
ReentrantLock lock = (lockRef == null ? null : lockRef.get());
while (lock == null) {
  lockMap.putIfAbsent(key, new WeakLockRef<>(new ReentrantLock(), queue, key));
  lockRef = lockMap.get(key);
  lock = (lockRef == null ? null : lockRef.get());
  if (lock != null) {
    return lock;
  }
  clearEmptyRef();
}
return lock;

}

@SuppressWarnings("unchecked")
private void clearEmptyRef() {

Reference ref;
while ((ref = queue.poll()) != null) {
  WeakLockRef weakLockRef = (WeakLockRef) ref;
  lockMap.remove(weakLockRef.key);
}

}

private static final class WeakLockRef extends WeakReference {

final T key;

private WeakLockRef(K referent, ReferenceQueue q, T key) {
  super(referent, q);
  this.key = key;
}

}
}

你可能感兴趣的:(java)