Atomics: take two forks at once
Unlike semaphore + mutex solution threads in  this solution acquire only one "lock". Thus there are no threads that are blocked waiting for a lock and hold some
Unlike [semaphore + mutex](https://leetcode.com/problems/the-dining-philosophers/discuss/407358/Semaphore-%2B-mutex) solution threads in  this solution acquire only one "lock". Thus there are no threads that are blocked waiting for a lock and hold some locks at the same time.\n```\nclass DiningPhilosophers\n{\n    private static final int spinLimit = 10;  \n    private AtomicInteger pickedForks = new AtomicInteger(0);\n    \n    public DiningPhilosophers()\n    {\n    }\n    \n    void pickForks(int mask)\n    {\n        int spinCounter = 0;\n        while (true) {\n            int forks = pickedForks.get() & ~mask;\n            if (pickedForks.weakCompareAndSet(forks, forks | mask)) break;\n            if (++spinCounter < spinLimit) continue;\n            spinCounter = 0;\n            Thread.yield();\n        }\n    }\n    \n    void putForks(int mask)\n    {\n        int spinCounter = 0;\n        while (true) {\n            int forks = pickedForks.get();\n            if (pickedForks.weakCompareAndSet(forks, forks & ~mask)) break;\n            if (++spinCounter < spinLimit) continue;\n            spinCounter = 0;\n            Thread.yield();\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        int mask = (3 << philosopher) % 0x1f;\n        \n        pickForks(mask);\n        \n        pickLeftFork.run();\n        pickRightFork.run();\n        eat.run();\n        putRightFork.run();\n        putLeftFork.run();\n        \n        putForks(mask);\n    }\n}\n```
[]