Java | Using 3 semaphores | Simple solution
Just chain the semaphores cleverly\n\nclass ZeroEvenOdd {\n    private int n;\n    Semaphore s0 = new Semaphore(1);\n    Semaphore s1 = new Semaphore(0); // odd
Just chain the semaphores cleverly\n```\nclass ZeroEvenOdd {\n    private int n;\n    Semaphore s0 = new Semaphore(1);\n    Semaphore s1 = new Semaphore(0); // odd\n    Semaphore s2 = new Semaphore(0); // even\n    \n\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        boolean flag = true; // if even\n        for(int i = 0; i < n; i++){\n            s0.acquire();\n            printNumber.accept(0);\n            flag = !flag;\n            if(flag) s2.release();\n            else s1.release();\n        }        \n    }\n\n    public void even(IntConsumer printNumber) throws InterruptedException {\n        for(int i = 2; i <= n; i+=2){\n            s2.acquire();\n            printNumber.accept(i);\n            s0.release();\n        }        \n    }\n\n    public void odd(IntConsumer printNumber) throws InterruptedException {\n        for(int i = 1; i <= n; i+=2){\n            s1.acquire();\n            printNumber.accept(i);\n            s0.release();\n        }        \n    }\n}\n```
['Java']