当前位置 博文首页 > 无限迭代中......:LeetCode 1114 按序打印
https://leetcode-cn.com/problems/print-in-order/
class Foo {
private AtomicInteger jobDone = new AtomicInteger(0);
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
// printFirst.run() outputs "first".
printFirst.run();
// mark the first job as done, by increasing its count.
jobDone.incrementAndGet();
}
public void second(Runnable printSecond) throws InterruptedException {
while (jobDone.get() != 1);
// waiting for the first job to be done.
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
jobDone.incrementAndGet();
}
public void third(Runnable printThird) throws InterruptedException {
while (jobDone.get() != 2);
// waiting for the first job to be done.
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
jobDone.incrementAndGet();
}
}
cs