Java - using semaphore for each fork, and managing competition between specific 2 philosophers
very specific solution for 5 philosophers\n\n\nclass DiningPhilosophers {\n\n    private Semaphore [] sem = new Semaphore [] {new Semaphore(1),\n     new Semaph
very specific solution for 5 philosophers\n\n```\nclass DiningPhilosophers {\n\n    private Semaphore [] sem = new Semaphore [] {new Semaphore(1),\n     new Semaphore(1), new Semaphore(1), new Semaphore(1), new Semaphore(1)};\n    private Semaphore lockLastOne = new Semaphore(1);\n    \n\n    public DiningPhilosophers() {\n        \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 == 0 ? 4 : philosopher - 1;\n        \n        if(philosopher == 0 || philosopher == 4)\n            lockLastOne.acquire();\n        sem[left].acquire(); \n        \n         \n        sem[right].acquire();\n        pickLeftFork.run();\n        pickRightFork.run();\n        \n        eat.run();\n        \n        putLeftFork.run();\n        putRightFork.run();\n        \n        sem[left].release();\n        sem[right].release();\n        \n        if(philosopher == 0 || philosopher == 4)\n            lockLastOne.release();\n    }\n}\n```
['Java']