Java - easy to understand - with 5 locks
java\nclass DiningPhilosophers {\n\n    final int NUM_PHILOSOPHERS = 5;\n\n    public class Fork extends ReentrantLock{};\n    Fork[] forks;\n\n    public Dinin
```java\nclass DiningPhilosophers {\n\n    final int NUM_PHILOSOPHERS = 5;\n\n    public class Fork extends ReentrantLock{};\n    Fork[] forks;\n\n    public DiningPhilosophers() {\n        forks = new Fork[NUM_PHILOSOPHERS];\n        Arrays.fill(forks, new Fork());\n    }\n\n    /* Get index of fork to the left of the philospher */\n    int left(int philosopher) {\n        return philosopher;\n    }\n\n    /* Get index of fork to the right of the philospher */\n    int right(int philosopher) {\n        if(philosopher==0) return NUM_PHILOSOPHERS-1;\n        return philosopher-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        while(true) {\n\t\t    //Pick left, followed by pick right.\n            tryPickLeft(philosopher, pickLeftFork); \n            if(!tryPickRight(philosopher, pickRightFork, putLeftFork)) continue;\n\n            eat.run();\n\n\t\t\t// put down left fork\n            putLeftFork.run();\n            forks[left(philosopher)].unlock();\n\t\t\t\n\t\t\t// put down right fork\n            putRightFork.run();\n            forks[right(philosopher)].unlock();\n\t\t\t\n            break; // break true while loop\n        }\n    }\n\n    public void tryPickLeft(int philosopher, Runnable pickLeftFork) {\n        forks[left(philosopher)].lock();\n        pickLeftFork.run();\n    }\n\n    public boolean tryPickRight(int philosopher, Runnable pickRightFork, Runnable putLeftFork) {\n        boolean successfulRight = forks[right(philosopher)].tryLock();\n        if(!successfulRight) {\n            /* Could not pick right fork. Put down left fork as well, and release lock on left fork */\n            putLeftFork.run();\n            forks[left(philosopher)].unlock();\n            return false;\n        }\n        pickRightFork.run();\n        return true;\n    }\n}\n```
[]