Java信號(hào)量Semaphore原理及代碼實(shí)例
Semaphore 通常用于限制可以訪(fǎng)問(wèn)某些資源(物理或邏輯的)的線(xiàn)程數(shù)目。自從5.0開(kāi)始,jdk在java.util.concurrent包里提供了Semaphore 的官方實(shí)現(xiàn),因此大家不需要自己去實(shí)現(xiàn)Semaphore。
下面的類(lèi)使用信號(hào)量控制對(duì)內(nèi)容池的訪(fǎng)問(wèn):
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(); // 從此信號(hào)量獲取一個(gè)許可,在提供一個(gè)許可前一直將線(xiàn)程阻塞,否則線(xiàn)程被中斷 return getNextAvailableItem(); } public void putItem(Object x) { if (markAsUnused(x)) available.release(); // 釋放一個(gè)許可,將其返回給信號(hào)量 } // 僅作示例參考,非真實(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及其背后的原理。
做一個(gè)簡(jiǎn)單的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; } }
使用這個(gè)semaphore可以避免錯(cuò)失某些信號(hào)通知。用take方法來(lái)代替notify,release方法來(lái)代替wait。如果某線(xiàn)程在調(diào)用release等待之前調(diào)用take方法,那么調(diào)用release方法的線(xiàn)程仍然知道take方法已經(jīng)被某個(gè)線(xiàn)程調(diào)用過(guò)了,因?yàn)樵揝emaphore內(nèi)部保存了take方法發(fā)出的信號(hào)。而wait和notify方法就沒(méi)有這樣的功能。
可計(jì)數(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)生的信號(hào)數(shù)量達(dá)到了上限,take方法將阻塞新的信號(hào)產(chǎn)生請(qǐng)求,直到某個(gè)線(xiàn)程調(diào)用release方法后,被阻塞于take方法的線(xiàn)程才能傳遞自己的信號(hào)。
把Semaphore當(dāng)鎖來(lái)使用:
當(dāng)信號(hào)量的數(shù)量上限是1時(shí),Semaphore可以被當(dāng)做鎖來(lái)使用。通過(guò)take和release方法來(lái)保護(hù)關(guān)鍵區(qū)域。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java web實(shí)現(xiàn)動(dòng)態(tài)圖片驗(yàn)證碼的示例代碼
這篇文章主要介紹了Java web實(shí)現(xiàn)動(dòng)態(tài)圖片驗(yàn)證碼的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01java簡(jiǎn)單手寫(xiě)版本實(shí)現(xiàn)時(shí)間輪算法
這篇文章主要為大家詳細(xì)介紹了java簡(jiǎn)單手寫(xiě)版本實(shí)現(xiàn)時(shí)間輪算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04SpringBoot上傳文件到本服務(wù)器 目錄與jar包同級(jí)問(wèn)題
這篇文章主要介紹了SpringBoot上傳文件到本服務(wù)器 目錄與jar包同級(jí)問(wèn)題,需要的朋友可以參考下2018-11-11關(guān)于Spring?Cloud實(shí)現(xiàn)日志管理模塊
這篇文章主要介紹了關(guān)于Spring?Cloud實(shí)現(xiàn)日志管理模塊問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11關(guān)于SpringBoot自定義條件注解與自動(dòng)配置
這篇文章主要介紹了關(guān)于SpringBoot自定義條件注解與自動(dòng)配置,Spring Boot的核心功能就是為整合第三方框架提供自動(dòng)配置,而本文則帶著大家實(shí)現(xiàn)了自己的自動(dòng)配置和Starter,需要的朋友可以參考下2023-07-07java算法實(shí)現(xiàn)預(yù)測(cè)雙色球中獎(jiǎng)號(hào)碼
這篇文章主要介紹了java算法實(shí)現(xiàn)預(yù)測(cè)雙色球中獎(jiǎng)號(hào)碼的相關(guān)資料,需要的朋友可以參考下2015-12-12spring boot使用sharding jdbc的配置方式
這篇文章主要介紹了spring boot使用sharding jdbc的配置方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12