您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“JUC之Semaphore源碼的示例分析”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“JUC之Semaphore源碼的示例分析”這篇文章吧。
Semaphore 主要用于限量控制并發(fā)執(zhí)行代碼的工具類, 其內(nèi)部通過 一個(gè) permit 來進(jìn)行定義并發(fā)執(zhí)行的數(shù)量。
/** * 使用非公平版本構(gòu)件 Semaphore */ public KSemaphore(int permits){ sync = new NonfairSync(permits); } /** * 指定版本構(gòu)件 Semaphore */ public KSemaphore(int permits, boolean fair){ sync = fair ? new FairSync(permits) : new NonfairSync(permits); }
/** AQS 的子類主要定義獲取釋放 lock */ abstract static class Sync extends KAbstractQueuedSynchronizer{ private static final long serialVersionUID = 1192457210091910933L; /** * 指定 permit 初始化 Semaphore */ Sync(int permits){ setState(permits); } /** * 返回剩余 permit */ final int getPermits(){ return getState(); } /** * 獲取 permit */ final int nonfairTryAcquireShared(int acquires){ for(;;){ int available = getState(); int remaining = available - acquires; // 判斷獲取 acquires 的剩余 permit 數(shù)目 if(remaining < 0 || compareAndSetState(available, remaining)){ // cas改變 state return remaining; } } } /** * 釋放 lock */ 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)){ // cas改變 state return true; } } } final void reducePermits(int reductions){ // 減少 permits for(;;){ int current = getState(); int next = current - reductions; if(next > current){ // underflow throw new Error(" Permit count underflow "); } if(compareAndSetState(current, next)){ return; } } } /** 將 permit 置為 0 */ final int drainPermits(){ for(;;){ int current = getState(); if(current == 0 || compareAndSetState(current, 0)){ return current; } } } }
/** * 調(diào)用 acquireSharedInterruptibly 響應(yīng)中斷的方式獲取 permit */ public void acquire() throws InterruptedException{ sync.acquireSharedInterruptibly(1); } /** * 調(diào)用 acquireUninterruptibly 非響應(yīng)中斷的方式獲取 permit */ public void acquireUninterruptibly(){ sync.acquireShared(1); } /** * 嘗試獲取 permit */ public boolean tryAcquire(){ return sync.nonfairTryAcquireShared(1) >= 0; } /** * 嘗試的獲取 permit, 支持超時(shí)與中斷 */ public boolean tryAcquire(long timeout, TimeUnit unit) throws InterruptedException{ return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout)); } /** * 支持中斷的獲取permit */ public void acquire(int permits) throws InterruptedException{ if(permits < 0){ throw new IllegalArgumentException(); } sync.acquireSharedInterruptibly(permits); } /** * 不響應(yīng)中斷的獲取 permit */ public void acquireUninterruptibly(int permits){ if(permits < 0) throw new IllegalArgumentException(); sync.acquireShared(permits); } /** * 嘗試獲取 permit */ public boolean tryAcquire(int permits){ if(permits < 0) throw new IllegalArgumentException(); return sync.nonfairTryAcquireShared(permits) >= 0; } /** * 嘗試 支持超時(shí)機(jī)制, 支持中斷 的獲取 permit */ public boolean tryAcquire(int permits, long timout, TimeUnit unit) throws InterruptedException{ if(permits < 0) throw new IllegalArgumentException(); return sync.tryAcquireSharedNanos(permits, unit.toNanos(timout)); }
/** * 釋放 permit */ public void release(){ sync.releaseShared(1); } /** * 釋放 permit */ public void release(int permits){ if(permits < 0) throw new IllegalArgumentException(); sync.releaseShared(permits); }
/** * 返回可用的 permit */ public int availablePermits(){ return sync.getPermits(); } /** * 消耗光 permit */ public int drainPermits(){ return sync.drainPermits(); } /** * 減少 reduction 個(gè)permit */ protected void reducePermits(int reduction){ if(reduction < 0) throw new IllegalArgumentException(); sync.reducePermits(reduction); } /** * 判斷是否是公平版本 */ public boolean isFair(){ return sync instanceof FairSync; } /** * 返回 AQS 中 Sync Queue 里面的等待線程 */ public final boolean hasQueuedThreads(){ return sync.hasQueuedThreads(); } /** * 返回 AQS 中 Sync Queue 里面的等待線程長度 */ public final int getQueueLength(){ return sync.getQueueLength(); } /** * 返回 AQS 中 Sync Queue 里面的等待線程 */ protected Collection<Thread> getQueueThreads(){ return sync.getQueuedThreads(); }
以上是“JUC之Semaphore源碼的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。