[Java] 1226. The Dining Philosophers, simple Java solution with two locks, includes test code
[Java] 1226. The Dining Philosophers, simple Java solution with two locks, includes test code.\nThe solution is not the fastest, I got 25% faster than other sub
[Java] 1226. The Dining Philosophers, simple Java solution with two locks, includes test code.\nThe solution is not the fastest, I got 25% faster than other submissions.\n\n```\nimport java.util.concurrent.TimeUnit;\nimport java.util.concurrent.locks.Lock;\nimport java.util.concurrent.locks.ReentrantLock;\n\npublic class DiningPhilosophers {\n\n    private Lock leftForkLock = new ReentrantLock();\n    private Lock rightForkLock = new ReentrantLock();\n\n    public DiningPhilosophers() {\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\n        while(true) {\n            if(leftForkLock.tryLock(100, TimeUnit.MILLISECONDS)) {\n                try {\n                    pickLeftFork.run();\n\n                    if (rightForkLock.tryLock(100, TimeUnit.MILLISECONDS)) {\n                        try {\n                            pickRightFork.run();\n                            eat.run();\n                            putRightFork.run();\n\n                            /*\n                             * we have eaten\n                             */\n                            return;\n                        } finally {\n                            rightForkLock.unlock();\n                        }\n                    }\n\n\n                } finally {\n                    putLeftFork.run();\n                    leftForkLock.unlock();\n                }\n            }\n        }\n    }\n}\n```\n\nCode that I used for teting\n\n```\n    private static void testDiningPhilosophers() {\n\n        DiningPhilosophers diningPhilosophers = new DiningPhilosophers();\n\n        Runnable pickUpLeftFork = () -> {\n            System.out.println("picked up left fork by " + Thread.currentThread().getName());\n        };\n        Runnable pickUpRightFork = () -> {\n            System.out.println("picked up right fork by " + Thread.currentThread().getName());\n        };\n\n        Runnable eat = () -> {\n            System.out.println("eat by " + Thread.currentThread().getName());\n\n        };\n\n        Runnable putDownLeftFork = () -> {\n            System.out.println("put down left fork by " + Thread.currentThread().getName());\n        };\n\n        Runnable putDownRightFork = () -> {\n            System.out.println("put down right fork by " + Thread.currentThread().getName());\n        };\n\n        /*\n         *  number of attempts to eat\n         */\n        int n = 2;\n\n        Thread f0 = new Thread(() -> {\n            try {\n\n                for (int i = 0 ; i < n; i++) {\n                    diningPhilosophers.wantsToEat(0, pickUpLeftFork, pickUpRightFork, eat, putDownLeftFork, putDownRightFork);\n                }\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n        }, "filo-0");\n\n        Thread f1 = new Thread(() -> {\n            try {\n\n                for (int i = 0 ; i < n; i++) {\n                    diningPhilosophers.wantsToEat(1, pickUpLeftFork, pickUpRightFork, eat, putDownLeftFork, putDownRightFork);\n                }\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n        }, "filo-1");\n\n        Thread f2 = new Thread(() -> {\n            try {\n\n                for (int i = 0 ; i < n; i++) {\n                    diningPhilosophers.wantsToEat(2, pickUpLeftFork, pickUpRightFork, eat, putDownLeftFork, putDownRightFork);\n                }\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n        }, "filo-2");\n\n        Thread f3 = new Thread(() -> {\n            try {\n\n                for (int i = 0 ; i < n; i++) {\n                    diningPhilosophers.wantsToEat(3, pickUpLeftFork, pickUpRightFork, eat, putDownLeftFork, putDownRightFork);\n                }\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n        }, "filo-3");\n\n        Thread f4 = new Thread(() -> {\n            try {\n\n                for (int i = 0 ; i < n; i++) {\n                    diningPhilosophers.wantsToEat(0, pickUpLeftFork, pickUpRightFork, eat, putDownLeftFork, putDownRightFork);\n                }\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            }\n        }, "filo-4");\n\n\n\n        f0.start();\n        f1.start();\n        f2.start();\n        f3.start();\n        f4.start();\n    }\n```
['Java']