了解JAVA并發(fā)工具常用設(shè)計(jì)套路
前言
在學(xué)習(xí)JAVA并發(fā)工具時(shí),分析JUC下的源碼,發(fā)現(xiàn)有三個(gè)利器:狀態(tài)、隊(duì)列、CAS。
狀態(tài)
一般是state屬性,如AQS源碼中的狀態(tài),是整個(gè)工具的核心,一般操作的執(zhí)行都要看當(dāng)前狀態(tài)是什么,
由于狀態(tài)是多線程共享的,所以都是volatile修飾,保證線程直接內(nèi)存可見(jiàn)。
/** * AbstractQueuedSynchronizer中的狀態(tài) */ private volatile int state; /** * Status field, taking on only the values: * SIGNAL: The successor of this node is (or will soon be) * blocked (via park), so the current node must * unpark its successor when it releases or * cancels. To avoid races, acquire methods must * first indicate they need a signal, * then retry the atomic acquire, and then, * on failure, block. * CANCELLED: This node is cancelled due to timeout or interrupt. * Nodes never leave this state. In particular, * a thread with cancelled node never again blocks. * CONDITION: This node is currently on a condition queue. * It will not be used as a sync queue node * until transferred, at which time the status * will be set to 0. (Use of this value here has * nothing to do with the other uses of the * field, but simplifies mechanics.) * PROPAGATE: A releaseShared should be propagated to other * nodes. This is set (for head node only) in * doReleaseShared to ensure propagation * continues, even if other operations have * since intervened. * 0: None of the above * * The values are arranged numerically to simplify use. * Non-negative values mean that a node doesn't need to * signal. So, most code doesn't need to check for particular * values, just for sign. * * The field is initialized to 0 for normal sync nodes, and * CONDITION for condition nodes. It is modified using CAS * (or when possible, unconditional volatile writes). */ volatile int waitStatus;
隊(duì)列
隊(duì)列一般由鏈表實(shí)現(xiàn)(單向鏈表,雙向鏈表),在線程獲取不到想要的資源或者狀態(tài)時(shí),將線程封裝成特定節(jié)點(diǎn),扔到等待隊(duì)列中,等待時(shí)機(jī)成熟,再?gòu)年?duì)列中取出,是悲觀鎖思想。
如AQS中的Node節(jié)點(diǎn),代碼太長(zhǎng)就不貼了。
CAS
CAS操作是樂(lè)觀鎖思想,是輕量級(jí)的并發(fā)處理。一般用于對(duì)上述狀態(tài)的修改,而且能保證有且只有一個(gè)線程能修改這個(gè)狀態(tài)。
一般由Unsafe類中的compareAndSwap之類的方法實(shí)現(xiàn)。使用CAS,往往伴隨自旋,如果修改狀態(tài)失敗,則不斷地重試,直到修改狀態(tài)成功。
以上就是java并發(fā)編程中的套路,抓住這個(gè)思路,想必能在學(xué)習(xí)中有所幫助。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java設(shè)計(jì)模式之責(zé)任鏈模式的概念、實(shí)現(xiàn)以及netty中的責(zé)任鏈模式
這篇文章主要給大家介紹了關(guān)于設(shè)計(jì)模式之責(zé)任鏈模式的概念、實(shí)現(xiàn)以及netty中的責(zé)任鏈模式的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12Java 使用json-lib處理JSON詳解及實(shí)例代碼
這篇文章主要介紹了Java 使用json-lib處理JSON詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02使用maven-archetype-plugin現(xiàn)有項(xiàng)目生成腳手架的方法
這篇文章主要介紹了使用maven-archetype-plugin現(xiàn)有項(xiàng)目生成腳手架的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11SpringBoot實(shí)現(xiàn)日志鏈路追蹤的項(xiàng)目實(shí)踐
在分布式系統(tǒng)中,由于請(qǐng)求的處理過(guò)程可能會(huì)跨越多個(gè)服務(wù),因此,對(duì)請(qǐng)求的追蹤變得尤為重要,本文主要介紹了SpringBoot實(shí)現(xiàn)日志鏈路追蹤的項(xiàng)目實(shí)踐,感興趣的可以了解一下2024-03-03SpringCloud_Eureka服務(wù)注冊(cè)與發(fā)現(xiàn)基礎(chǔ)及構(gòu)建步驟
Eureka服務(wù)注冊(cè)中心,主要用于提供服務(wù)注冊(cè)功能,當(dāng)微服務(wù)啟動(dòng)時(shí),會(huì)將自己的服務(wù)注冊(cè)到Eureka Server,這篇文章主要介紹了SpringCloud中Eureka的配置及詳細(xì)使用,需要的朋友可以參考下2023-01-01Java 字符串轉(zhuǎn)float運(yùn)算 float轉(zhuǎn)字符串的方法
今天小編就為大家分享一篇Java 字符串轉(zhuǎn)float運(yùn)算 float轉(zhuǎn)字符串的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07