[Beats 99.6%] Using 3 semaphore - easy to understand
Full code with test suites\nhttps://github.com/Freeze777/SDE-Interviewer-Notes/blob/main/LeetCodeJava/src/main/java/leetcode/concurrency/ZeroEvenOdd.java\n\n# C
# Full code with test suites\nhttps://github.com/Freeze777/SDE-Interviewer-Notes/blob/main/LeetCodeJava/src/main/java/leetcode/concurrency/ZeroEvenOdd.java\n\n# Code\n```\nclass ZeroEvenOdd {\n    private final Semaphore zero = new Semaphore(1);\n    private final Semaphore even = new Semaphore(0);\n    private final Semaphore odd = new Semaphore(0);\n    final int n;\n\n    public ZeroEvenOdd(int n) {\n        this.n = n;\n    }\n\n    public void zero(IntConsumer printNumber) throws InterruptedException {\n        for (int i = 1; i <= n; i++) {\n            zero.acquire();\n            printNumber.accept(0);\n            if (i % 2 == 1) odd.release();\n            else even.release();\n        }\n    }\n\n    public void even(IntConsumer printNumber) throws InterruptedException {\n        for (int i = 2; i <= n; i += 2) {\n            even.acquire();\n            printNumber.accept(i);\n            zero.release();\n        }\n    }\n\n    public void odd(IntConsumer printNumber) throws InterruptedException {\n        for (int i = 1; i <= n; i += 2) {\n            odd.acquire();\n            printNumber.accept(i);\n            zero.release();\n        }\n    }\n}\n```
['Java']