Skip to content

Latest commit

 

History

History
37 lines (26 loc) · 767 Bytes

Question_1114.md

File metadata and controls

37 lines (26 loc) · 767 Bytes

LeetCode Records - Question 1114 Print in Order

Attempt 1: Use AtomicInteger and while loops

class Foo {

    private AtomicInteger num;

    public Foo() {
        num = new AtomicInteger(0);
    }

    public void first(Runnable printFirst) throws InterruptedException {
        printFirst.run();

        num.incrementAndGet();
    }

    public void second(Runnable printSecond) throws InterruptedException {
        while (num.get() != 1) {}

        printSecond.run();

        num.incrementAndGet();
    }

    public void third(Runnable printThird) throws InterruptedException {
        while (num.get() != 2) {}

        printThird.run();
    }
}
  • Runtime: 9 ms (Beats: 99.83%)
  • Memory: 42.39 MB (Beats: 18.95%)