[Java/Python 3] Two clean codes either language.
Method 1: Semaphore\n\nJava:\n\nimport java.util.concurrent.*;\n\nclass ZeroEvenOdd {\n\n    private int n;\n    private Semaphore zeroSem, oddSem, evenSem;\n  
**Method 1: Semaphore**\n\n**Java:**\n```\nimport java.util.concurrent.*;\n\nclass ZeroEvenOdd {\n\n    private int n;\n    private Semaphore zeroSem, oddSem, evenSem;\n    \n    public ZeroEvenOdd(int n) {\n        this.n = n;\n        zeroSem = new Semaphore(1);\n        oddSem = new Semaphore(0);\n        evenSem = 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            zeroSem.acquire();\n            printNumber.accept(0);\n            (i % 2 == 0 ? oddSem : evenSem).release(); // Alternately release odd() and even().\n        }\n    }\n\n    public void even(IntConsumer printNumber) throws InterruptedException {\n        for (int i = 2; i <= n; i += 2) {\n            evenSem.acquire();\n            printNumber.accept(i);\n            zeroSem.release();\n        }\n    }\n\n    public void odd(IntConsumer printNumber) throws InterruptedException {\n        for (int i = 1; i <= n; i += 2) {\n            oddSem.acquire();\n            printNumber.accept(i);\n            zeroSem.release();\n        }\n    }\n\t\n}\n```\n**Python 3:**\n```\nfrom threading import Semaphore\n\nclass ZeroEvenOdd:\n    def __init__(self, n):\n        self.n = n\n        self.zeroSem = Semaphore() # default is 1.\n        self.oddSem = Semaphore(0)       \n        self.evenSem = Semaphore(0)       \n        \n\t# printNumber(x) outputs "x", where x is an integer.\n    def zero(self, printNumber: \'Callable[[int], None]\') -> None:\n        for i in range(self.n):\n            self.zeroSem.acquire()\n            printNumber(0)\n            (self.evenSem if i % 2 else self.oddSem).release()  # Alternately release oddSem and evenSem.   \n        \n    def even(self, printNumber: \'Callable[[int], None]\') -> None:\n        for i in range(2, self.n + 1, 2):\n            self.evenSem.acquire()\n            printNumber(i)\n            self.zeroSem.release()\n        \n    def odd(self, printNumber: \'Callable[[int], None]\') -> None:\n        for i in range(1, self.n + 1, 2):\n            self.oddSem.acquire()\n            printNumber(i)\n            self.zeroSem.release()\n        \n``` \n----\n\n**Method 2:**\n**Java Synchronization**\n```\n    private int n;\n    private int sem = 0;\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 synchronized void zero(IntConsumer printNumber) throws InterruptedException {\n        for (int i = 0; i < n; ++i) {\n            while (sem != 0) // need sem == 0 to unlock.\n                wait();\n            printNumber.accept(0);\n            sem = i % 2 == 0 ? 1 : 2;\n            notifyAll();\n        }\n    }\n\n    public synchronized void even(IntConsumer printNumber) throws InterruptedException {\n        for (int i = 2; i <= n; i += 2) {\n            while (sem != 2) // need sem == 2 to unlock.\n                wait();\n            printNumber.accept(i);\n            sem = 0;\n            notifyAll();\n        }\n    }\n\n    public synchronized void odd(IntConsumer printNumber) throws InterruptedException {\n        for (int i = 1; i <= n; i += 2) {\n            while (sem != 1) // need sem == 1 to unlock.\n                wait();\n            printNumber.accept(i);\n            sem = 0;\n            notifyAll();\n        }\n    }\n```\n\n**Python 3 Condition:**\n```\nfrom threading import Condition\n\nclass ZeroEvenOdd:\n    def __init__(self, n):\n        self.n = n\n        self.sem = 0\n        self.c = Condition()       \n        \n\t# printNumber(x) outputs "x", where x is an integer.\n    def zero(self, printNumber: \'Callable[[int], None]\') -> None:\n        for i in range(self.n):\n            with self.c:\n                while self.sem: self.c.wait()\n                printNumber(0)\n                self.sem = 2 if i % 2 else 1  # Alternately turn to odd() and even().   \n                self.c.notify_all()\n        \n    def even(self, printNumber: \'Callable[[int], None]\') -> None:\n        for i in range(2, self.n + 1, 2):\n            with self.c:\n                while self.sem - 2: self.c.wait()\n                printNumber(i)\n                self.sem = 0\n                self.c.notify_all()\n        \n    def odd(self, printNumber: \'Callable[[int], None]\') -> None:\n        for i in range(1, self.n + 1, 2):\n            with self.c:\n                while self.sem - 1: self.c.wait()\n                printNumber(i)\n                self.sem = 0\n                self.c.notify_all()\n```
[]