AQS源碼閱讀筆記(一)

AQS源碼閱讀筆記

先看下這個類張非常重要的一個靜態內部類Node。如下:

  

static final class Node {//表示當前節點以共享模式等待鎖static final Node SHARED = new Node();//表示當前模式以獨占模式等待鎖static final Node EXCLUSIVE = null;//表示當前線程等待鎖的動作被取消了(那么當前節點將會在下一次迭代節點的時候被踢出)static final int CANCELLED =  1;//表示當前節點處于掛起狀態,如果當前節點的前一個節點釋放了鎖,那么當前節點將會被喚醒static final int SIGNAL    = -1;//(此處先不分析,后續在分析COnfition的時候再分析)static final int CONDITION = -2;//此處我們先不分析,后續在分析釋放鎖的時候分析static final int PROPAGATE = -3;    //當前節點的狀態volatile int waitStatus;//指向當前節點前一個節點的引用volatile Node prev;//指向當前節點后一個節點的引用  volatile Node next;//當前節點持有的線程volatile Thread thread;//當前節點以何種方式等待鎖(它的取值,要么是SHARE,要么是EXCLUSIVE)Node nextWaiter;//當前線程是否以共享模式等待鎖final boolean isShared() {return nextWaiter == SHARED;}//查找當前節點的前一個節點final Node predecessor() throws NullPointerException {Node p = prev;if (p == null)throw new NullPointerException();elsereturn p;}Node() {    // Used to establish initial head or SHARED marker}Node(Thread thread, Node mode) {     // Used by addWaiterthis.nextWaiter = mode;this.thread = thread;}Node(Thread thread, int waitStatus) { // Used by Conditionthis.waitStatus = waitStatus;this.thread = thread;}}    

  

  接著,我們再來看看AQS中的字段:

	private transient volatile Node head;private transient volatile Node tail;private volatile int state;

  其中, node和tail分別表示頭結點和尾節點,這兩個字段是用來的保證同步隊列原子入(出)隊操作(具體后續在分析具體的實現類中說)。

