一、介绍
一个可重入互斥锁,其基本行为和语义与使用同步方法和语句访问的隐式监视锁相同,但具有扩展功能。
ReentrantLock由上次成功锁定但尚未解锁的线程拥有。当锁不属于另一个线程时,调用锁的线程返回成功则获得锁。如果当前线程已经拥有锁,该方法将立即返回。
公平锁:
在争用状态下,锁倾向于将访问权授予等待时间最长的线程。使用公平锁被多个线程访问的程序可能显示较低的总体吞吐量
非公平锁:
此锁不能保证任何特定的访问顺序。
锁的公平性并不能保证线程调度的公平性。因此,使用公平锁的多个线程中的一个可以连续多次获得它,而其他活动线程没有进展,当前也没有持有该锁。
1 2 3 4 5 6 7 8 9 10 11 12 13
| class X { private final ReentrantLock lock = new ReentrantLock(); public void m() { lock.lock(); try { } finally { lock.unlock() } } }
|
二、源码分析
此锁的同步控制基础。下面分为公平版本和不公平版本。使用aqs状态表示锁上的持有次数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| abstract static class Sync extends AbstractQueuedSynchronizer { private static final long serialVersionUID = -5179523762034025860L;
abstract void lock();
final boolean nonfairTryAcquire(int acquires) { final Thread current = Thread.currentThread(); int c = getState(); if (c == 0) { if (compareAndSetState(0, acquires)) { setExclusiveOwnerThread(current); return true; } } else if (current == getExclusiveOwnerThread()) { int nextc = c + acquires; if (nextc < 0) throw new Error("Maximum lock count exceeded"); setState(nextc); return true; } return false; } protected final boolean tryRelease(int releases) { int c = getState() - releases; if (Thread.currentThread() != getExclusiveOwnerThread()) throw new IllegalMonitorStateException(); boolean free = false; if (c == 0) { free = true; setExclusiveOwnerThread(null); } setState(c); return free; }
protected final boolean isHeldExclusively() { return getExclusiveOwnerThread() == Thread.currentThread(); }
final ConditionObject newCondition() { return new ConditionObject(); }
final Thread getOwner() { return getState() == 0 ? null : getExclusiveOwnerThread(); }
final int getHoldCount() { return isHeldExclusively() ? getState() : 0; }
final boolean isLocked() { return getState() != 0; } }
|
2.1 非公平锁
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| static final class NonfairSync extends Sync { private static final long serialVersionUID = 7316153563782823691L;
final void lock() { if (compareAndSetState(0, 1)) setExclusiveOwnerThread(Thread.currentThread()); else acquire(1); }
protected final boolean tryAcquire(int acquires) { return nonfairTryAcquire(acquires); } }
|
2.2 公平锁
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| static final class FairSync extends Sync { private static final long serialVersionUID = -3000897897090466540L;
final void lock() { acquire(1); }
protected final boolean tryAcquire(int acquires) { final Thread current = Thread.currentThread(); int c = getState(); if (c == 0) { if (!hasQueuedPredecessors() && compareAndSetState(0, acquires)) { setExclusiveOwnerThread(current); return true; } } else if (current == getExclusiveOwnerThread()) { int nextc = c + acquires; if (nextc < 0) throw new Error("Maximum lock count exceeded"); setState(nextc); return true; } return false; } }
public final boolean hasQueuedPredecessors() { Node t = tail; Node h = head; Node s; return h != t && ((s = h.next) == null || s.thread != Thread.currentThread()); }
|