Java simple solution that works
Finally, easy one for a programmer! I was sick of DP problems and was so glad to bump into the task where I can finally put my daily skills in work.\n 1. try to
Finally, easy one for a programmer! I was sick of DP problems and was so glad to bump into the task where I can finally put my daily skills in work.\n 1. try to acquire locks for both left and right fork during N ms interval, if not - repeat (we are using time limited lock to avoid deadlocks)\n2. make N a slightly random to decrease a number of collisions\n```\nclass DiningPhilosophers {\n\n    private final Random rnd = new Random();    \n    private ReentrantLock[] forks = new ReentrantLock[5];\n    \n    public DiningPhilosophers() {\n        for(int i = 0; i < 5; i++) {\n            forks[i] = new ReentrantLock();\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        \n        ReentrantLock rightFork = forks[philosopher];\n        ReentrantLock leftFork = forks[(philosopher + 1) % 5];\n        \n        boolean lf = false, rf = false;\n        \n        do {\n            rf = rightFork.tryLock(rnd.nextInt(100)+50, TimeUnit.MILLISECONDS);\n            lf = leftFork.tryLock(rnd.nextInt(100)+50, TimeUnit.MILLISECONDS); \n            if(rf && !lf) {\n                rf = false;\n                rightFork.unlock();\n            }\n        } while(!(lf && rf));\n       \n        try{\n            pickRightFork.run();\n            pickLeftFork.run();\n\n            eat.run();\n            \n            putLeftFork.run();\n            putRightFork.run();\n        } finally {\n            rightFork.unlock();\n            leftFork.unlock();\n        }\n        \n    }\n}\n```
[]