Untitled
unknown
plain_text
2 years ago
1.5 kB
9
Indexable
class Foo {
private final Lock lock = new ReentrantLock();
private final Condition condition2 = lock.newCondition();
private final Condition condition3 = lock.newCondition();
private boolean isFirstPrinted = false;
private boolean isSecondPrinted = false;
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
lock.lock();
try {
// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
isFirstPrinted = true;
condition2.signalAll();
} finally {
lock.unlock();
}
}
public void second(Runnable printSecond) throws InterruptedException {
lock.lock();
try {
while (!isFirstPrinted) {
condition2.await();
}
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
isSecondPrinted = true;
condition3.signalAll();
} finally {
lock.unlock();
}
}
public void third(Runnable printThird) throws InterruptedException {
lock.lock();
try {
while (!isSecondPrinted) {
condition3.await();
}
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
} finally {
lock.unlock();
}
}
}Editor is loading...