import java.util.concurrent.Semaphore;classFoo{public Semaphore Semaphore_one=newSemaphore(0);public Semaphore Semaphore_two=newSemaphore(0);publicFoo(){}publicvoidfirst(Runnable printFirst)throws InterruptedException {// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
Semaphore_one.release();}publicvoidsecond(Runnable printSecond)throws InterruptedException {//阻塞
Semaphore_one.acquire();// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
Semaphore_two.release();}publicvoidthird(Runnable printThird)throws InterruptedException {
Semaphore_two.acquire();// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();}}
countdownlatch
classFoo{private CountDownLatch c2;private CountDownLatch c3;publicFoo(){
c2 =newCountDownLatch(1);
c3 =newCountDownLatch(1);}publicvoidfirst(Runnable printFirst)throws InterruptedException {// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
c2.countDown();}publicvoidsecond(Runnable printSecond)throws InterruptedException {
c2.await();// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
c3.countDown();}publicvoidthird(Runnable printThird)throws InterruptedException {
c3.await();// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();}}