Beats 100%
Approach\n1. All but one of the philosophers reach for right fork first followed by the left fork. Rest reach for the left fork first.  This prevents the deadlo
# Approach\n1. All but one of the philosophers reach for right fork first followed by the left fork. Rest reach for the left fork first.  This prevents the deadlock.\n\n2. Use re-entrant lock with a fairness policy to try to prevent starvation.\n\n\n# Code\n```\nclass DiningPhilosophers {\n\n    List<ReentrantLock> locks;\n    public DiningPhilosophers() {\n        locks = new ArrayList<>();\n        for (int i = 0; i < 5; i++) {\n            locks.add(new ReentrantLock(true));\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        Lock right = locks.get(philosopher);\n\n        int leftForkNum = (philosopher + 1) % 5;\n        Lock left = locks.get(leftForkNum);\n\n        Lock first = left;\n        Lock second = right;\n        Runnable pickFirst = pickLeftFork;\n        Runnable pickSecond = pickRightFork;\n\n        if (philosopher == 4) {\n            first = right;\n            second = left;\n            pickFirst = pickRightFork;\n            pickSecond = pickLeftFork;\n        }\n\n        while (true) {\n            first.lock();\n\n            if (!second.tryLock(1, TimeUnit.MILLISECONDS)) {\n                first.unlock();\n                continue;\n            }\n            pickFirst.run();\n            pickSecond.run();\n            eat.run();\n\n            putLeftFork.run();\n            putRightFork.run();\n\n            first.unlock();\n            second.unlock(); \n            break;\n        }\n              \n    }\n\n}\n```
['Java']