Java 信號量Semaphore的實現(xiàn)
近日于LeetCode看題遇1114 按序打印,獲悉一解法使用了Semaphore,順勢研究,記心得于此。
此解視Semaphore為鎖,以保證同一時刻單線程的順序執(zhí)行。在此原題上,我作出如下更改。
package test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class SemaphoreDemo {
static Semaphore A;
static Semaphore B;
static Semaphore C;
public static void main(String[] args) throws InterruptedException {
A = new Semaphore(1);
B = new Semaphore(0);
C = new Semaphore(0);
ExecutorService ex=Executors.newFixedThreadPool(10);
for (int i = 0; i <7; i++) {
ex.execute(new R1());
ex.execute(new R2());
ex.execute(new R3());
}
ex.shutdown();
}
public static class R1 implements Runnable{
@Override
public void run() {
try {
// A.acquire();
System.out.println("1"+Thread.currentThread().getName());
// B.release();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static class R2 implements Runnable{
@Override
public void run() {
try {
// B.acquire();
System.out.println("2"+Thread.currentThread().getName());
// C.release();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static class R3 implements Runnable{
@Override
public void run() {
try {
// C.acquire();
System.out.println("3"+Thread.currentThread().getName());
// A.release();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
10個線程的常量池中,分別調(diào)用R1,R2,R3的方法多次,控制臺輸出對應(yīng)各方法名拼接執(zhí)行該方法的線程名。多次執(zhí)行結(jié)果各不相同:
1pool-1-thread-1 2pool-1-thread-2 1pool-1-thread-4 3pool-1-thread-6 2pool-1-thread-5 3pool-1-thread-3 1pool-1-thread-7 2pool-1-thread-8 3pool-1-thread-9 3pool-1-thread-1 2pool-1-thread-8 1pool-1-thread-4 3pool-1-thread-1 1pool-1-thread-2 2pool-1-thread-9 1pool-1-thread-10 3pool-1-thread-1 2pool-1-thread-5 1pool-1-thread-6 3pool-1-thread-4 2pool-1-thread-8
1pool-1-thread-1 2pool-1-thread-2 3pool-1-thread-3 1pool-1-thread-4 2pool-1-thread-5 3pool-1-thread-6 1pool-1-thread-7 2pool-1-thread-8 3pool-1-thread-9 1pool-1-thread-10 3pool-1-thread-1 1pool-1-thread-4 2pool-1-thread-8 3pool-1-thread-3 2pool-1-thread-10 1pool-1-thread-2 2pool-1-thread-9 3pool-1-thread-4 1pool-1-thread-7 3pool-1-thread-6 2pool-1-thread-5
方法能調(diào)用,多線程下卻無法保證方法的順序執(zhí)行。使用Semaphore后,代碼為:
package test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class SemaphoreDemo {
static Semaphore A;
static Semaphore B;
static Semaphore C;
public static void main(String[] args) throws InterruptedException {
A = new Semaphore(1);
B = new Semaphore(0);
C = new Semaphore(0);
ExecutorService ex=Executors.newFixedThreadPool(10);
for (int i = 0; i <7; i++) {
ex.execute(new R1());
ex.execute(new R2());
ex.execute(new R3());
}
ex.shutdown();
}
public static class R1 implements Runnable{
@Override
public void run() {
try {
A.acquire();
System.out.println("1"+Thread.currentThread().getName());
B.release();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static class R2 implements Runnable{
@Override
public void run() {
try {
B.acquire();
System.out.println("2"+Thread.currentThread().getName());
C.release();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static class R3 implements Runnable{
@Override
public void run() {
try {
C.acquire();
System.out.println("3"+Thread.currentThread().getName());
A.release();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
多次運(yùn)行結(jié)果皆能保證1、2、3的順序:
1pool-1-thread-1 2pool-1-thread-2 3pool-1-thread-3 1pool-1-thread-4 2pool-1-thread-5 3pool-1-thread-6 1pool-1-thread-7 2pool-1-thread-8 3pool-1-thread-9 1pool-1-thread-10 2pool-1-thread-1 3pool-1-thread-2 1pool-1-thread-3 2pool-1-thread-4 3pool-1-thread-5 1pool-1-thread-6 2pool-1-thread-9 3pool-1-thread-7 1pool-1-thread-10 2pool-1-thread-8 3pool-1-thread-1
附上api文檔鏈接 Semaphore
A = new Semaphore(1);
B = new Semaphore(0);
C = new Semaphore(0);
進(jìn)入R2、R3方法的線程會執(zhí)行acquire()方法,而B、C中的計數(shù)器為0獲取不到許可,阻塞直到一個可用,或者線程被中斷,不能繼續(xù)執(zhí)行。R1方法中A尚有1個許可可拿到,方法執(zhí)行,并給B發(fā)布一個許可,若B先于A執(zhí)行acquire(),此時B為阻塞狀態(tài),則獲取到剛剛發(fā)布的許可,該線程被重新啟用。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot RocketMq實現(xiàn)過程詳解
這篇文章主要介紹了Springboot RocketMq實現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-05-05
java Split 實現(xiàn)去除一個空格和多個空格
這篇文章主要介紹了java Split 實現(xiàn)去除一個空格和多個空格,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
深入了解Maven Settings.xml文件的結(jié)構(gòu)和功能
這篇文章主要為大家介紹了Maven Settings.xml文件基本結(jié)構(gòu)和功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
SSH框架網(wǎng)上商城項目第11戰(zhàn)之查詢和刪除商品功能實現(xiàn)
這篇文章主要為大家詳細(xì)介紹了SSH框架網(wǎng)上商城項目第11戰(zhàn)之查詢和刪除商品功能實現(xiàn)的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-06-06

