当前位置 博文首页 > 无限迭代中......:LeetCode 1114 按序打印

    无限迭代中......:LeetCode 1114 按序打印

    作者:[db:作者] 时间:2021-07-19 16:25

    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
    下一篇:没有了