Jave | Detail explained method use synchronized method
Key Notes: \n * 1. need another varible to help decide which thread need to run next, as below state shows. Set to next thread state.\n * 2. need has a for loop
* Key Notes: \n * 1. need another varible to help decide which thread need to run next, as below state shows. Set to next thread state.\n * 2. need has a for loop in each thread, thus once the three threads are running, it will print out for 2*n without further call.\n\n```\nclass ZeroEvenOdd {\n    private int n;\n    private int state = 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 (state != 0) {\n                wait();\n            }\n            printNumber.accept(0);\n            //as within for loop i will increase, thus here if odd, i = 1.\n            state = i % 2 == 0 ? 1 : 2;\n            notifyAll();\n        }\n    }\n\n    public synchronized void even(IntConsumer printNumber) throws InterruptedException {\n        //here i += 2 to find next even.\n        for(int i = 2 ; i <= n; i+=2) {\n            while (state != 2) {\n                wait();\n            }\n            printNumber.accept(i);\n            state = 0;\n            notifyAll();\n        }\n    }\n\n    public synchronized void odd(IntConsumer printNumber) throws InterruptedException {\n        //here i += 2 to find next odd.\n        for(int i = 1 ; i <= n; i += 2) {\n            while (state != 1) {\n                wait();\n            }\n            printNumber.accept(i);\n            state = 0;\n            notifyAll();\n        }\n    }\n}\n```
['Java']