日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA开发需求分析套路_JAVA并发工具常用设计套路示例代码

發(fā)布時間:2023/12/10 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA开发需求分析套路_JAVA并发工具常用设计套路示例代码 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

了解JAVA并發(fā)工具常用設(shè)計套路

前言

在學(xué)習(xí)JAVA并發(fā)工具時,分析JUC下的源碼,發(fā)現(xiàn)有三個利器:狀態(tài)、隊列、CAS。

狀態(tài)

一般是state屬性,如AQS源碼中的狀態(tài),是整個工具的核心,一般操作的執(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;

隊列

隊列一般由鏈表實現(xiàn)(單向鏈表,雙向鏈表),在線程獲取不到想要的資源或者狀態(tài)時,將線程封裝成特定節(jié)點(diǎn),扔到等待隊列中,等待時機(jī)成熟,再從隊列中取出,是悲觀鎖思想。

如AQS中的Node節(jié)點(diǎn),代碼太長就不貼了。

CAS

CAS操作是樂觀鎖思想,是輕量級的并發(fā)處理。一般用于對上述狀態(tài)的修改,而且能保證有且只有一個線程能修改這個狀態(tài)。

一般由Unsafe類中的compareAndSwap之類的方法實現(xiàn)。使用CAS,往往伴隨自旋,如果修改狀態(tài)失敗,則不斷地重試,直到修改狀態(tài)成功。

以上就是java并發(fā)編程中的套路,抓住這個思路,想必能在學(xué)習(xí)中有所幫助。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持碼農(nóng)之家。

以上就是本次給大家分享的關(guān)于java的全部知識點(diǎn)內(nèi)容總結(jié),大家還可以在下方相關(guān)文章里找到相關(guān)文章進(jìn)一步學(xué)習(xí),感謝大家的閱讀和支持。

總結(jié)

以上是生活随笔為你收集整理的JAVA开发需求分析套路_JAVA并发工具常用设计套路示例代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。