ReentrantLock可重入锁 ,内部类Sync继承AQS,Sync有两个子类,分别为NonfairSync和FairSync 。ReentrantLock锁为排它锁 ,使用它可以有效解决死锁问题。分析类关系图,ReentrantLock实现了Lock和Serializable接口,Syn做为ReentrantLock的一个内部类,我们从Sync开始看起
abstract static class Sync extends AbstractQueuedSynchronizer {
private static final long serialVersionUID = - 5179523762034025860 L;
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;
}
}
static final class NonfairSync extends Sync {
private static final long serialVersionUID = 7316153563782823691 L;
final void lock ( ) {
if ( compareAndSetState ( 0 , 1 ) )
setExclusiveOwnerThread ( Thread. currentThread ( ) ) ;
else
acquire ( 1 ) ;
}
protected final boolean tryAcquire ( int acquires) {
return nonfairTryAcquire ( acquires) ;
}
}
public final void acquire ( int arg) {
if ( ! tryAcquire ( arg) &&
acquireQueued ( addWaiter ( Node. EXCLUSIVE) , arg) )
selfInterrupt ( ) ;
}
final boolean acquireQueued ( final Node node, int arg) {
boolean failed = true ;
try {
boolean interrupted = false ;
for ( ; ; ) {
final Node p = node. predecessor ( ) ;
if ( p == head && tryAcquire ( arg) ) {
setHead ( node) ;
p. next = null;
failed = false ;
return interrupted;
}
if ( shouldParkAfterFailedAcquire ( p, node) &&
parkAndCheckInterrupt ( ) )
interrupted = true ;
}
} finally {
if ( failed)
cancelAcquire ( node) ;
}
}
private static boolean shouldParkAfterFailedAcquire ( Node pred, Node node) {
int ws = pred. waitStatus;
if ( ws == Node. SIGNAL)
return true ;
if ( ws > 0 ) {
do {
node. prev = pred = pred. prev;
} while ( pred. waitStatus > 0 ) ;
pred. next = node;
} else {
compareAndSetWaitStatus ( pred, ws, Node. SIGNAL) ;
}
return false ;
}
private final boolean parkAndCheckInterrupt ( ) {
LockSupport. park ( this ) ;
return Thread. interrupted ( ) ;
}
public static void park ( Object blocker) {
Thread t = Thread. currentThread ( ) ;
setBlocker ( t, blocker) ;
UNSAFE. park ( false , 0 L) ;
setBlocker ( t, null) ;
}
static final class FairSync extends Sync {
private static final long serialVersionUID = - 3000897897090466540 L;
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 ;
}
}