Simple Java Semaphore
\n\nimport java.util.concurrent.Semaphore;\n\nclass DiningPhilosophers {\n\n    private Semaphore[] forkSemaphores;\n\n    public DiningPhilosophers() {\n      
```\n\nimport java.util.concurrent.Semaphore;\n\nclass DiningPhilosophers {\n\n    private Semaphore[] forkSemaphores;\n\n    public DiningPhilosophers() {\n        forkSemaphores = new Semaphore[5];\n        for (int i = 0; i < 5; i++)\n            forkSemaphores[i] = new Semaphore(1);\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 right = philosopher;\n        int left = (philosopher + 1) % 5;\n\n        if (philosopher < 4) {\n            forkSemaphores[left].acquire();\n            forkSemaphores[right].acquire();\n        } else {\n            forkSemaphores[right].acquire();\n            forkSemaphores[left].acquire();\n        }\n\n        pickLeftFork.run();\n        pickRightFork.run();\n        eat.run();\n        putLeftFork.run();\n        putRightFork.run();\n\n        forkSemaphores[left].release();\n        forkSemaphores[right].release();\n    }\n\n\n}\n\n```
[]