您好,登錄后才能下訂單哦!
這篇文章主要介紹“用線程實(shí)現(xiàn)按序打印”,在日常操作中,相信很多人在用線程實(shí)現(xiàn)按序打印問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對(duì)大家解答”用線程實(shí)現(xiàn)按序打印”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
總結(jié):所有的解法都是一個(gè)理念,無論采用何種方式,開始時(shí)必須執(zhí)行first線程,然后設(shè)置條件滿足second執(zhí)行而first和third線程都不能執(zhí)行,同時(shí)只有first線程執(zhí)行完才能給與該條件,然后設(shè)置條件滿足third執(zhí)行而first和second線程都不能執(zhí)行,同時(shí)只有second線程執(zhí)行成功后才能給與該條件
解法一:Synchronized鎖和控制變量
public class Foo {
//控制變量
private int flag = 0;
//定義Object對(duì)象為鎖
private Object lock = new Object();
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
synchronized (lock){
//如果flag不為0則讓first線程等待,while循環(huán)控制first線程如果不滿住條件就一直在while代碼塊中,防止出現(xiàn)中途跳入,執(zhí)行下面的代碼,其余線程while循環(huán)同理
while( flag != 0){
lock.wait();
}
// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
//定義成員變量為 1
flag = 1;
//喚醒其余所有的線程
lock.notifyAll();
}
}
public void second(Runnable printSecond) throws InterruptedException {
synchronized (lock){
//如果成員變量不為1則讓二號(hào)等待
while (flag != 1){
lock.wait();
}
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
//如果成員變量為 1 ,則代表first線程剛執(zhí)行完,所以執(zhí)行second,并且改變成員變量為 2
flag = 2;
//喚醒其余所有的線程
lock.notifyAll();
}
}
public void third(Runnable printThird) throws InterruptedException {
synchronized (lock){
//如果flag不等于2 則一直處于等待的狀態(tài)
while (flag != 2){
lock.wait();
}
// printThird.run() outputs "third". Do not change or remove this line.
//如果成員變量為 2 ,則代表second線程剛執(zhí)行完,所以執(zhí)行third,并且改變成員變量為 0
printThird.run();
flag = 0;
lock.notifyAll();
}
}
}
解法二:CountDownLatch
public class Foo {
//聲明兩個(gè) CountDownLatch變量
private CountDownLatch countDownLatch01;
private CountDownLatch countDownLatch02;
public Foo() {
//初始化每個(gè)CountDownLatch的值為1,表示有一個(gè)線程執(zhí)行完后,執(zhí)行等待的線程
countDownLatch01 = new CountDownLatch(1);
countDownLatch02 = new CountDownLatch(1);
}
public void first(Runnable printFirst) throws InterruptedException {
//當(dāng)前只有first線程沒有任何的阻礙,其余兩個(gè)線程都處于等待階段
// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
//直到CountDownLatch01里面計(jì)數(shù)為0才執(zhí)行因調(diào)用該countDownCatch01.await()而等待的線程
countDownLatch01.countDown();
}
public void second(Runnable printSecond) throws InterruptedException {
//只有countDownLatch01為0才能通過,否則會(huì)一直阻塞
countDownLatch01.await();
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
//直到CountDownLatch02里面計(jì)數(shù)為0才執(zhí)行因調(diào)用該countDownCatch02.await()而等待的線程
countDownLatch02.countDown();
}
public void third(Runnable printThird) throws InterruptedException {
//只有countDownLatch02為0才能通過,否則會(huì)一直阻塞
countDownLatch02.await();
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
}
}
解法三:Semaphore(信號(hào)量)
Semaphore與CountDownLatch相似,不同的地方在于Semaphore的值被獲取到后是可以釋放的,并不像CountDownLatch那樣一直減到底
獲得Semaphore的線程處理完它的邏輯之后,你就可以調(diào)用它的Release()函數(shù)將它的計(jì)數(shù)器重新加1,這樣其它被阻塞的線程就可以得到調(diào)用了
public class Foo03 {
//聲明兩個(gè) Semaphore變量
private Semaphore spa,spb;
public Foo03() {
//初始化Semaphore為0的原因:如果這個(gè)Semaphore為零,如果另一線程調(diào)用(acquire)這個(gè)Semaphore就會(huì)產(chǎn)生阻塞,便可以控制second和third線程的執(zhí)行
spa = new Semaphore(0);
spb = new Semaphore(0);
}
public void first(Runnable printFirst) throws InterruptedException {
// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
//只有等first線程釋放Semaphore后使Semaphore值為1,另外一個(gè)線程才可以調(diào)用(acquire)
spa.release();
}
public void second(Runnable printSecond) throws InterruptedException {
//只有spa為1才能執(zhí)行acquire,如果為0就會(huì)產(chǎn)生阻塞
spa.acquire();
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
spb.release();
}
public void third(Runnable printThird) throws InterruptedException {
//只有spb為1才能通過,如果為0就會(huì)阻塞
spb.acquire();
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
}
}
到此,關(guān)于“用線程實(shí)現(xiàn)按序打印”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。