Using wait() and notifyAll()
Code\njava []\nclass ZeroEvenOdd {\n    private int n;\n    private static Object lock;\n    private boolean isZeroTurn;\n    private boolean isEvenTurn;\n\n   
# Code\n```java []\nclass ZeroEvenOdd {\n    private int n;\n    private static Object lock;\n    private boolean isZeroTurn;\n    private boolean isEvenTurn;\n\n    public ZeroEvenOdd(int n) {\n        this.n = n;\n        lock = new Object();\n        isZeroTurn = true;\n        isEvenTurn = false;\n    }\n\n    // printNumber.accept(x) outputs "x", where x is an integer.\n    public void zero(IntConsumer printNumber) throws InterruptedException {\n        for (int i = 0; i < n; i++) {\n            synchronized (lock) {\n                while (!isZeroTurn) {\n                    lock.wait();\n                }\n                printNumber.accept(0);\n                isZeroTurn = false;\n                isEvenTurn = (i % 2 != 0);\n                lock.notifyAll();\n            }\n        }\n    }\n\n    public void even(IntConsumer printNumber) throws InterruptedException {\n        for (int i = 2; i <= n; i += 2) {\n            synchronized (lock) {\n                while (isZeroTurn || !isEvenTurn) {\n                    lock.wait();\n                }\n                printNumber.accept(i);\n                isZeroTurn = true;\n                lock.notifyAll();\n            }\n        }\n    }\n\n    public void odd(IntConsumer printNumber) throws InterruptedException {\n        for (int i = 1; i <= n; i += 2) {\n            synchronized (lock) {\n                while (isZeroTurn || isEvenTurn) {\n                    lock.wait();\n                }\n                printNumber.accept(i);\n                isZeroTurn = true;\n                lock.notifyAll();\n            }\n        }\n    }\n}\n\n```
['Java']