Java中的Semaphore信號(hào)量簡(jiǎn)析
一、是什么?
Semaphore:信號(hào)量,用來限制能同時(shí)訪問共享資源的線程上限
二、簡(jiǎn)單使用
public class TestSemaphore {
public static void main(String[] args) {
// 1. 創(chuàng)建 semaphore 對(duì)象
Semaphore semaphore = new Semaphore(3);
// 2. 10個(gè)線程同時(shí)運(yùn)行
for (int i = 0; i < 10; i++) {
new Thread(() -> {
try {
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
log.debug("running...");
sleep(1);
log.debug("end...");
} finally {
semaphore.release();
}
}).start();
}
}
}結(jié)果:始終只有三個(gè)線程處于正在運(yùn)行的狀態(tài)

三、semaphore應(yīng)用
- 使用semaphore限流,在訪問高峰期時(shí),讓請(qǐng)求線程阻塞。當(dāng)然它只適合限制單機(jī)線程數(shù)量,并且是僅限制線程數(shù),而不是限制資源數(shù)(例如連接數(shù))
- 使用Semaphore實(shí)現(xiàn)簡(jiǎn)單連接池,對(duì)比享元模式下的實(shí)現(xiàn)(用wait和notify),性能和可讀性要更好
class Pool {
// 1. 連接池大小
private final int poolSize;
// 2. 連接對(duì)象數(shù)組
private Connection[] connections;
// 3. 連接狀態(tài)數(shù)組 0 表示空閑, 1 表示繁忙
private AtomicIntegerArray states;
private Semaphore semaphore;
// 4. 構(gòu)造方法初始化
public Pool(int poolSize) {
this.poolSize = poolSize;
// 讓許可數(shù)與資源數(shù)一致
this.semaphore = new Semaphore(poolSize);
this.connections = new Connection[poolSize];
this.states = new AtomicIntegerArray(new int[poolSize]);
for (int i = 0; i < poolSize; i++) {
connections[i] = new MockConnection("連接" + (i+1));
}
}
// 5. 借連接
public Connection borrow() {// t1, t2, t3
// 獲取許可
try {
semaphore.acquire(); // 沒有許可的線程,在此等待
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < poolSize; i++) {
// 獲取空閑連接
if(states.get(i) == 0) {
if (states.compareAndSet(i, 0, 1)) {
log.debug("borrow {}", connections[i]);
return connections[i];
}
}
}
// 不會(huì)執(zhí)行到這里
return null;
}
// 6. 歸還連接
public void free(Connection conn) {
for (int i = 0; i < poolSize; i++) {
if (connections[i] == conn) {
states.set(i, 0);
log.debug("free {}", conn);
semaphore.release();
break;
}
}
}
}四、Semaphore原理


到此這篇關(guān)于Java中的Semaphore信號(hào)量簡(jiǎn)析的文章就介紹到這了,更多相關(guān)Semaphore信號(hào)量?jī)?nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于springboot redirect重定向路徑問題總結(jié)
這篇文章主要介紹了springboot redirect重定向路徑問題總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
SpringBoot請(qǐng)求轉(zhuǎn)發(fā)的方式小結(jié)
本文主要介紹了SpringBoot請(qǐng)求轉(zhuǎn)發(fā)的方式,一共有兩大類,一種是controller控制器轉(zhuǎn)發(fā)一種是使用HttpServletRequest進(jìn)行轉(zhuǎn)發(fā),本文就詳細(xì)的介紹一下,感興趣的可以了解一下2023-09-09
詳解Java如何實(shí)現(xiàn)加密或者解密PDF文檔
PDF文檔加密是一種用于保護(hù)文件內(nèi)容的功能。這篇文章主要介紹了Java實(shí)現(xiàn)加密或者解密PDF文檔的方法,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-03-03
SpringMvc后臺(tái)接收json數(shù)據(jù)中文亂碼問題詳解
這篇文章主要介紹了SpringMvc后臺(tái)接收json數(shù)據(jù)中文亂碼問題詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
java利用CompletionService保證任務(wù)先完成先獲取到執(zhí)行結(jié)果
這篇文章主要為大家詳細(xì)介紹了java如何利用CompletionService來保證任務(wù)先完成先獲取到執(zhí)行結(jié)果,文中的示例代碼講解詳細(xì),需要的可以參考下2023-08-08
Java Map接口及其實(shí)現(xiàn)類原理解析
這篇文章主要介紹了Java Map接口及其實(shí)現(xiàn)類原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03

