private static final long serialVersionUID = 7373984872572414699L;// 提供了所有实现机制private final Sync sync;


ReentrantLock有两个构造方法
public ReentrantLock() {sync = new NonfairSync();}public ReentrantLock(boolean fair) {sync = fair ? new FairSync() : new NonfairSync();}
3.1 Lock
final void lock() {acquire(1);}
final void lock() {// 如果cas操作能够将state置为1,则说明此时锁空闲,如果抢锁成功,则将独占线程设置为自己;如果锁此时不空闲,则调用acquire方法if (compareAndSetState(0, 1))setExclusiveOwnerThread(Thread.currentThread());elseacquire(1);}
protected final boolean tryAcquire(int acquires) {final Thread current = Thread.currentThread();// 获取锁的状态int c = getState();// 为0说明锁空闲if (c == 0) {// 通过hasQueuePredecessors方法判断同步队列是否为空,如果为false则说明为空,则我们再通过cas去获取锁,如果也成功,则将当前线程设置为独占线程,并返回trueif (!hasQueuedPredecessors() &&compareAndSetState(0, acquires)) {setExclusiveOwnerThread(current);return true;}}// 如果c>0则锁已经被使用// 如果此时占用锁的线程就是本线程,说明本线程已经拿到锁(可重入锁)// 最后更新state返回trueelse if (current == getExclusiveOwnerThread()) {int nextc = c + acquires;if (nextc < 0 if (nexextct< 0 <)throw new Error("Maximum lock count exceeded");setState(nextc);return true;}// 拿锁不成功返回falsereturn false;}
3.1.2 NonfairSync实现的tryAcquire
protected final boolean tryAcquire(int acquires) {return nonfairTryAcquire(acquires);}
final boolean nonfairTryAcquire(int acquires) {final Thread current = Thread.currentThread();int c = getState();if (c == 0) {// 与公平锁tryAcquire唯一的不同就是在这没有判断同步队列是否还有等待线程if (compareAndSetState(0, acquires)) {setExclusiveOwnerThread(current);return true;}}else if (current == getExclusiveOwnerThread()) {int nextc = c + acquires;if (nextc < 0) // overflowthrow new Error("Maximum lock count exceeded");setState(nextc);return true;}return false;}
3.2 LockInterruptibly
public void lockInterruptibly() throws InterruptedException {sync.acquireInterruptibly(1);}
public final void acquireInterruptibly(int arg)throws InterruptedException {// 查看标志位判断是否被中断if (Thread.interrupted())throw new InterruptedException();if (!tryAcquire(arg))doAcquireInterruptibly(arg);}
private void doAcquireInterruptibly(int arg)throws InterruptedException {final Node node = addWaiter(Node.EXCLUSIVE);boolean failed = true;try {for (;;) {final Node p = node.predecessor();if (p == head && tryAcquire(arg)) {setHead(node);p.next = null; // help GCfailed = false;return;}if (shouldParkAfterFailedAcquire(p, node) &&parkAndCheckInterrupt())throw new InterruptedException();}} finally {if (failed)cancelAcquire(node);}}
3.3 tryLock
public boolean tryLock() {return sync.nonfairTryAcquire(1);}
public boolean tryLock(long timeout, TimeUnit unit)throws InterruptedException {return sync.tryAcquireNanos(1, unit.toNanos(timeout));}
public final boolean tryAcquireNanos(int arg, long nanosTimeout)throws InterruptedException {if (Thread.interrupted())throw new InterruptedException();return tryAcquire(arg) ||doAcquireNanos(arg, nanosTimeout);}

3.5 unlock
public void unlock() {sync.release(1);}
3.6 newCondition
final ConditionObject newCondition() {return new ConditionObject();}
文章转载自24只羊羊羊,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




