内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

AQS學(xué)習(xí)-創(chuàng)新互聯(lián)

創(chuàng)新互聯(lián)公司服務(wù)緊隨時(shí)代發(fā)展步伐,進(jìn)行技術(shù)革新和技術(shù)進(jìn)步,經(jīng)過十余年的發(fā)展和積累,已經(jīng)匯集了一批資深網(wǎng)站策劃師、設(shè)計(jì)師、專業(yè)的網(wǎng)站實(shí)施團(tuán)隊(duì)以及高素質(zhì)售后服務(wù)人員,并且完全形成了一套成熟的業(yè)務(wù)流程,能夠完全依照客戶要求對網(wǎng)站進(jìn)行成都做網(wǎng)站、成都網(wǎng)站建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、建設(shè)、維護(hù)、更新和改版,實(shí)現(xiàn)客戶網(wǎng)站對外宣傳展示的首要目的,并為客戶企業(yè)品牌互聯(lián)網(wǎng)化提供全面的解決方案。

1.1 AQS 簡單介紹

AQS 的全稱為(AbstractQueuedSynchronizer),這個(gè)類在 java.util.concurrent.locks 包下面。

AQS 是一個(gè)用來構(gòu)建鎖和同步器的框架,使用 AQS 能簡單且高效地構(gòu)造出應(yīng)用廣泛的大量的同步器, 比如我們提到的 ReentrantLock,Semaphore,其他的諸如 ReentrantReadWriteLock,SynchronousQueue,F(xiàn)utureTask(jdk1.7) 等等皆是基于 AQS 的。當(dāng)然,我們自己也能利用 AQS 非常輕松容易地構(gòu)造出符合我們自己需求的同步器。

1.2 AQS 原理

1.2.1 AQS 原理概覽

AQS 核心思想是,如果被請求的共享資源空閑,則將當(dāng)前請求資源的線程設(shè)置為有效的工作線程,并且將共享資源設(shè)置為鎖定狀態(tài)。如果被請求的共享資源被占用,那么就需要一套線程阻塞等待以及被喚醒 時(shí)鎖分配的機(jī)制,這個(gè)機(jī)制 AQS 是用?CLH 隊(duì)列鎖實(shí)現(xiàn)的,即將暫時(shí)獲取不到鎖的線程加入到隊(duì)列中。

關(guān)于AQS類有一段注釋是這樣寫的:

Wait queue node class.
The wait queue is a variant of a "CLH" (Craig, Landin, and Hagersten) lock queue. CLH locks are normally used for spinlocks. We instead use them for blocking synchronizers, but use the same basic tactic of holding some of the control information about a thread in the predecessor of its node. A "status" field in each node keeps track of whether a thread should block. A node is signalled when its predecessor releases. Each node of the queue otherwise serves as a specific-notification-style monitor holding a single waiting thread. The status field does NOT control whether threads are granted locks etc though. A thread may try to acquire if it is first in the queue. But being first does not guarantee success; it only gives the right to contend. So the currently released contender thread may need to rewait.
To enqueue into a CLH lock, you atomically splice it in as new tail. To dequeue, you just set the head field.

大致的意思是這樣的:

等待隊(duì)列節(jié)點(diǎn)類。

等待隊(duì)列是“CLH”(Craig、Landin和Hagersten)鎖定隊(duì)列的變體。CLH鎖通常用于自旋鎖。相反,我們使用它們來阻止同步器,但使用相同的基本策略,即在其節(jié)點(diǎn)的前一個(gè)線程中保存一些有關(guān)線程的控制信息。每個(gè)節(jié)點(diǎn)中的“狀態(tài)”字段跟蹤線程是否應(yīng)該阻塞。節(jié)點(diǎn)在其前任釋放時(shí)發(fā)出信號。否則,隊(duì)列的每個(gè)節(jié)點(diǎn)都充當(dāng)一個(gè)特定的通知樣式監(jiān)視器,保存一個(gè)等待線程。狀態(tài)字段不控制線程是否被授予鎖等。如果線程是隊(duì)列中的第一個(gè),則線程可能會嘗試獲取。但第一并不能保證成功;它只賦予了競爭的權(quán)利。因此,當(dāng)前發(fā)布的競爭者線程可能需要重新等待。