  state在此處可以簡單理解為加鎖的次數(每次加鎖,state + 1,每次釋放鎖, state - 1,當state = 0的時候,就表示沒有線程持有鎖 )。

后續結合具體的實現類來分析各種加鎖,解鎖。

?

 

    

轉載于:https://www.cnblogs.com/bedlimate/p/8810893.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/278154.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278154.shtml
英文地址,請注明出處:http://en.pswp.cn/news/278154.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python之實現從ftp下載文件到本地

#!/usr/bin/python # codingutf-8 import os from ftplib import FTP # 引入ftp模塊class MyFtp:ftp FTP()def __init__(self,host,port21):self.ftp.connect(host,port)def login(self,username,pwd):self.ftp.set_debuglevel(2) # 打開調試級別2,顯示詳細信息s…

如何在Instagram上過濾冒犯性評論

Shubham AgarwalShubham AgarwalIf you have a public Instagram profile, chances are you’ve been a victim of inappropriate comments from strangers. Given the social network’s vast size, it’s practically impossible to escape such bad actors. Thankfully, Ins…

復雜性思維中文第二版 附錄 A、算法分析

附錄 A、算法分析 原文:Appendix A Analysis of algorithms 譯者:飛龍 協議:CC BY-NC-SA 4.0 自豪地采用谷歌翻譯 部分參考了《Think Python 2e 中譯本 第二十一章:算法分析》 算法分析 (Analysis of algorithms) 是計算機科學的一…

如何在Windows 8.1中獲取Windows 10樣式的開始菜單

On January 21, Microsoft officially announced the new features that would be included in Windows 10. While you’ll have to wait for the release to enjoy most of the new features, you can take advantage of the new Windows 10 Start menu today. 1月21日&#x…

Android工程中javax annotation Nullable找不到的替代方案

我們在某些Android開源庫中會遇到下面的引用找不到的問題:import javax.annotation.Nonnull;import javax.annotation.Nullable; 其實Android實現了javax的類似注解,可以使用下面的引用替換:import android.support.annotation.NonNull;impor…

HDU1561:The more, The Better——題解

http://acm.hdu.edu.cn/showproblem.php?pid1561 ACboy很喜歡玩一種戰略游戲,在一個地圖上,有N座城堡,每座城堡都有一定的寶物,在每次游戲中ACboy允許攻克M個城堡并獲得里面的寶物。但由于地理位置原因,有些城堡不能直…

ubuntu列出所有磁盤_列出Ubuntu上的磁盤空間使用情況

ubuntu列出所有磁盤Simply open a new Terminal window and type in this command 只需打開一個新的終端窗口并輸入此命令 df -Th f翻譯自: https://www.howtogeek.com/howto/ubuntu/list-disk-space-usage-on-ubuntu/ubuntu列出所有磁盤

python基礎之字符編碼

閱讀目錄 一 了解字符編碼的知識儲備二 字符編碼介紹三 字符編碼應用之文件編輯器3.1 文本編輯器之nodpad3.2 文本編輯器之pycharm3.3 文本編輯器之python解釋器3.4 總結四 字符編碼應用之python4.1 執行python程序的三個階段4.2 python2與python3字符串類型的區別一 了解字符編…

C# WinForm開發系列 - DataGridView

1.DataGridView實現課程表 testcontrol.rar 2.DataGridView二維表頭及單元格合并 DataGridView單元格合并和二維表頭.rar myMultiColHeaderDgv.rar 3.DataGridView單元格顯示GIF圖片 gifanimationindatagrid.rar 4.自定義顯示DataGridView列(行頭顯示行號與圖標,同一單元格顯示…

ruby語法_Ruby函數(方法)語法

ruby語法The Ruby language makes it easy to create functions. Ruby語言使創建函數變得容易。 Function Syntax 功能語法 def functionname(variable) return <value>end def functionname(variable)return <值>結束Examples 例子 Your function can compute …

011-git-將tag推送到遠端

1、將tag推送到遠端 在使用TortoiseGit過程中&#xff0c;push推送過程中&#xff0c;選擇include tag&#xff0c;遠端就有次分支。

pageadmin CMS網站建設教程:站點添加自定義字段

首先看看pagedmin默認的站點設置都有什么&#xff0c;如下圖&#xff1a; 這里只有一些最基本的參數設置&#xff0c;用過3.0版本或用過其他公司開發的cms的用戶應該有這種體驗&#xff0c;在站點設置中可以設置logo圖片&#xff0c;備案號&#xff0c;底部內容等等。 那么為什…

如何將世界時鐘和時區小部件添加到您的iPhone

When you work remotely or have friends and family who live in another country, it’s important to know what time it is across time zones. A world clock (or time zone) widget on your iPhone’s Home screen makes this much easier. 當您遠程工作或有家人和朋友居…

Python網絡爬蟲之三種數據解析方式

引入 回顧requests實現數據爬取的流程 指定url基于requests模塊發起請求獲取響應對象中的數據進行持久化存儲其實&#xff0c;在上述流程中還需要較為重要的一步&#xff0c;就是在持久化存儲之前需要進行指定數據解析。因為大多數情況下的需求&#xff0c;我們都會指定去使用聚…

一行代碼實現底部導航欄TabLayout

歡迎關注公眾號&#xff1a;JueCode app中底部導航欄已經是很常見的控件了&#xff0c;比如微信&#xff0c;簡書&#xff0c;QQ等都有這類控件&#xff0c;都是點擊底部標簽切換界面。主要的實現手段有 RadioGroupFragmentTabLayoutTabLayoutBottom Navigation其中TabLayout一…

小程序視頻截gif_3個簡單的應用程序,可讓您深入視頻和GIF

小程序視頻截gifDeepfakes make it possible to manipulate videos and GIFs. The technology has become so easy to use, you can now create deepfakes right on your phone. That’s right—you can now easily insert yourself into a meme. 借助Deepfake &#xff0c;可以…

【AtCoder】ARC095 E - Symmetric Grid 模擬

【題目】E - Symmetric Grid 【題意】給定n*m的小寫字母矩陣&#xff0c;求是否能通過若干行互換和列互換使得矩陣中心對稱。n,m<12。 【算法】模擬 【題解】首先行列操作獨立&#xff0c;如果已確定行操作&#xff0c;那么兩個在對稱位置的列要滿足條件必須其中一列反轉后和…

一、內存尋址

1.內存地址分類: 邏輯地址、線性地址、物理地址 邏輯地址:段選擇符偏移量 線性地址:C語言中取地址符&打印出來的地址就是這個地址&#xff0c;也叫虛擬地址。 物理地址:內存總線尋址的具體地址&#xff0c;是真實存在的。 邏輯地址通過分段單元轉換成線性地址&#xff0c;線…

如何使用Google TV設置Chromecast

Justin Duino賈斯汀杜伊諾(Justin Duino)Google changed up its streaming platform with the release of the Chromecast with Google TV. Instead of being a Cast-only device like Chromecasts before it, Google’s latest dongle runs the successor of Android TV. If y…

js之 foreach, map, every, some

js中array有四個方法 foreach, map, every, some&#xff0c;其使用各有傾向。 關注點一&#xff1a;foreach 和 map 無法跳出循環&#xff0c;每個元素均執行foreach 和 map 無法跳出循環&#xff0c;他們是對每個數組元素調用 callback&#xff1b; foreach 無返回值&#xf…