[Java] Faster than 100% using Conditions and 2 flags
Runtime: 7 ms, faster than 100.00% of Java online submissions for Print Zero Even Odd.\nMemory Usage: 37.9 MB, less than 92.35% of Java online submissions for P
Runtime: 7 ms, faster than 100.00% of Java online submissions for Print Zero Even Odd.\nMemory Usage: 37.9 MB, less than 92.35% of Java online submissions for Print Zero Even Odd.\n\n```\nclass ZeroEvenOdd {\n    private int n;\n    private boolean zero = true;\n    private boolean nextOdd = true;\n    \n    private Lock lock = new ReentrantLock();\n    private Condition wantsZero = lock.newCondition();\n    private Condition wantsNumber = lock.newCondition();\n    \n    public ZeroEvenOdd(int n) {\n        this.n = n;\n    }\n\n    // printNumber.accept(x) outputs "x", where x is an integer.\n    public void zero(IntConsumer printNumber) throws InterruptedException {\n        lock.lock();\n        for (int i=0; i<n; i++) {\n            while (!zero) wantsZero.await();\n            \n            printNumber.accept(0);\n            \n            zero = false;\n            wantsNumber.signalAll();\n        }\n        lock.unlock();\n    }\n\n    public void even(IntConsumer printNumber) throws InterruptedException {\n        lock.lock();\n        for (int i=2; i<=n; i+=2) {\n            while (zero || nextOdd == true) wantsNumber.await();\n            \n            printNumber.accept(i);\n            \n            zero = true;\n            nextOdd = true;\n            wantsZero.signalAll();\n        }\n        lock.unlock();\n    }\n\n    public void odd(IntConsumer printNumber) throws InterruptedException {\n        lock.lock();\n        for (int i=1; i<=n; i+=2) {\n            while (zero || nextOdd == false) wantsNumber.await();\n            \n            printNumber.accept(i);\n            \n            zero = true;\n            nextOdd = false;\n            wantsZero.signalAll();\n        }\n        lock.unlock();\n    }\n}\n```
[]