# Dining Philosophers in Java 8

DevFeed: [Dining Philosophers in Java 8](<https://devfeed.tech/articles/dining-philosophers-in-java-8-32218.md>)

Original publisher: [Read original article](<https://bruceeckel.com/2016/12/29/dining-philosophers-in-java-8/>)

Author: Bruce Eckel

Published: 2016-12-29T00:00:00Z

Content type: article

Language: en

Sources: [Bruce Eckel - Computing Thoughts](<https://devfeed.tech/sources/bruce-eckel-computing-thoughts.md>)

Topics: [Deadlock](<https://devfeed.tech/topics/deadlock.md>), [Concurrency](<https://devfeed.tech/topics/concurrency.md>), [Java](<https://devfeed.tech/topics/java.md>), [locking](<https://devfeed.tech/topics/locking.md>), [async](<https://devfeed.tech/topics/async.md>), [ExecutorService](<https://devfeed.tech/topics/executorservice.md>)

Tags: [arrays](<https://devfeed.tech/tags/arrays.md>), [chopsticks](<https://devfeed.tech/tags/chopsticks.md>), [class](<https://devfeed.tech/tags/class.md>), [collection](<https://devfeed.tech/tags/collection.md>), [collections](<https://devfeed.tech/tags/collections.md>), [completablefuture](<https://devfeed.tech/tags/completablefuture.md>), [concurrency](<https://devfeed.tech/tags/concurrency.md>), [concurrent](<https://devfeed.tech/tags/concurrent.md>), [constructor](<https://devfeed.tech/tags/constructor.md>), [deadlock](<https://devfeed.tech/tags/deadlock.md>), [deadlocks](<https://devfeed.tech/tags/deadlocks.md>), [debug](<https://devfeed.tech/tags/debug.md>), [dijkstra](<https://devfeed.tech/tags/dijkstra.md>), [executor](<https://devfeed.tech/tags/executor.md>), [foreach](<https://devfeed.tech/tags/foreach.md>), [forks](<https://devfeed.tech/tags/forks.md>), [java](<https://devfeed.tech/tags/java.md>), [java-8](<https://devfeed.tech/tags/java-8.md>), [tasks](<https://devfeed.tech/tags/tasks.md>)

## AI overview

The Java example shows how philosophers can deadlock when each takes a right chopstick and then waits for the left, creating circular wait. A `StickHolder` uses a size-one `BlockingQueue` to ensure only one philosopher can hold a chopstick at a time; philosophers block in `take()` until it is returned. The program may appear to run correctly before deadlocking, and adding thinking time or philosophers makes deadlock less likely without eliminating the underlying risk. The example breaks circular wait by reversing the chopstick order for one philosopher, so that philosopher takes the left stick first. The text notes that deadlock can also be prevented by breaking any of its four necessary conditions, and that avoiding shared resources is easiest when feasible.

## Source excerpt

Because tasks can become blocked, it's possible for one task to get stuck waiting for another task, which in turn waits for another task, and so on, until the chain leads back to a task waiting on the first one. You get a continuous loop of tasks waiting on each other, and no one can move. This is called deadlock.1 If you try running a program and it deadlocks right away, you can immediately track down the bug.