学完操作系统,习惯用信号量了
class Foo {
Semaphore s1, s2;
public Foo() {
s1 = new Semaphore(0);
s2 = new Semaphore(0);
}
public void first(Runnable printFirst) throws InterruptedException {
printFirst.run();
s1.release();
}
public void second(Runnable printSecond) throws InterruptedException {
s1.acquire();
printSecond.run();
s2.release();
}
public void third(Runnable printThird) throws InterruptedException {
s2.acquire();
printThird.run();
}
}