Java wait and notify
\tArray eating indicates who\'s eating\n2. \tin loop, acquire lock and check if left or right philosopher is eating, \n\t \t if true, wait(which will relase loc
1. \tArray `eating` indicates who\'s eating\n2. \tin loop, acquire `lock` and check if left or right philosopher is eating, \n\t* \t if true, wait(which will relase lock), after notified, next loop\n\t* \t otherwise, mark eating and  break\n3. do operations without lock, pick porks, eat, put down pork etc.\n4. in synchronized lock, mark not eating and notify.\n```\nclass DiningPhilosophers {\n    boolean[] eating;\n    Object lock;\n    public DiningPhilosophers() {\n        eating=new boolean[5];\n        lock=new Object();\'\n    }\n\n    // call the run() method of any runnable to execute its code\n    public void wantsToEat(int philosopher,\n                           Runnable pickLeftFork,\n                           Runnable pickRightFork,\n                           Runnable eat,\n                           Runnable putLeftFork,\n                           Runnable putRightFork) throws InterruptedException {\n        int left=(philosopher+4)%5;\n        int right=(philosopher+1)%5;\n        while(true) {\n            synchronized(lock) {\n                if(eating[left]||eating[right]) {\n                    lock.wait();\n                    continue;\n                }\n                eating[philosopher]=true;\n            }\n            break;\n        }\n        pickLeftFork.run();\n        pickRightFork.run();\n        eat.run();\n        putLeftFork.run();\n        putRightFork.run();\n        synchronized(lock) {\n            eating[philosopher]=false;\n            lock.notifyAll();\n        }\n    }\n}\n```
[]