日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

Java多線程之ReentrantReadWriteLock源碼解析

瀏覽:201日期:2022-08-13 11:18:10
一、介紹1.1 ReentrantReadWriteLock

ReentrantReadWriteLock 是一個讀寫鎖,允許多個讀或者一個寫線程在執行。

內部的 Sync 繼承自 AQS,這個 Sync 包含一個共享讀鎖 ReadLock 和一個獨占寫鎖 WriteLock。

該鎖可以設置公平和非公平,默認非公平。

一個持有寫鎖的線程可以獲取讀鎖。如果該線程先持有寫鎖,再持有讀鎖并釋放寫鎖,稱為鎖降級。

WriteLock支持Condition并且與ReentrantLock語義一致,而ReadLock則不能使用Condition,否則拋出UnsupportedOperationException異常。

public class ReentrantReadWriteLock implements ReadWriteLock { /** 讀鎖 */ private final ReentrantReadWriteLock.ReadLock readerLock; /** 寫鎖 */ private final ReentrantReadWriteLock.WriteLock writerLock; /** 持有的AQS子類對象 */ final Sync sync; abstract static class Sync extends AbstractQueuedSynchronizer {} static final class NonfairSync extends Sync {} static final class FairSync extends Sync {} public static class ReadLock implements Lock {} public static class WriteLock implements Lock {} //默認非公平 public ReentrantReadWriteLock() {this(false); } public ReentrantReadWriteLock(boolean fair) {sync = fair ? new FairSync() : new NonfairSync();readerLock = new ReadLock(this);writerLock = new WriteLock(this); } public static class ReadLock implements Lock { private final Sync sync;protected ReadLock(ReentrantReadWriteLock lock) { sync = lock.sync;} } public static class WriteLock implements Lock { private final Sync sync;protected WriteLock(ReentrantReadWriteLock lock) { sync = lock.sync;} }}1.2 state

Sync 繼承了 AQS,其中有一個 int 的成員變量 state,int 共32位,這里將其視為兩部分,高16位表示讀的數量,低16位表示寫的數量,這里的數量表示線程重入后的總數量。

abstract static class Sync extends AbstractQueuedSynchronizer { //繼承的一個int的成員變量,將其拆分為高16位和低16位 //private volatile int state; static final int SHARED_SHIFT = 16; //讀一次,鎖增加的值 static final int SHARED_UNIT = (1 << SHARED_SHIFT); static final int MAX_COUNT = (1 << SHARED_SHIFT) - 1; static final int EXCLUSIVE_MASK = (1 << SHARED_SHIFT) - 1; //讀的數量 static int sharedCount(int c) { return c >>> SHARED_SHIFT; } //寫的數量 static int exclusiveCount(int c) { return c & EXCLUSIVE_MASK; }}1.3 HoldCounter

讀鎖使用了一個 ThreadLocal<HoldCounter> 讓每個線程有一個線程私有的 HoldCounter,HoldCounter包含一個線程 id 以及讀重入的次數。

查找對應線程的HoldCounter 其實只用一個 ThreadLocalHoldCounter 也足夠了。這里為了加快查詢,用了兩個額外的緩存,即 cachedHoldCounter、firstReader 和 firstReaderHoldCount(后兩個組合起來相當于一個 HoldCounter)。

在讀鎖的相關操作中,先檢查 firstReader 是否為當前線程,否則檢查 cachedHoldCounter 內部的線程是否為當前線程,如果失敗最后會通過 readHolds 來獲取當前線程的 HoldCounter。

static final class HoldCounter { int count = 0; // 使用線程id,而不是線程的引用。這樣可以防止垃圾不被回收 final long tid = getThreadId(Thread.currentThread());}static final class ThreadLocalHoldCounter extends ThreadLocal<HoldCounter> { public HoldCounter initialValue() {return new HoldCounter(); }}//使用的ThreadLocalprivate transient ThreadLocalHoldCounter readHolds;//一個緩存private transient HoldCounter cachedHoldCounter;//組合起來相當于一個緩存private transient Thread firstReader = null;private transient int firstReaderHoldCount;二、讀鎖2.1 讀鎖的獲取

下面講解 tryAcquireShared 和 tryReadLock,tryReadLock 是一種直接搶占的非公平獲取,和 tryAcquireShared 中的非公平獲取有所不同。

2.1.1 tryAcquireShared

根據注釋

1.檢查是否存在其他線程持有的寫鎖,是的話失敗,返回 -1;

2.判斷在當前公平狀態下能否讀,以及是否超過讀的最大數量,滿足條件則嘗試 CAS 修改狀態,讓 state 加一個單位的讀 SHARED_UNIT;修改成功后會根據三種情況,即首次讀、firstReader 是當前線程,以及其他情況分別進行處理,成功,返回1;

3.前面未返回結果,會執行 fullTryAcquireShared。

可以將該方法視為 fullTryAcquireShared 的一次快速嘗試,如果嘗試失敗,會在 fullTryAcquireShared 的自旋中一直執行,直到返回成功或者失敗。

//ReadLockpublic void lock() { sync.acquireShared(1);} //AQSpublic final void acquireShared(int arg) { if (tryAcquireShared(arg) < 0)doAcquireShared(arg);} //Syncprotected final int tryAcquireShared(int unused) { /* * Walkthrough: * 1. If write lock held by another thread, fail. * 2. Otherwise, this thread is eligible for * lock wrt state, so ask if it should block * because of queue policy. If not, try * to grant by CASing state and updating count. * Note that step does not check for reentrant * acquires, which is postponed to full version * to avoid having to check hold count in * the more typical non-reentrant case. * 3. If step 2 fails either because thread * apparently not eligible or CAS fails or count * saturated, chain to version with full retry loop. */ Thread current = Thread.currentThread(); int c = getState(); // 如果寫的數量不是0,且寫線程不是當前線程,失敗 if (exclusiveCount(c) != 0 &&getExclusiveOwnerThread() != current)return -1; // 獲取讀的個數 int r = sharedCount(c); // 如果當前線程想要讀,沒有被堵塞 // 當前讀的數量未超過最大允許的讀的個數 // CAS執行成功 if (!readerShouldBlock() &&r < MAX_COUNT &&compareAndSetState(c, c + SHARED_UNIT)) { // 第一次讀,修改firstReader和firstReaderHoldCount if (r == 0) { firstReader = current; firstReaderHoldCount = 1; // 如果當前線程正好是firstReader} else if (firstReader == current) { firstReaderHoldCount++; // 其他情況,經過一系列處理后,使得rh為當前線程的HoldCounter // 對rh的記數加一} else { HoldCounter rh = cachedHoldCounter; // 如果cached為null或者不是當前線程 if (rh == null || rh.tid != getThreadId(current)) // 從readHolds中get,并修改cachedcachedHoldCounter = rh = readHolds.get(); // 如果cached不是null,但記數為null // 這種情況表示當前線程的HoldCounter已經被刪除,即為null, // 但cached仍然保留著null之前的那個HoldCounter, // 為了方便,直接將cached設置給ThreadLocal即可 else if (rh.count == 0)readHolds.set(rh); //執行到這里,rh表示當前線程的HoldCounter,記數加1 rh.count++;}return 1; } // 前面未返回結果,執行第三步 return fullTryAcquireShared(current);}2.1.2 fullTryAcquireShared

在上述的簡單嘗試 tryAcquireShared 未能確定結果后,執行第三步 fullTryAcquireShared 自旋來不斷嘗試獲取讀鎖,直到成功獲取鎖返回1,或者滿足相應條件認定失敗返回-1。

1.其他線程持有寫鎖,失敗

2.當前線程讀的嘗試滿足堵塞條件表示當前線程排在其他線程后面,且當前線程沒有持有鎖即非重入的情況,失敗

3.其他情況則不斷自旋CAS,達到最大讀的數量會拋出異常,其他情況在成功后返回1。

final int fullTryAcquireShared(Thread current) { /* * This code is in part redundant with that in * tryAcquireShared but is simpler overall by not * complicating tryAcquireShared with interactions between * retries and lazily reading hold counts. */ HoldCounter rh = null; for (;;) {int c = getState();if (exclusiveCount(c) != 0) { // 存在其他線程持有寫鎖,返回-1 if (getExclusiveOwnerThread() != current)return -1; // else we hold the exclusive lock; blocking here // would cause deadlock. //沒有寫鎖,且該線程排在其他線程后面,應該被堵塞 //如果已經持有讀鎖,此次獲取是重入,可以執行else if 之后的操作; //否則,會被堵塞,返回-1。} else if (readerShouldBlock()) { // Make sure we’re not acquiring read lock reentrantly //檢查firstReader if (firstReader == current) {// assert firstReaderHoldCount > 0; } else {if (rh == null) { rh = cachedHoldCounter; if (rh == null || rh.tid != getThreadId(current)) { //執行到下一步rh是cached或者readHolds.get(),檢查rhrh = readHolds.get(); //在get時,如果不存在,會產生一個新的HoldCounter //記數為0表示不是重入鎖,會刪除讓其重新為nullif (rh.count == 0) readHolds.remove(); }} //返回失敗if (rh.count == 0) return -1; }} //達到最大值,不允許繼續增加if (sharedCount(c) == MAX_COUNT) throw new Error('Maximum lock count exceeded'); //和2.1.1中相似if (compareAndSetState(c, c + SHARED_UNIT)) { if (sharedCount(c) == 0) {firstReader = current;firstReaderHoldCount = 1; } else if (firstReader == current) {firstReaderHoldCount++; } else {if (rh == null) rh = cachedHoldCounter;if (rh == null || rh.tid != getThreadId(current)) rh = readHolds.get();else if (rh.count == 0) readHolds.set(rh);rh.count++;cachedHoldCounter = rh; // cache for release } return 1;} }}2.1.3 readerShouldBlock

該方法返回當前線程請求獲得讀鎖是否應該被堵塞,在公平鎖和非公平鎖中的實現不同

在公平鎖中,返回在排隊的隊列中當前線程之前是否存在其他線程,是的話返回 true,當前線程在隊列頭部或者隊列為空返回 false。

// FairSyncfinal boolean readerShouldBlock() { return hasQueuedPredecessors();}// AQSpublic 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());}

在非公平鎖中,隊列中存在兩個節點,且第二個節點是獨占的寫節點,會返回 true,使得新來的讀線程堵塞。

這種方式只能在第二個節點是請求寫鎖的情況下返回 true,避免寫鎖的無限等待;如果寫鎖的請求節點在隊列的其他位置,返回 false,不影響新來的讀線程獲取讀鎖。

如果不按照這種方式處理,而按照隊列中的順序進行處理,則只要存在其他線程在讀,每次來一個新的線程請求讀鎖,總是成功,寫鎖會一直等待下去。

// NonfairSyncfinal boolean readerShouldBlock() { /* As a heuristic to avoid indefinite writer starvation, * block if the thread that momentarily appears to be head * of queue, if one exists, is a waiting writer. This is * only a probabilistic effect since a new reader will not * block if there is a waiting writer behind other enabled * readers that have not yet drained from the queue. */ return apparentlyFirstQueuedIsExclusive();}// AQSfinal boolean apparentlyFirstQueuedIsExclusive() { Node h, s; return (h = head) != null &&(s = h.next) != null &&!s.isShared() &&s.thread != null;}2.1.4 tryReadLock

和 fullTryAcquireShared 有相似之處,該方法總是直接去搶占鎖,直到其他線程獲取寫鎖返回失敗,或者當前當前線程獲取讀鎖返回成功。

//ReadLockpublic boolean tryLock() { return sync.tryReadLock();}//Syncfinal boolean tryReadLock() { Thread current = Thread.currentThread(); for (;;) {int c = getState();if (exclusiveCount(c) != 0 && getExclusiveOwnerThread() != current) return false;int r = sharedCount(c);if (r == MAX_COUNT) throw new Error('Maximum lock count exceeded');if (compareAndSetState(c, c + SHARED_UNIT)) { if (r == 0) {firstReader = current;firstReaderHoldCount = 1; } else if (firstReader == current) {firstReaderHoldCount++; } else {HoldCounter rh = cachedHoldCounter;if (rh == null || rh.tid != getThreadId(current)) cachedHoldCounter = rh = readHolds.get();else if (rh.count == 0) readHolds.set(rh);rh.count++; } return true;} }}2.2 讀鎖的釋放

tryReleaseShared 在 if/else 中實現了通過 first/cached/readHolds 獲取相應的 HoldCounter,并修改其中的記數,記數為0則刪除;在 for 中,不斷自旋實現 CAS 修改狀態 c,如果修改后的狀態為0,表示讀寫鎖全部釋放,返回 true,否則是 false。

// ReadLockpublic void unlock() { sync.releaseShared(1);}// AQSpublic final boolean releaseShared(int arg) { if (tryReleaseShared(arg)) {doReleaseShared();return true; } return false;}// Syncprotected final boolean tryReleaseShared(int unused) { Thread current = Thread.currentThread(); // 先檢查 firstReader是否是當前線程 if (firstReader == current) {// assert firstReaderHoldCount > 0;if (firstReaderHoldCount == 1) firstReader = null;else firstReaderHoldCount--; //否則,處理 cached/readHolds中的HoldCounter } else {HoldCounter rh = cachedHoldCounter;if (rh == null || rh.tid != getThreadId(current)) rh = readHolds.get();int count = rh.count;if (count <= 1) { readHolds.remove(); if (count <= 0)throw unmatchedUnlockException();}--rh.count; } //自旋修改 state for (;;) {int c = getState();int nextc = c - SHARED_UNIT;if (compareAndSetState(c, nextc)) // Releasing the read lock has no effect on readers, // but it may allow waiting writers to proceed if // both read and write locks are now free. //只有讀寫鎖均釋放干凈,才返回true return nextc == 0; }}三、寫鎖3.1 寫鎖的獲取

下面講解 tryAcquire 和 tryWriteLock,tryWriteLock 是一種非公平的獲取。

3.1.1 tryAcquire

根據注釋,tryAcquire 分為三步

1.如果讀記數非0,或者寫記數非0且寫線程不是當前線程,失敗

2.寫鎖的獲取應該被堵塞或者CAS失敗,失敗

3.其他情況,寫重入和新來的寫線程,均成功

//WriteLockpublic void lock() { sync.acquire(1);}//AQSpublic final void acquire(int arg) { if (!tryAcquire(arg) &&acquireQueued(addWaiter(Node.EXCLUSIVE), arg))selfInterrupt();}//Syncprotected final boolean tryAcquire(int acquires) { /* * Walkthrough: * 1. If read count nonzero or write count nonzero * and owner is a different thread, fail. * 2. If count would saturate, fail. (This can only * happen if count is already nonzero.) * 3. Otherwise, this thread is eligible for lock if * it is either a reentrant acquire or * queue policy allows it. If so, update state * and set owner. */ Thread current = Thread.currentThread(); int c = getState(); int w = exclusiveCount(c); //c分為兩部分,寫和讀 if (c != 0) {// (Note: if c != 0 and w == 0 then shared count != 0) // c非0,w是0,則讀記數非0 || 獨占的寫線程不是當前線程 // 返回 falseif (w == 0 || current != getExclusiveOwnerThread()) return false;if (w + exclusiveCount(acquires) > MAX_COUNT) throw new Error('Maximum lock count exceeded');// Reentrant acquire // 重入的情況setState(c + acquires);return true; } // 寫應該被堵塞或者CAS失敗,返回false if (writerShouldBlock() ||!compareAndSetState(c, c + acquires))return false; // 非重入,在CAS成功后,設定獨占寫線程為當前線程,返回true setExclusiveOwnerThread(current); return true;}3.1.2 writerShouldBlock

在公平鎖中,檢查隊列前面是否有其他線程在排隊,在非公平鎖中,總是返回false,即總是不堵塞。

//FairSyncfinal boolean writerShouldBlock() { return hasQueuedPredecessors();}//NonfairSyncfinal boolean writerShouldBlock() { return false; // writers can always barge}3.1.3 tryWriteLock

和 tryAcquire 在非公平鎖的寫法基本一樣。

final boolean tryWriteLock() { Thread current = Thread.currentThread(); int c = getState(); if (c != 0) {int w = exclusiveCount(c);if (w == 0 || current != getExclusiveOwnerThread()) return false;if (w == MAX_COUNT) throw new Error('Maximum lock count exceeded'); } if (!compareAndSetState(c, c + 1))return false; setExclusiveOwnerThread(current); return true;}3.2 寫鎖的釋放

在 tryRelease 中,修改相應的狀態,如果修改后寫鎖記數為0,則返回 true。

//WriteLockpublic void unlock() { sync.release(1);}//AQSpublic final boolean release(int arg) { if (tryRelease(arg)) {Node h = head;if (h != null && h.waitStatus != 0) unparkSuccessor(h);return true; } return false;}//Syncprotected final boolean tryRelease(int releases) { // 首先檢查當前線程是否持有寫鎖 if (!isHeldExclusively())throw new IllegalMonitorStateException(); int nextc = getState() - releases; // 根據修改后的寫記數來確定free boolean free = exclusiveCount(nextc) == 0; // 此時,寫鎖完全釋放,設定寫獨占線程為null if (free)setExclusiveOwnerThread(null); setState(nextc); // 返回 free return free;}四、鎖降級

如果一個線程已經持有寫鎖,再去獲取讀鎖并釋放寫鎖,這個過程稱為鎖降級。

持有寫鎖的時候去獲取讀鎖,只有該持有寫鎖的線程能夠成功獲取讀鎖,然后再釋放寫鎖,保證此時當前線程是有讀鎖的;如果有寫鎖,先釋放寫鎖,再獲取讀鎖,可能暫時不能獲取讀鎖,會在隊列中排隊等待。

到此這篇關于Java基礎之ReentrantReadWriteLock源碼解析的文章就介紹到這了,更多相關Java ReentrantReadWriteLock源碼解析內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Java
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
亚洲永久字幕| 中文一区一区三区免费在线观 | 国产伦精品一区二区三区在线播放| 在线观看免费一区二区| 亚洲成人精品| 1024精品一区二区三区| 99综合视频| 蜜桃伊人久久| 天堂av一区| 欧美日韩中出| 麻豆精品av| 四虎成人av| 成人影视亚洲图片在线| 精品国产免费人成网站| 国产精品chinese| 尤物精品在线| 国产美女久久| 日韩欧美精品一区| 久久99视频| 蜜桃成人精品| 国产综合精品| 亚洲少妇自拍| 最新国产精品| 欧美日韩一区二区国产 | 国产一区成人| 丝袜亚洲精品中文字幕一区| 亚洲三级av| 国产极品一区| 欧美男人天堂| 亚洲欧美日韩视频二区| 视频一区日韩精品| 国产伦理久久久久久妇女| 福利欧美精品在线| 久久精品高清| 日韩高清在线观看一区二区| 福利片在线一区二区| 欧美日韩国产精品一区二区亚洲| 日韩国产91| 欧美天堂亚洲电影院在线观看| 久久亚洲视频| 午夜久久av| av高清一区| 欧美在线观看天堂一区二区三区| 国产一区三区在线播放| 亚洲欧洲一区二区天堂久久| 国产私拍福利精品视频二区| 欧美美女一区| 久久影院资源站| 亚洲经典在线| 黄在线观看免费网站ktv| 亚洲麻豆一区| 精品在线99| 国产不卡av一区二区| 91久久精品无嫩草影院| 在线视频日韩| 蜜桃精品视频| 国产精品午夜av| 一区二区精品| 国产一级久久| 国产主播一区| 成人亚洲精品| 麻豆久久久久久久| 国产麻豆一区二区三区精品视频| 亚州av乱码久久精品蜜桃| 久久一区精品| 久久激五月天综合精品| 亚洲欧美日韩视频二区| 激情久久五月| 精品日韩视频| 麻豆网站免费在线观看| 丝瓜av网站精品一区二区 | 精品国产精品久久一区免费式| 国产欧美日韩一区二区三区四区 | 亚洲精品进入| 欧美69视频| 久久中文字幕二区| 日韩欧美一区二区三区在线视频| 精品视频一二| 91麻豆国产自产在线观看亚洲| 日韩一区二区三区精品| 亚洲人妖在线| 久久国际精品| 麻豆精品在线播放| 国产在线视频欧美一区| 欧美aⅴ一区二区三区视频| 国产精品红桃| 国产中文在线播放| 精品视频一区二区三区四区五区| 国内精品麻豆美女在线播放视频| 成人高清一区| 激情自拍一区| 蜜臀av亚洲一区中文字幕| 亚洲精品福利| 久久精品国产亚洲一区二区三区| 国产亚洲精品美女久久 | 欧美肉体xxxx裸体137大胆| 日韩精品网站| 久久亚洲图片| 老司机精品在线| 欧美日韩一二| 日韩国产一二三区| 日韩在线免费| 亚洲精品三级| 在线看片福利| 日本综合精品一区| 日韩中文首页| 日本不卡视频在线观看| 欧美aa在线视频| 欧美综合另类| 国产精品一区二区三区av| 在线视频观看日韩| 五月激情久久| 亚洲中午字幕| 欧美日韩在线观看首页| 麻豆中文一区二区| 久久精品一区| 国产伦乱精品| 日韩国产在线不卡视频| 女同性一区二区三区人了人一| 久久影视三级福利片| 97久久超碰| 欧美日韩 国产精品| 欧美日韩精品免费观看视频完整| 国产欧美一区二区三区精品酒店| 久久不见久久见中文字幕免费| 日本成人在线网站| 久久国产精品久久久久久电车 | 国产综合亚洲精品一区二| 福利在线免费视频| 精品国产一区二区三区噜噜噜| 国产乱码精品一区二区亚洲| 日韩av一二三| 国产精品免费精品自在线观看| 久久精品 人人爱| 午夜精品久久久久久久久久蜜桃| 中文字幕av一区二区三区人| 久久av在线| 水蜜桃久久夜色精品一区的特点| 亚洲天堂av资源在线观看| 日韩精品免费一区二区夜夜嗨| 久久亚洲不卡| 日韩欧美美女在线观看| 亚洲美女久久| 国产精品三级| 亚洲二区视频| 欧美日本精品| 亚洲天堂久久| 亚洲精品韩国| 夜鲁夜鲁夜鲁视频在线播放| 日韩激情视频网站| 激情欧美丁香| 福利精品在线| 免费看一区二区三区| 亚洲伊人精品酒店| 视频精品一区二区| 999国产精品视频| 六月丁香综合在线视频| 91亚洲精品视频在线观看 | 欧美久久精品一级c片| 红杏一区二区三区| 亚洲夜间福利| 国产欧美一区二区色老头| 在线精品视频在线观看高清| 黄色在线观看www| 日韩国产专区| 四虎影视精品| 91欧美精品| av中文资源在线资源免费观看| 香蕉久久99| 国产精品一区二区三区av| 久久精品电影| 亚洲字幕久久| 日韩欧美一区二区三区免费观看| 欧美另类综合| 精品一区二区三区的国产在线观看| 久久精品123| 91成人小视频| 欧美特黄一级大片| 免费日韩av片| 日韩电影免费网址| 亚洲影视一区二区三区| 久久不见久久见国语| 日韩午夜av| 欧美亚洲综合视频| 欧美影院三区| 久久久久久色 | 国产精品日本一区二区三区在线 | 日韩欧美精品| 婷婷综合电影| 国产综合欧美| 国产精品免费精品自在线观看| 精品一级视频| 亚洲区欧美区| 日本不卡一二三区黄网| 日本视频中文字幕一区二区三区| 日本综合视频| 欧美激情在线精品一区二区三区| 久久精品99国产精品日本| 天堂久久一区| 久久国产精品免费精品3p|