要排隊(duì)進(jìn)入CLH鎖,您可以將其作為新的尾部自動拼接。要退出隊(duì)列,只需設(shè)置head字段。

?+------+? ?prev? ?+-----+? ? ? ??+-----+
head |? ? ? ? ?|?<----? ? ?|? ? ? ? |<---- |? ? ? ?| ?tail
?+------+? ? ? ? ? ? ?+-----+? ? ? ? +-----+
?

Insertion into a CLH queue requires only a single atomic operation on "tail", so there is a simple atomic point of demarcation from unqueued to queued. Similarly, dequeuing involves only updating the "head". However, it takes a bit more work for nodes to determine who their successors are, in part to deal with possible cancellation due to timeouts and interrupts.
The "prev" links (not used in original CLH locks), are mainly needed to handle cancellation. If a node is cancelled, its successor is (normally) relinked to a non-cancelled predecessor. For explanation of similar mechanics in the case of spin locks, see the papers by Scott and Scherer at http://www.cs.rochester.edu/u/scott/synchronization/
We also use "next" links to implement blocking mechanics. The thread id for each node is kept in its own node, so a predecessor signals the next node to wake up by traversing next link to determine which thread it is. Determination of successor must avoid races with newly queued nodes to set the "next" fields of their predecessors. This is solved when necessary by checking backwards from the atomically updated "tail" when a node's successor appears to be null. (Or, said differently, the next-links are an optimization so that we don't usually need a backward scan.)

翻譯:

插入CLH隊(duì)列只需要對“尾部”執(zhí)行一個(gè)原子操作,所以從未排隊(duì)到排隊(duì)有一個(gè)簡單的原子分界點(diǎn)。同樣,退出隊(duì)列只涉及更新“頭部”。然而,節(jié)點(diǎn)需要更多的工作來確定他們的繼任者是誰,部分原因是要處理由于超時(shí)和中斷而可能導(dǎo)致的取消。

“prev”鏈接(未在原始CLH鎖中使用)主要用于處理取消。如果某個(gè)節(jié)點(diǎn)被取消,則其后續(xù)節(jié)點(diǎn)(通常)將重新鏈接到未取消的前一個(gè)節(jié)點(diǎn)。有關(guān)自旋鎖的類似力學(xué)解釋,請參閱Scott和Scherer在http://www.cs.rochester.edu/u/scott/synchronization/

我們還使用“下一步”鏈接來實(shí)現(xiàn)阻塞機(jī)制。每個(gè)節(jié)點(diǎn)的線程id都保存在自己的節(jié)點(diǎn)中,因此前一個(gè)節(jié)點(diǎn)通過遍歷下一個(gè)鏈接來通知下一個(gè)節(jié)點(diǎn)喚醒,以確定它是哪個(gè)線程。確定后一個(gè)節(jié)點(diǎn)必須避免與新排隊(duì)的節(jié)點(diǎn)競爭,以設(shè)置前一個(gè)的“下一個(gè)”字段。當(dāng)一個(gè)節(jié)點(diǎn)的后繼節(jié)點(diǎn)看起來為空時(shí),這可以在必要時(shí)通過從原子更新的“尾部”向后檢查來解決。(或者,換言之,下一個(gè)鏈接是一個(gè)優(yōu)化,因此我們通常不需要反向掃描。)

