????3. juc?е?????????
????juc?е?????????? 1. ????????; 2. ??д??. ??????????????????? AbstractQueuedSynchronizer. ????????
????3.1 AbstractQueuedSynchronizer
?????????????int????????? ???????FIFO???м?UnSafe??CAS????????????????????? AQS????? int ????????????????????????????4?????????? tryAcquire(int)??tryRelease(int)??tryAcquireShared(int)??tryReleaseShared(int)?????????????????/??????????????????????? ????????????????????????????????????????????????????????????????2?? ??????????
????????????2?е???λ?:
??????????????????????????????????????????????????????????о????????????????????????API????????????????????????????????????????????????????????????????????Щ?????????????磺?????????????????м???????????????????????????????????и??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
????3.2 ReentrantLock
?????????????? ??????????????(FairSync/NonFairSync)?? ?????????? ???Sync?????AbstractQueuedSynchronizer.
?????????????????:
????FairSync ?????ж??????????(tryAcquire)????????ж?????? ?ж?????????????б??????????? ??????????????????????????е?????????????
if (c == 0) {
if (!hasQueuedPredecessors() && //??????
compareAndSetState(0?? acquires)) {
setExclusiveOwnerThread(current);
return true;
}
}
public final boolean hasQueuedPredecessors() {
// The correctness of this depends on head being initialized
// before tail and on head.next being accurate if the current
// thread is first in queue.
Node t = tail; // Read fields in reverse initialization order
Node h = head;
Node s;
return h != t &&
((s = h.next) == null || s.thread != Thread.currentThread());
}
????3.3 ReentrantReadWriteLock
????3.3.1 ????
????????????д???? ???????????????????ó????? ????volatile?к????? ???к????????
????volatile??????????? ??1дN????????£? ?????????. ???????NдN???? ??α???????????????????ж???????? ???ReentrantReadWriteLock??.
????3.3.2 ??????:
????????
public static class ReadLock implements Lock?? java.io.Serializable  {
private final Sync sync;
protected ReadLock(ReentrantReadWriteLock lock) {
sync = lock.sync;
}
public void lock() {
sync.acquireShared(1);//??????
}
public void lockInterruptibly() throws InterruptedException {
sync.acquireSharedInterruptibly(1);
}
public  boolean tryLock() {
return sync.tryReadLock();
}
public boolean tryLock(long timeout?? TimeUnit unit) throws InterruptedException {
return sync.tryAcquireSharedNanos(1?? unit.toNanos(timeout));
}
public  void unlock() {
sync.releaseShared(1);
}
public Condition newCondition() {
throw new UnsupportedOperationException();
}
}
????д??
public static class WriteLock implements Lock?? java.io.Serializable  {
private final Sync sync;
protected WriteLock(ReentrantReadWriteLock lock) {
sync = lock.sync;
}
public void lock() {
sync.acquire(1);//?????
}
public void lockInterruptibly() throws InterruptedException {
sync.acquireInterruptibly(1);
}
public boolean tryLock( ) {
return sync.tryWriteLock();
}
public boolean tryLock(long timeout?? TimeUnit unit) throws InterruptedException {
return sync.tryAcquireNanos(1?? unit.toNanos(timeout));
}
public void unlock() {
sync.release(1);
}
public Condition newCondition() {
return sync.newCondition();
}
public boolean isHeldByCurrentThread() {
return sync.isHeldExclusively();
}
public int getHoldCount() {
return sync.getWriteHoldCount();
}
}
????WriteLock???????????????ReentrantLock?????????????????????????AQS??acquire/release??????????????????????????ReentrantLock?????????????????嵥1???嵥2?????????ReadLock??????????????WriteLock?????????????
????AQS???????state??Σ?int?????32λ???????????ж??????????????????????????????????0????1???????????????????????????????????????????????????????????????????ReadWriteLock?????д???????????????μ?????????????????????????????????????д?????????????????????????????state???????????????ReentrantReadWrilteLock???潫????????????????λ16λ???????????????????λ16λ????????????????????????????????2^16-1=65536??????????????????????????????65535??
????3.3.3 д????????:
?????????????????0??c=getState()???0???????д???????w???0???????????????0??????????????????????????????????????????????д?????????????????????????????65535??????Error??
???????????д?????λ0???????????????0?????????1???????c!=0???????????????????????????????????????????д????????????????
??????????????д????????????????true??
protected final boolean tryAcquire(int acquires) {
Thread current = Thread.currentThread();
int c = getState();
int w = exclusiveCount(c);
if (c != 0) {
if (w == 0 || current != getExclusiveOwnerThread())
return false;
if (w + exclusiveCount(acquires) > MAX_COUNT)
throw new Error("Maximum lock count exceeded");
}
if ((w == 0 && writerShouldBlock(current)) ||
!compareAndSetState(c?? c + acquires))
return false;
setExclusiveOwnerThread(current);
return true;
}
????3.3.4 ?г???д??????????:
??????????
??????д???????????д????????????????????????????????д??????????????д?????????????????????????????? д?????д???????????λ?????????????????????????????????д?????? ?????д???????65535?????д??????65535???????????
??????????
????д?????д????????????????????????д????????????д??????????????????????????????????
??????????
??????????????????????д????????????????д?????????????ж??????????????????????????????д????????????????????????????
??????????ж?
???????????д?????????????????ж???????????????
????????????
????д????????????????(Condition)?????????????????£????????????????????????????????????UnsupportedOperationException????