總結:全部的解法都是一個理念,不管採用何種方式,開始時必須執行first線程,而後設置條件知足second執行而first和third線程都不能執行,同時只有first線程執行完才能給與該條件,而後設置條件知足third執行而first和second線程都不能執行,同時只有second線程執行成功後才能給與該條件函數
解法一:Synchronized鎖和控制變量ui
public class Foo {
//控制變量
private int flag = 0;
//定義Object對象爲鎖
private Object lock = new Object();
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
synchronized (lock){
//若是flag不爲0則讓first線程等待,while循環控制first線程若是不滿住條件就一直在while代碼塊中,防止出現中途跳入,執行下面的代碼,其他線程while循環同理
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則讓二號等待
while (flag != 1){
lock.wait();
}
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
//若是成員變量爲 1 ,則表明first線程剛執行完,因此執行second,而且改變成員變量爲 2
flag = 2;
//喚醒其他全部的線程
lock.notifyAll();
}
}
public void third(Runnable printThird) throws InterruptedException {
synchronized (lock){
//若是flag不等於2 則一直處於等待的狀態
while (flag != 2){
lock.wait();
}
// printThird.run() outputs "third". Do not change or remove this line.
//若是成員變量爲 2 ,則表明second線程剛執行完,因此執行third,而且改變成員變量爲 0
printThird.run();
flag = 0;
lock.notifyAll();
}
}
}
解法二:CountDownLatchthis
public class Foo {
//聲明兩個 CountDownLatch變量
private CountDownLatch countDownLatch01;
private CountDownLatch countDownLatch02;spa
public Foo() {
//初始化每一個CountDownLatch的值爲1,表示有一個線程執行完後,執行等待的線程
countDownLatch01 = new CountDownLatch(1);
countDownLatch02 = new CountDownLatch(1);
}
public void first(Runnable printFirst) throws InterruptedException {
//當前只有first線程沒有任何的阻礙,其他兩個線程都處於等待階段
// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
//直到CountDownLatch01裏面計數爲0才執行因調用該countDownCatch01.await()而等待的線程
countDownLatch01.countDown();
}
public void second(Runnable printSecond) throws InterruptedException {
//只有countDownLatch01爲0才能經過,不然會一直阻塞
countDownLatch01.await();
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
//直到CountDownLatch02裏面計數爲0才執行因調用該countDownCatch02.await()而等待的線程
countDownLatch02.countDown();
}
public void third(Runnable printThird) throws InterruptedException {
//只有countDownLatch02爲0才能經過,不然會一直阻塞
countDownLatch02.await();
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
}
}線程
解法三:Semaphore(信號量)
Semaphore與CountDownLatch類似,不一樣的地方在於Semaphore的值被獲取到後是能夠釋放的,並不像CountDownLatch那樣一直減到底對象
得到Semaphore的線程處理完它的邏輯以後,你就能夠調用它的Release()函數將它的計數器從新加1,這樣其它被阻塞的線程就能夠獲得調用了rem
public class Foo03 {
//聲明兩個 Semaphore變量
private Semaphore spa,spb;
public Foo03() {
//初始化Semaphore爲0的緣由:若是這個Semaphore爲零,若是另外一線程調用(acquire)這個Semaphore就會產生阻塞,即可以控制second和third線程的執行
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,另一個線程才能夠調用(acquire)
spa.release();
}
public void second(Runnable printSecond) throws InterruptedException {
//只有spa爲1才能執行acquire,若是爲0就會產生阻塞
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就會阻塞
spb.acquire();
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
}
}it