Java并发包中锁的原理(ReentrantLock、ReentrantReadWriteLock)
ReentrantLock fairLock = new ReentrantLock ( ) ;
ReentrantLock fairLock = new ReentrantLock ( true ) ;
ReentrantLock nonFairLock = new ReentrantLock ( false ) ;
ReentrantReadWriteLock fairReentrantReadWriteLock = new ReentrantReadWriteLock ( ) ;
ReentrantReadWriteLock fairReentrantReadWriteLock = new ReentrantReadWriteLock ( true ) ;
ReentrantReadWriteLock. ReadLock readLock = fairReentrantReadWriteLock. readLock ( ) ;
ReentrantReadWriteLock. WriteLock writeLock = fairReentrantReadWriteLock. writeLock ( ) ;
ReentrantReadWriteLock nonFairReentrantReadWriteLock = new ReentrantReadWriteLock ( false ) ;
ReentrantReadWriteLock. ReadLock readLock = nonFairReentrantReadWriteLock. readLock ( ) ;
ReentrantReadWriteLock. WriteLock writeLock = nonFairReentrantReadWriteLock. writeLock ( ) ;
StampedLock stampedLock = new StampedLock ( ) ;
long writeStamp = stampedLock. writeLock ( ) ;
long readStamp = stampedLock. readLock ( ) ;
long stamp = stampedLock. tryOptimisticRead ( ) ;
ReentrantLock公平锁
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 ;
}
}
ReentrantLock非公平锁
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) ;
}
}
abstract class AbstractQueuedSynchronizer - hasQueuedPredecessors实现
public final boolean hasQueuedPredecessors ( ) {
Node t = tail;
Node h = head;
Node s;
return h != t &&
( ( s = h. next) == null || s. thread != Thread. currentThread ( ) ) ;
}
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;
}
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 ;
}
private void readObject ( java. io. ObjectInputStream s)
throws java. io. IOException, ClassNotFoundException {
s. defaultReadObject ( ) ;
setState ( 0 ) ;
}
}
CopyOnWriteArrayList内部由ReentrantLock加锁实现线程安全
final transient ReentrantLock lock = new ReentrantLock ( ) ;
public boolean add ( E e) {
final ReentrantLock lock = this . lock;
lock. lock ( ) ;
try {
Object[ ] elements = getArray ( ) ;
int len = elements. length;
Object[ ] newElements = Arrays. copyOf ( elements, len + 1 ) ;
newElements[ len] = e;
setArray ( newElements) ;
return true ;
} finally {
lock. unlock ( ) ;
}
}
public E set ( int index, E element) {
final ReentrantLock lock = this . lock;
lock. lock ( ) ;
try {
Object[ ] elements = getArray ( ) ;
E oldValue = get ( elements, index) ;
if ( oldValue != element) {
int len = elements. length;
Object[ ] newElements = Arrays. copyOf ( elements, len) ;
newElements[ index] = element;
setArray ( newElements) ;
} else {
setArray ( elements) ;
}
return oldValue;
} finally {
lock. unlock ( ) ;
}
}