Java 5ms solution with Semaphore - easiest to understand, no fancy trick
In my point of view, to crack a concurrency problem, the best way is to start with a non-threadsafe solution and add concurrency to it. \n\nimport java.util.con
In my point of view, to crack a concurrency problem, the best way is to start with a non-threadsafe solution and add concurrency to it. \n```\nimport java.util.concurrent.*;\n\nclass ZeroEvenOdd {\n    private int n;\n    Semaphore s1, s2, s3;\n    \n    public ZeroEvenOdd(int n) {\n        this.n = n;\n        s1 = new Semaphore(1);\n        s2 = new Semaphore(0);\n        s3 = new Semaphore(0);\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            s1.acquire();\n            printNumber.accept(0);\n            if (i % 2 == 0) {\n                s3.release();\n            } else {\n                s2.release();\n            }\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            s1.release();\n        }\n    }\n\n    public void odd(IntConsumer printNumber) throws InterruptedException {\n        for (int i = 1; i <= n; i += 2) {\n            s3.acquire();\n            printNumber.accept(i);\n            s1.release();\n        }\n    }\n}\n```
[]