Leetcode_1114_按序打印_多线程

学完操作系统,习惯用信号量了

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();
    }
}

你可能感兴趣的:(daily_algorithm,多线程,leetcode,算法,职场和发展)