Java - Solve dining-philosophers problem using the explicit lock constructs.

Introduction

Five philosophers spend all of their time either thinking or eating.

They sit around a circular table with five chairs and five forks.

There are only five forks and all five philosophers need to pick the two nearest one from his left and one from his right forks to eat.

Once a philosopher finishes eating, he puts down both forks and starts thinking.

A philosopher cannot pick up a fork if his neighbor is using it.

The following code models the philosophers assuming that an object of the ReentrantLock class represents a fork.

Demo

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Main{
  public static void main(String[] argv) {
    Lock fork1 = new ReentrantLock();
    Lock fork2 = new ReentrantLock();
    Lock fork3 = new ReentrantLock();
    Lock fork4 = new ReentrantLock();
    Lock fork5 = new ReentrantLock();
    /*from  ww w  .j  a v a  2s.c om*/
    Philosopher p1 = new Philosopher(fork1, fork2, "P1");
    Philosopher p2 = new Philosopher(fork2, fork3, "P2");
    Philosopher p3 = new Philosopher(fork3, fork4, "P3");
    Philosopher p4 = new Philosopher(fork4, fork5, "P4");
    Philosopher p5 = new Philosopher(fork5, fork1, "P5");
    
    p1.eat();
    p1.think();
    
    p2.eat();
    p2.think();
    
    p3.eat();
    p3.think();
    
    p4.eat();
    p4.think();

    p5.eat();
    p5.think();
    
    
    p1.eat();
    p2.eat();
    p3.eat();
    p4.eat();
    p5.eat();
 
    p1.think();
    p2.think();
    p3.think();
    p4.think();
    p5.think();

  }
} 
class Philosopher {
  private Lock leftFork;
  private Lock rightFork;
  private String name; // Philosopher's name

  public Philosopher(Lock leftFork, Lock rightFork, String name) {
    this.leftFork = leftFork;
    this.rightFork = rightFork;
    this.name = name;
  }

  public void think() {
    System.out.println(name + " is thinking...");
  }

  public void eat() {
    // Try to get the left fork
    if (leftFork.tryLock()) {
      try {
        // try to get the right fork
        if (rightFork.tryLock()) {
          try {
            try {
            // Got both forks. Eat now
            Thread.sleep(1000);
            System.out.println(name + " is eating...");
       
              Thread.sleep(1000);
            } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }
          } finally {
            // release the right fork
            rightFork.unlock();
          }
        }
      } finally {
        // release the left fork
        leftFork.unlock();
      }
    }
  }
}

Result

Related Topic