了解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)存可見。
/** * 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ī)成熟,再從隊(duì)列中取出,是悲觀鎖思想。
如AQS中的Node節(jié)點(diǎn),代碼太長就不貼了。
CAS
CAS操作是樂觀鎖思想,是輕量級的并發(fā)處理。一般用于對上述狀態(tài)的修改,而且能保證有且只有一個(gè)線程能修改這個(gè)狀態(tài)。
一般由Unsafe類中的compareAndSwap之類的方法實(shí)現(xiàn)。使用CAS,往往伴隨自旋,如果修改狀態(tài)失敗,則不斷地重試,直到修改狀態(tài)成功。
以上就是java并發(fā)編程中的套路,抓住這個(gè)思路,想必能在學(xué)習(xí)中有所幫助。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java使用Jasypt進(jìn)行加密和解密的技術(shù)指南
Jasypt (Java Simplified Encryption) 是一個(gè)簡化 Java 應(yīng)用中加密工作的庫,它支持加密和解密操作,易于與 Spring Boot 集成,通過 Jasypt,可以安全地管理敏感信息,比如數(shù)據(jù)庫密碼、API 密鑰等,本文介紹了Java使用Jasypt進(jìn)行加密和解密的技術(shù)指南,需要的朋友可以參考下2025-03-03
SpringMVC中MultipartFile上傳獲取圖片的寬度和高度詳解
本篇文章主要介紹了SpringMVC中MultipartFile上傳獲取圖片的寬度和高度,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
SpringBoot?+?Disruptor實(shí)現(xiàn)特快高并發(fā)處理及使用Disruptor高速實(shí)現(xiàn)隊(duì)列的過程
Disruptor是一個(gè)開源的Java框架,它被設(shè)計(jì)用于在生產(chǎn)者—消費(fèi)者(producer-consumer problem,簡稱PCP)問題上獲得盡量高的吞吐量(TPS)和盡量低的延遲,這篇文章主要介紹了SpringBoot?+?Disruptor?實(shí)現(xiàn)特快高并發(fā)處理,使用Disruptor高速實(shí)現(xiàn)隊(duì)列,需要的朋友可以參考下2023-11-11
詳解Java List的擴(kuò)容機(jī)制原理及應(yīng)用
在Java中,List是一種非常常用的數(shù)據(jù)結(jié)構(gòu),用于存儲(chǔ)有序的元素集合,本文將分析Java List的擴(kuò)容機(jī)制原理,并通過示例代碼和測試代碼來加強(qiáng)闡述內(nèi)容,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08
SpringBoot 動(dòng)態(tài)配置郵箱發(fā)件人過程解析
這篇文章主要介紹了SpringBoot 動(dòng)態(tài)配置郵箱發(fā)件人過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
servlet基礎(chǔ)知識_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了servlet基礎(chǔ)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Java Spring分別實(shí)現(xiàn)定時(shí)任務(wù)方法
這篇文章主要為大家詳細(xì)介紹了Java與Spring設(shè)置動(dòng)態(tài)定時(shí)任務(wù)的方法,定時(shí)任務(wù)的應(yīng)用場景十分廣泛,如定時(shí)清理文件、定時(shí)生成報(bào)表、定時(shí)數(shù)據(jù)同步備份等2022-07-07

