Simple Java solution, with 5 Semaphores
\nclass DiningPhilosophers {\n\n\n    private Semaphore[] semaphores = new Semaphore[5];\n\n    private Map<Integer, Semaphore[]> map = new HashMap<>();\n\n    
```\nclass DiningPhilosophers {\n\n\n    private Semaphore[] semaphores = new Semaphore[5];\n\n    private Map<Integer, Semaphore[]> map = new HashMap<>();\n\n    public DiningPhilosophers() {\n        for (int i=0; i<5; i++) {\n            semaphores[i] = new Semaphore(1);\n        }\n        map.put(0, new Semaphore[]{semaphores[0], semaphores[4]});\n        map.put(1, new Semaphore[]{semaphores[0], semaphores[1]});\n        map.put(2, new Semaphore[]{semaphores[1], semaphores[2]});\n        map.put(3, new Semaphore[]{semaphores[2], semaphores[3]});\n        map.put(4, new Semaphore[]{semaphores[3], semaphores[4]});\n    }\n\n    // call the run() method of any runnable to execute its code\n    public void wantsToEat(int n,\n        Runnable pickLeftFork,\n        Runnable pickRightFork,\n        Runnable eat,\n        Runnable putLeftFork,\n        Runnable putRightFork) throws InterruptedException {\n        boolean locked = tryLock(n);\n        while (!locked) {\n            Thread.sleep(0);    ///// Interesting. Without this \'TIMED OUT\' will happen.\n            locked = tryLock(n);\n        }\n        pickLeftFork.run();\n        pickRightFork.run();\n        eat.run();\n        putLeftFork.run();\n        putRightFork.run();\n        release(n);\n    }\n\n    private void release(int n) {\n        Semaphore[] semaphores = getSemaphores(n);\n        semaphores[0].release();\n        semaphores[1].release();\n    }\n\n    // if the man can get both of the left / right focks\n    private boolean tryLock(int n) {\n        Semaphore[] semaphores = getSemaphores(n);\n        if (semaphores[0].tryAcquire()) {\n            if (semaphores[1].tryAcquire()) {\n                return true;            //he can get both, return true\n            } else {\n                semaphores[0].release();   // can only get one, give up the one he already got\n            }\n        }\n        return false;  // failed this time\n    }\n\n    private Semaphore[] getSemaphores(int n) {\n        return map.get(n);\n    }\n}\n```
[]