java原始碼-Semaphore
開篇
-
Semaphore是一種基於計數的訊號量。它可以設定一個閾值,基於此,多個執行緒競爭獲取許可訊號,做完自己的申請後歸還,超過閾值後,執行緒申請許可訊號將會被阻塞。
-
Semaphore可以用來構建一些物件池,資源池之類的,比如資料庫連線池,我們也可以建立計數為1的Semaphore,將其作為一種類似互斥鎖的機制,這也叫二元訊號量,表示兩種互斥狀態。
-
從原始碼角度來看,Semaphore的實現方式和CountDownLatch非常相似,基於AQS做了一些定製。通過維持AQS的鎖全域性計數state欄位來實現定量鎖的加鎖和解鎖操作。
Semaphore類定義
- Semaphore類內部的Sync繼承自AQS,作為Semaphore的公平鎖和非公平鎖的基類。
- Semaphore類內部的NonfairSync繼承自Sync類,通過非公平的方法加鎖和解鎖。
- Semaphore類內部的FairSync繼承自Sync類,通過公平的方法加鎖和解鎖。
- Semaphore的建構函式建立FairSync或NonfairSync物件賦值給Sync。
public class Semaphore implements java.io.Serializable {
private final Sync sync;
// 繼承自AQS實現抽象類Sync,作為NonfairSync和FairSync的基類。
abstract static class Sync extends AbstractQueuedSynchronizer {
private static final long serialVersionUID = 1192457210091910933L;
Sync(int permits) {
setState(permits);
}
final int getPermits() {
return getState();
}
final int nonfairTryAcquireShared(int acquires) {
for (;;) {
int available = getState();
int remaining = available - acquires;
if (remaining < 0 ||
compareAndSetState(available, remaining))
return remaining;
}
}
protected final boolean tryReleaseShared(int releases) {
for (;;) {
int current = getState();
int next = current + releases;
if (next < current) // overflow
throw new Error("Maximum permit count exceeded");
if (compareAndSetState(current, next))
return true;
}
}
final void reducePermits(int reductions) {
for (;;) {
int current = getState();
int next = current - reductions;
if (next > current) // underflow
throw new Error("Permit count underflow");
if (compareAndSetState(current, next))
return;
}
}
final int drainPermits() {
for (;;) {
int current = getState();
if (current == 0 || compareAndSetState(current, 0))
return current;
}
}
}
// 非公平鎖類定義
static final class NonfairSync extends Sync {
private static final long serialVersionUID = -2694183684443567898L;
NonfairSync(int permits) {
super(permits);
}
protected int tryAcquireShared(int acquires) {
return nonfairTryAcquireShared(acquires);
}
}
// 公平鎖類定義
static final class FairSync extends Sync {
private static final long serialVersionUID = 2014338818796000944L;
FairSync(int permits) {
super(permits);
}
protected int tryAcquireShared(int acquires) {
for (;;) {
if (hasQueuedPredecessors())
return -1;
// 判斷是否還能獲取鎖,通過遞減全域性計數state來實現
int available = getState();
int remaining = available - acquires;
if (remaining < 0 ||
compareAndSetState(available, remaining))
return remaining;
}
}
}
// 建構函式
public Semaphore(int permits) {
sync = new NonfairSync(permits);
}
// 建構函式
public Semaphore(int permits, boolean fair) {
sync = fair ? new FairSync(permits) : new NonfairSync(permits);
}
Semaphore加鎖過程
- Semaphore加鎖過程基於AQS實現的。
- Semaphore加鎖過程步驟是嘗試獲取鎖,如果獲鎖成功執行緒繼續執行,獲鎖失敗就掛起當前執行緒。
- Semaphore加鎖過程因為公平鎖和非公平鎖略有不同,但是大流程是一致的。
- tryAcquireShared()方法是嘗試的操作,doAcquireSharedInterruptibly()是獲鎖失敗的操作。
- tryAcquireShared()方法通過遞減鎖全域性技術變數state來判定是否能獲取鎖。
public void acquire() throws InterruptedException {
sync.acquireSharedInterruptibly(1);
}
public final void acquireSharedInterruptibly(int arg)
throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
if (tryAcquireShared(arg) < 0)
doAcquireSharedInterruptibly(arg);
}
protected int tryAcquireShared(int acquires) {
for (;;) {
if (hasQueuedPredecessors())
return -1;
// 通過遞減鎖全域性技術變數state來判定是否能獲取鎖
// 值小於0說明獲鎖失敗,否則代表獲鎖成功
int available = getState();
int remaining = available - acquires;
if (remaining < 0 ||
compareAndSetState(available, remaining))
return remaining;
}
}
private void doAcquireSharedInterruptibly(int arg)
throws InterruptedException {
final Node node = addWaiter(Node.SHARED);
boolean failed = true;
try {
for (;;) {
final Node p = node.predecessor();
if (p == head) {
int r = tryAcquireShared(arg);
if (r >= 0) {
setHeadAndPropagate(node, r);
p.next = null; // help GC
failed = false;
return;
}
}
if (shouldParkAfterFailedAcquire(p, node) &&
parkAndCheckInterrupt())
throw new InterruptedException();
}
} finally {
if (failed)
cancelAcquire(node);
}
}
Semaphore解鎖過程
- Semaphore解鎖過程基於AQS實現的。
- Semaphore加鎖過程步驟是嘗試釋放鎖,釋放成功後就喚醒其他等待執行緒。
- Semaphore的tryReleaseShared()方法嘗試釋放鎖,doReleaseShared()方法喚醒休眠等待執行緒。
- Semaphore的tryReleaseShared()方法通過遞增鎖的全域性計數state來實現。
public void release() {
sync.releaseShared(1);
}
public final boolean releaseShared(int arg) {
if (tryReleaseShared(arg)) {
doReleaseShared();
return true;
}
return false;
}
protected final boolean tryReleaseShared(int releases) {
for (;;) {
// 釋放鎖,通過累加全域性計數state來實現
int current = getState();
int next = current + releases;
if (next < current) // overflow
throw new Error("Maximum permit count exceeded");
if (compareAndSetState(current, next))
return true;
}
}
private void doReleaseShared() {
for (;;) {
Node h = head;
if (h != null && h != tail) {
int ws = h.waitStatus;
if (ws == Node.SIGNAL) {
if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0))
continue; // loop to recheck cases
unparkSuccessor(h);
}
else if (ws == 0 &&
!compareAndSetWaitStatus(h, 0, Node.PROPAGATE))
continue; // loop on failed CAS
}
if (h == head) // loop if head changed
break;
}
}
參考文章
相關文章
- Java併發之Semaphore原始碼解析(一)Java原始碼
- Java併發之Semaphore原始碼解析(二)Java原始碼
- Semaphore原始碼解析原始碼
- Semaphore原始碼分析原始碼
- Java併發包原始碼學習系列:同步元件Semaphore原始碼解析Java原始碼元件
- Java 併發程式設計(十五) -- Semaphore原始碼分析Java程式設計原始碼
- 深入淺出Semaphore原始碼解析原始碼
- Semaphore訊號量原始碼解析原始碼
- 原始碼分析:Semaphore之訊號量原始碼
- 併發工具類:Semaphore原始碼解讀原始碼
- 第 57 期 sync/semaphore 原始碼淺析原始碼
- Redisson 分散式鎖原始碼 11:Semaphore 和 CountDownLatchRedis分散式原始碼CountDownLatch
- go中semaphore(訊號量)原始碼解讀Go原始碼
- 多執行緒基礎(十九):Semaphore原始碼分析執行緒原始碼
- 07 併發工具類CountDownLatch、CyclicBarrier、Semaphore使用及原始碼分析CountDownLatch原始碼
- JUC原始碼學習筆記2——AQS共享和Semaphore,CountDownLatch原始碼筆記AQSCountDownLatch
- Java併發系列—工具類:SemaphoreJava
- Java併發(6)- CountDownLatch、Semaphore與AQSJavaCountDownLatchAQS
- java原始碼-CountDownLatchJava原始碼CountDownLatch
- java原始碼-BufferedReaderJava原始碼
- java原始碼-AtomicIntegerJava原始碼
- java原始碼-AtomicReferenceJava原始碼
- Java集合原始碼剖析——ArrayList原始碼剖析Java原始碼
- Java容器原始碼學習--ArrayList原始碼分析Java原始碼
- java多執行緒系列:Semaphore和ExchangerJava執行緒
- Java 併發工具類 CountDownLatch、CyclicBarrier、Semaphore、ExchangerJavaCountDownLatch
- java原始碼-java.util.ListJava原始碼
- 併發模擬-程式碼CountDownLatch,SemaphoreCountDownLatch
- Semaphore
- Java 原始碼,反碼和補碼Java原始碼
- java 原始碼分析 —BooleanJava原始碼Boolean
- Java——HashMap原始碼解析JavaHashMap原始碼
- Java——ArrayList原始碼解析Java原始碼
- Java——LinkedHashMap原始碼解析JavaHashMap原始碼
- java原始碼-ThreadPoolExecutor(2)Java原始碼thread
- Java:HashMap原始碼分析JavaHashMap原始碼
- Java String原始碼分析Java原始碼
- 【Java】ServiceLoader原始碼分析Java原始碼