Java with lock per for
The idea is to take locks in forks. Each fork = lock.\nAt the same time a philosopher can it only if two locks are locked (two forks in hands).\n\n\n    private
The idea is to take locks in forks. Each fork = lock.\nAt the same time a philosopher can it only if two locks are locked (two forks in hands).\n\n```\n    private static final int philosophers_count = 5;\n    private final List<Lock> locks = new ArrayList<>();\n\n    public DiningPhilosophers() {\n        for (int i=0; i<philosophers_count; i++) {\n            Lock lock = new ReentrantLock(true);\n            locks.add(lock);\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        while (true) {\n        if (left(philosopher).tryLock()) {\n            try {\n                if (right(philosopher).tryLock()) {\n                    try {\n                        pickLeftFork.run();\n                        pickRightFork.run();\n\n                        eat.run();\n\n                        putLeftFork.run();\n                        putRightFork.run();\n                        return;\n                    } finally {\n                        right(philosopher).unlock();\n                    }\n                }\n            } finally {\n                left(philosopher).unlock();\n            }\n        }\n\t\t// add random delay for the next try to take 2 locks\n\t\t// without random delay each philosopher can take righ fork (first lock) fail for the left fork (second lock) and putdown first forever\n        if (ThreadLocalRandom.current().nextBoolean()) {\n            try { Thread.sleep(1); } catch (InterruptedException e) { }\n        }\n        }\n    }\n    \n    private Lock left(int n) {\n        int next = (n + 1) % locks.size();\n        return locks.get(next);\n    }\n    private Lock right(int n) {\n        return locks.get(n);\n    }\n```
[]