深入理解讀寫鎖 ReadWriteLock原始碼分析

2021-08-20 17:13:15 字數 3988 閱讀 3389

public

inte***ce

readwritelock

class cacheddata 

// downgrade by acquiring read lock

before releasing write

lock

rwl.readlock().lock();

} finally

}try finally

}}

class rwdictionary 

finally

}public string allkeys()

finally

}public data put(string key, data value)

finally

}public

void

clear()

finally

}}

public

reentrantreadwritelock()

public

reentrantreadwritelock(boolean fair)

public reentrantreadwritelock.writelock writelock() 

public reentrantreadwritelock.readlock readlock()

static

final

class

fairsync

extends

sync

final

boolean readershouldblock()

}

static

final

class

nonfairsync

extends

sync

final

boolean readershouldblock()

}

public

void

lock()

if (tryacquireshared(arg) < 0)

doacquireshared(arg);

protected final int tryacquireshared(int unused) 

//如果當前執行緒重入了,記錄firstreaderholdcount

else

if (firstreader == current)

//當前讀執行緒和第乙個讀執行緒不同,記錄每乙個執行緒讀的次數

else

return

1; }

//否則,迴圈嘗試

return fulltryacquireshared(current);

}

final

int fulltryacquireshared(thread current)

//如果讀執行緒需要阻塞

else

if (readershouldblock())

//說明有別的讀執行緒占有了鎖

else

}if (rh.count == 0)

return -1;}}

//如果讀鎖達到了最大值,丟擲異常

if (sharedcount(c) == max_count)

throw

new error("maximum lock count exceeded");

//如果成功更改狀態,成功返回

if (compareandsetstate(c, c + shared_unit)) else

if (firstreader == current) else

return

1; }

}}

public

void

lock()

public

final

void

acquire(int arg)

protected

final

boolean

tryacquire(int acquires)

//如果當前沒有寫鎖或者讀鎖,如果寫執行緒應該阻塞或者cas失敗,返回false

if (writershouldblock() ||

!compareandsetstate(c, c + acquires))

return

false;

//否則將當前執行緒置為獲得寫鎖的執行緒,返回true

setexclusiveownerthread(current);

return

true;

}

public

void

unlock()

public

final

boolean

releaseshared(int arg)

return

false;

}

protected

final

boolean tryreleaseshared(int unused)

//否則,是holdcounter中計數-1

else

--rh.count;

}//死迴圈

for (;;)

}

public

void

unlock()

public

final

boolean

release(int arg)

return

false;

}

protected

final

boolean

tryrelease(int releases)

protected thread getowner()
final thread getowner()
public

intgetreadlockcount()

final

int getreadlockcount()

static

int sharedcount(int c)

final

int getreadholdcount()

static

final

class

holdcounter

static

final

class

threadlocalholdcounter

extends

threadlocal

}

除此之外,對於第乙個讀執行緒有特殊的處理,sync中有如下兩個變數:

private

transient thread firstreader = null;

private

transient

int firstreaderholdcount;

final int getwriteholdcount()

深入理解Socket的讀寫

對於linux網路程式設計,有很多坑需要我們去踩。在這個時候,我們才會知道理論知識的重要性。無論是哪種語言,網路程式設計都可以寫成厚厚的一本書。舉個例子,比如 當網路斷掉,我們呼叫write去往socket中寫入資料,為什麼返回正常寫入呢?所以有空多看看 tcp ip詳解 unix網路程式設計 等經...

深入理解悲觀鎖和樂觀鎖

悲觀鎖 pessimistic lock 顧名思義,就是很悲觀,每次去拿資料的時候都認為別人會修改,所以每次在拿資料的時候都會上鎖,這樣別人想拿這個資料就會block直到它拿到鎖。樂觀鎖 optimistic lock 顧名思義,就是很樂觀,每次去拿資料的時候都認為別人不會修改,所以不會上鎖,但是在...

深入理解分布式鎖

如上圖,在分布式系統中,訂單模組為了迎戰高併發,訂單服務被橫向拆分,拆分成了不同的程序,就像上圖,兩個人同時訪問訂單服務,然後訂單系統1和訂單系統2共用乙個mysql當成資料庫,經過他們查詢發現僅有一件商品,所以他們自個認為都可以下單 如果不加鎖限制,可能會出現庫存減為負數的情況 怎麼辦呢?如上圖m...