8ms Java using synchronized block + wait + notify
Java\nclass DiningPhilosophers {\n    private boolean[] valid;\n    \n    private final int[] leftFork = new int[] {0, 1, 2, 3, 4};\n    private final int[] rig
```Java\nclass DiningPhilosophers {\n    private boolean[] valid;\n    \n    private final int[] leftFork = new int[] {0, 1, 2, 3, 4};\n    private final int[] rightFork = new int[] {4, 0, 1, 2, 3};\n    \n    public DiningPhilosophers() {\n        valid = new boolean[5];\n        for (int i = 0; i < 5; ++i) {\n            valid[i] = true;\n        }\n    }\n    \n    \n    // call the run() method of any runnable to execute its code\n    public void wantsToEat(int i,\n                           Runnable pickLeftFork,\n                           Runnable pickRightFork,\n                           Runnable eat,\n                           Runnable putLeftFork,\n                           Runnable putRightFork) throws InterruptedException {\n        int left = leftFork[i];\n        int right = rightFork[i];\n        \n        synchronized (valid) {\n            while (!valid[left] || !valid[right]) {\n                valid.wait();\n            }\n            valid[left] = false;\n            valid[right] = false;\n            pickLeftFork.run();\n            pickRightFork.run();\n            valid.notifyAll();\n        }\n        \n        eat.run();\n        \n        synchronized(valid) {\n            putLeftFork.run();\n            valid[left] = true;\n            valid.notifyAll();\n        }\n        \n        synchronized(valid) {\n            putRightFork.run();\n            valid[right] = true;\n            valid.notifyAll();\n        }\n    }\n}\n```
[]