av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

Java信號量Semaphore原理及代碼實(shí)例

瀏覽:69日期:2022-08-22 15:52:23

Semaphore 通常用于限制可以訪問某些資源(物理或邏輯的)的線程數(shù)目。自從5.0開始,jdk在java.util.concurrent包里提供了Semaphore 的官方實(shí)現(xiàn),因此大家不需要自己去實(shí)現(xiàn)Semaphore。

下面的類使用信號量控制對內(nèi)容池的訪問:

import java.util.concurrent.Semaphore; class Pool { private static final int MAX_AVAILABLE = 100; private final Semaphore available = new Semaphore(MAX_AVAILABLE, true); public Object getItem() throws InterruptedException { available.acquire(); // 從此信號量獲取一個許可,在提供一個許可前一直將線程阻塞,否則線程被中斷 return getNextAvailableItem(); } public void putItem(Object x) { if (markAsUnused(x)) available.release(); // 釋放一個許可,將其返回給信號量 } // 僅作示例參考,非真實(shí)數(shù)據(jù) protected Object[] items = null; protected boolean[] used = new boolean[MAX_AVAILABLE]; protected synchronized Object getNextAvailableItem() { for (int i = 0; i < MAX_AVAILABLE; ++i) { if (!used[i]) { used[i] = true; return items[i]; } } return null; } protected synchronized boolean markAsUnused(Object item) { for (int i = 0; i < MAX_AVAILABLE; ++i) { if (item == items[i]) { if (used[i]) { used[i] = false; return true; } else return false; } } return false; } }

雖然JDK已經(jīng)提供了相關(guān)實(shí)現(xiàn),但是還是很有必要去熟悉如何使用Semaphore及其背后的原理。做一個簡單的Semaphore實(shí)現(xiàn):

class SemaphoreTest { private boolean signal = false; public synchronized void take() { this.signal = true; this.notify(); } public synchronized void release() throws InterruptedException { while (!this.signal) wait(); this.signal = false; } }

使用這個semaphore可以避免錯失某些信號通知。用take方法來代替notify,release方法來代替wait。如果某線程在調(diào)用release等待之前調(diào)用take方法,那么調(diào)用release方法的線程仍然知道take方法已經(jīng)被某個線程調(diào)用過了,因?yàn)樵揝emaphore內(nèi)部保存了take方法發(fā)出的信號。而wait和notify方法就沒有這樣的功能。

可計數(shù)的Semaphore:

class SemaphoreTest { private int signals = 0; public synchronized void take() { this.signals++; this.notify(); } public synchronized void release() throws InterruptedException { while (this.signals == 0) wait(); this.signals--; } }

Semaphore上限:

class SemaphoreTest {private int signals = 0;private int bound = 0;public SemaphoreTest(int upperBound) {this.bound = upperBound;}public synchronized void take() throws InterruptedException {while (this.signals == bound)wait();this.signals++;this.notify();}public synchronized void release() throws InterruptedException {while (this.signals == 0)wait();this.signals--;this.notify();}}

當(dāng)已經(jīng)產(chǎn)生的信號數(shù)量達(dá)到了上限,take方法將阻塞新的信號產(chǎn)生請求,直到某個線程調(diào)用release方法后,被阻塞于take方法的線程才能傳遞自己的信號。

把Semaphore當(dāng)鎖來使用:

當(dāng)信號量的數(shù)量上限是1時,Semaphore可以被當(dāng)做鎖來使用。通過take和release方法來保護(hù)關(guān)鍵區(qū)域。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 成人在线免费 | 色黄视频在线观看 | 天天操天天操天天 | 黄色三级视频网站 | 精品国产一二三区 | av免费网| 九九精品九九 | 欧美黄网站 | 国产精品一级 | 午夜精品在线观看 | 亚洲精品www久久久久久广东 | 久久亚洲一区二区 | 波多野结衣一级片 | 在线视频一区二区 | 久久综合国产 | 久久久精品在线观看 | 天天干天天草 | 亚洲日本国产 | 又黄又爽又色视频 | 黄色一级片视频 | 久久日韩精品 | 欧美国产精品一区二区 | 岛国av噜噜噜久久久狠狠av | 在线亚洲天堂 | 日韩精品一区二区在线 | 成人性生活片 | 色综合婷婷 | 亚洲福利一区 | 四虎最新地址 | 超碰免费观看 | 男人添女荫道口图片 | 中文字幕在线视频播放 | 99久久精品国产毛片 | 亚洲少妇一区 | www.狠狠| 成人在线观看视频网站 | 伊人成人在线视频 | 日韩av免费在线播放 | 五月激情久久 | 日韩午夜片 | 青青综合|