Java中的Semaphore信號量簡析
更新時間:2023年12月16日 09:07:12 作者:小晨想好好學習
這篇文章主要介紹了Java中的Semaphore信號量簡析,Semaphore:信號量,用來限制能同時訪問共享資源的線程上限,使用Semaphore實現(xiàn)簡單連接池,對比享元模式下的實現(xiàn)(用wait和notify),性能和可讀性要更好,需要的朋友可以參考下
一、是什么?
Semaphore:信號量,用來限制能同時訪問共享資源的線程上限
二、簡單使用
public class TestSemaphore {
public static void main(String[] args) {
// 1. 創(chuàng)建 semaphore 對象
Semaphore semaphore = new Semaphore(3);
// 2. 10個線程同時運行
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é)果:始終只有三個線程處于正在運行的狀態(tài)

三、semaphore應(yīng)用
- 使用semaphore限流,在訪問高峰期時,讓請求線程阻塞。當然它只適合限制單機線程數(shù)量,并且是僅限制線程數(shù),而不是限制資源數(shù)(例如連接數(shù))
- 使用Semaphore實現(xiàn)簡單連接池,對比享元模式下的實現(xiàn)(用wait和notify),性能和可讀性要更好
class Pool {
// 1. 連接池大小
private final int poolSize;
// 2. 連接對象數(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];
}
}
}
// 不會執(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信號量簡析的文章就介紹到這了,更多相關(guān)Semaphore信號量內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于springboot redirect重定向路徑問題總結(jié)
這篇文章主要介紹了springboot redirect重定向路徑問題總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
SpringBoot請求轉(zhuǎn)發(fā)的方式小結(jié)
本文主要介紹了SpringBoot請求轉(zhuǎn)發(fā)的方式,一共有兩大類,一種是controller控制器轉(zhuǎn)發(fā)一種是使用HttpServletRequest進行轉(zhuǎn)發(fā),本文就詳細的介紹一下,感興趣的可以了解一下2023-09-09
SpringMvc后臺接收json數(shù)據(jù)中文亂碼問題詳解
這篇文章主要介紹了SpringMvc后臺接收json數(shù)據(jù)中文亂碼問題詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-09-09
java利用CompletionService保證任務(wù)先完成先獲取到執(zhí)行結(jié)果
這篇文章主要為大家詳細介紹了java如何利用CompletionService來保證任務(wù)先完成先獲取到執(zhí)行結(jié)果,文中的示例代碼講解詳細,需要的可以參考下2023-08-08

