java信號量控制線程打印順序的示例分享
import java.util.concurrent.Semaphore;
public class ThreeThread {
public static void main(String[] args) throws InterruptedException {
Semaphore sempA = new Semaphore(1);
Semaphore sempB = new Semaphore(0);
Semaphore sempC = new Semaphore(0);
int N=100;
Thread threadA = new PrintThread(N, sempA, sempB, "A");
Thread threadB = new PrintThread(N, sempB, sempC, "B");
Thread threadC = new PrintThread(N, sempC, sempA, "C");
threadA.start();
threadB.start();
threadC.start();
}
static class PrintThread extends Thread{
int N;
Semaphore curSemp;
Semaphore nextSemp;
String name;
public PrintThread(int n, Semaphore curSemp, Semaphore nextSemp, String name) {
N = n;
this.curSemp = curSemp;
this.nextSemp = nextSemp;
this.name = name;
}
public void run() {
for (int i = 0; i < N; ++i) {
try {
curSemp.acquire();
System.out.println(name);
nextSemp.release();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
}
相關(guān)文章
注冊中心配置了spring?security后客戶端啟動報錯
這篇文章主要為大家介紹了注冊中心配置了spring?security后客戶端啟動報錯問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07SSH框架網(wǎng)上商城項目第23戰(zhàn)之在線支付功能實現(xiàn)
這篇文章主要為大家詳細(xì)介紹了SSH框架網(wǎng)上商城項目第23戰(zhàn)之在線支付功能實現(xiàn),感興趣的小伙伴們可以參考一下2016-06-06SpringSecurity 手機號登錄功能實現(xiàn)
這篇文章主要介紹了SpringSecurity 手機號登錄功能實現(xiàn),本文通過實例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2023-12-12詳解Java 序列化與反序列化(Serialization)
這篇文章主要介紹了Java 序列化與反序列化(Serialization),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí) 吧2019-03-03spring security實現(xiàn)下次自動登錄功能過程解析
這篇文章主要介紹了spring security實現(xiàn)記住我下次自動登錄功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11Spring Security認(rèn)證提供程序示例詳解
這篇文章主要給大家介紹了關(guān)于Spring Security認(rèn)證提供程序的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Spring Security具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05Java進(jìn)行反編譯生成.java文件方式(javap、jad下載安裝使用)
這篇文章主要介紹了Java進(jìn)行反編譯生成.java文件方式(javap、jad下載安裝使用),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12