Cancellation introduces some conservatism to the basic algorithms. Since we must poll for cancellation of other nodes, we can miss noticing whether a cancelled node is ahead or behind us. This is dealt with by always unparking successors upon cancellation, allowing them to stabilize on a new predecessor, unless we can identify an uncancelled predecessor who will carry this responsibility.
CLH queues need a dummy header node to get started. But we don't create them on construction, because it would be wasted effort if there is never contention. Instead, the node is constructed and head and tail pointers are set upon first contention.
Threads waiting on Conditions use the same nodes, but use an additional link. Conditions only need to link nodes in simple (non-concurrent) linked queues because they are only accessed when exclusively held. Upon await, a node is inserted into a condition queue. Upon signal, the node is transferred to the main queue. A special value of status field is used to mark which queue a node is on.
Thanks go to Dave Dice, Mark Moir, Victor Luchangco, Bill Scherer and Michael Scott, along with members of JSR-166 expert group, for helpful ideas, discussions, and critiques on the design of this class.

翻譯:

抵消給基本算法引入了一些保守性。由于我們必須輪詢其他節(jié)點(diǎn)的取消,因此我們可能無法注意到被取消的節(jié)點(diǎn)是在我們之前還是在我們之后。這是通過在取消時(shí)始終取消后續(xù)節(jié)點(diǎn)的標(biāo)記來解決的,允許他們穩(wěn)定在一個(gè)新的前任節(jié)點(diǎn)上,除非我們能夠確定一個(gè)未被取消的前任節(jié)點(diǎn)將承擔(dān)此責(zé)任。

CLH隊(duì)列需要一個(gè)虛擬頭節(jié)點(diǎn)才能啟動。但我們不會在施工中創(chuàng)建它們,因?yàn)槿绻淮嬖跔幾h,這將浪費(fèi)精力。相反,在第一次爭用時(shí)構(gòu)造節(jié)點(diǎn)并設(shè)置頭指針和尾指針。

等待條件的線程使用相同的節(jié)點(diǎn),但使用額外的鏈接。條件只需要鏈接簡單(非并發(fā))鏈接隊(duì)列中的節(jié)點(diǎn),因?yàn)樗鼈冎辉讵?dú)占時(shí)被訪問。等待時(shí),節(jié)點(diǎn)被插入到條件隊(duì)列中。發(fā)出信號后,節(jié)點(diǎn)被轉(zhuǎn)移到主隊(duì)列。狀態(tài)字段的特殊值用于標(biāo)記節(jié)點(diǎn)所在的隊(duì)列。

感謝Dave Dice、Mark Moir、Victor Luchangco、Bill Scherer和Michael Scott以及JSR-166專家組成員對本課程設(shè)計(jì)的有益想法、討論和評論。

看個(gè) AQS (AbstractQueuedSynchronizer)原理圖借用知乎的一張圖:?

AQS 使用一個(gè) int 成員變量來表示同步狀態(tài),通過內(nèi)置的 FIFO 隊(duì)列來完成獲取資源線程的排隊(duì)工作。

AQS 使用 CAS 對該同步狀態(tài)進(jìn)行原子操作實(shí)現(xiàn)對其值的修改。

狀態(tài)信息通過 protected 類型的getState , setState , compareAndSetState 進(jìn)行操作

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧

新聞標(biāo)題:AQS學(xué)習(xí)-創(chuàng)新互聯(lián)
瀏覽地址:http://www.rwnh.cn/article2/djppoc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號網(wǎng)站維護(hù)、網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)、Google、網(wǎng)頁設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司
淳化县| 互助| 都昌县| 平阴县| 合水县| 宁乡县| 鹿邑县| 岢岚县| 九龙城区| 贡嘎县| 黄陵县| 六安市| 盐山县| 衡山县| 淮安市| 富裕县| 囊谦县| 昆山市| 施秉县| 盖州市| 高安市| 台中市| 隆化县| 图木舒克市| 奉化市| 河南省| 遂宁市| 砚山县| 凤翔县| 凤阳县| 阿鲁科尔沁旗| 图木舒克市| 屯门区| 宜都市| 平度市| 定南县| 景东| 萨嘎县| 历史| 龙山县| 西吉县|