Home » Core Java » threads » Dinning Philosophers deadlock example

About Ilias Tsagklis

Avatar photo
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

Dinning Philosophers deadlock example

This is an example of the Dining Philosophers’ problem. A short description of the problem shows that there are N philosphers sitting around a circular table eating and discussing philosphy. The problem is that each philosopher needs 2 forks to eat, and there are only N forks, one between each 2 philosophers. An algorithm is needed for the philosophers to follow that will ensure that none starves as long as each philosopher eventually stops eating, and such that the maximum number of philosophers can eat at once. The example consists of the following classes:

  • The class Spoon represents the spoons that a Philosopher needs in order to eat.
  • The class Philosopher extends the Thread, and overrides its run() method and its toString() method. It consists of two methods, think() and eat(). In the think() method, the thread sleeps for a random number of milliseconds. In its eat() method the thread holds in a synchronized statement the left Spoon first and then the second Spoon that represent the left and the right spoon of the Philosopher. In its constructor the thread gets the two Spoon objects that represent the left and the right spoon and calls its start() method to begin execution. In its overriden run() method the thread keeps calling think() and eat() methods forever.
  • The Timeout class extends the Timer class and overrides its run() method. It sets its super Timer(boolean isDaemon) true, so that the associated thread should run as a daemon. It calls its schedule(TimerTask task, long delay) method with a new TimerTask and a specified delay. Is is used in the example in order to stop the running Java Virtual Machine after a specified time.
  • In order to run the example, we create a Philosopher array. We create the two Spoon objects, that are the left and right spoon. For each one of the philosopher array positions we create a new Philosopher to put into the array and we give the Philosopher a left and a right spoon. Then the right spoon is set to the left and a new right Spoon is created for the next Philosopher. In order to avoid the deadlock we use the static boolean gotoDeadLock. When set to true a new Philosopher is created and gets the left and first spoon, that have values set above and in the while statement.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

class Spoon {

    private static int cnt = 0;
    private int num = cnt++;

    public String toString() {

  return "Spoon " + num;

class Philosopher extends Thread {

    private static Random rnd = new Random();
    private static int cnt = 0;
    private int num = cnt++;
    private Spoon leftSpoon;
    private Spoon rightSpoon;
    static int waiting = 0;

    public Philosopher(Spoon left, Spoon right) {

  leftSpoon = left;

  rightSpoon = right;


    public void think() {

  System.out.println(this + " is thinking");

  if (waiting > 0) {

try {


} catch (InterruptedException e) {

    throw new RuntimeException(e);



    public void eat() {

  synchronized (leftSpoon) {

System.out.println(this + " has " + this.leftSpoon + " Waiting for " + this.rightSpoon);

synchronized (rightSpoon) {

    System.out.println(this + " eating");



    public String toString() {

  return "Philosopher " + num;

    public void run() {

  while (true) {




public class DiningPhilosophers {
    private static boolean gotoDeadLock =true;

    public static void main(String[] args) {

  Philosopher[] phil = new Philosopher[10];

  Philosopher.waiting = 8;

  Spoon left = new Spoon(), right = new Spoon(), first = left;

  int i = 0;

  while (i < phil.length - 1) {

phil[i++] = new Philosopher(left, right);

left = right;

right = new Spoon();



  if (gotoDeadLock) {

phil[i] = new Philosopher(left, first);

  } else 


phil[i] = new Philosopher(first, left);


  if (args.length >= 4) {

int delay = 3;

if (delay != 0) {

    Timeout timeout = new Timeout(delay * 1000, "Timed out");



class Timeout extends Timer {

    public Timeout(int delay, final String msg) {


  schedule(new TimerTask() {


public void run() {




  }, delay);

This was an example of the Dining Philosophers’ problem in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments