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();}
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
