The dining philosophers problem Solution in java

Hasitha Chandula
4 min readMay 4, 2019

Problem

The dining philosophers problem states that there are 5 philosophers sharing a circular table and they eat and think alternatively. There is a bowl of rice for each of the philosophers and 5 chopsticks. A philosopher needs both their right and a left chopstick to eat. A hungry philosopher may only eat if there are both chopsticks available. Otherwise, a philosopher puts down their chopstick and begin thinking again.

Solution

My implemented solution is, First I create static int variable to set how many philosophers in our case (in my case i set it to 5). Then I created 2 arrays one for philosophers and other for chopsticks. The size of these two arrays set to the values of philosophers variable.

static int philosopher = 5;
static philosopher philosophers[] = new philosopher[philosopher];
static chopstick chopsticks[] = new chopstick[philosopher];

Then i create chopstick class and this class has 3 methods, which are grab() , release() and isFree().

static class chopstick {

public Semaphore mutex = new Semaphore(1);

void grab() {
try {
mutex.acquire();
}
catch (Exception e) {
e.printStackTrace(System.out);
}
}

void release() {
mutex.release();
}

boolean isFree() {
return mutex.availablePermits() > 0;
}

}

After that i create philosopher class which extends the Thread class in java. In this class i create 3 variables, constructor and two methods. one Method is run and it try to eat if one philosophers has both left and right chopsticks. otherwise the philosopher left the chopstick. To check that i used while loop. And the other method is eat in this method check if one philosopher eat then print the philosopher number and eat time. To display eat time I used ThreadLocalRandom.current().nextInt(0, 1000); and set parameters to 0 and 1000. This is my philosopher class,

static class philosopher extends Thread {

public int number;
public chopstick leftchopstick;
public chopstick rightchopstick;

philosopher(int num, chopstick left, chopstick right) {
number = num;
leftchopstick = left;
rightchopstick = right;
}

public void run(){

while (true) {
leftchopstick.grab();
System.out.println("philosopher " + (number+1) + " grabs left chopstick.");
rightchopstick.grab();
System.out.println("philosopher " + (number+1) + " grabs right chopstick.");
eat();
leftchopstick.release();
System.out.println("philosopher " + (number+1) + " releases left chopstick.");
rightchopstick.release();
System.out.println("philosopher " + (number+1) + " releases right chopstick.");
}
}

void eat() {
try {
int sleepTime = ThreadLocalRandom.current().nextInt(0, 1000);
System.out.println("philosopher " + (number+1) + " eats for " + sleepTime);
Thread.sleep(sleepTime);
}
catch (Exception e) {
e.printStackTrace(System.out);
}
}

}

And in the main Method I write this code

public static void main(String argv[]) {

for (int i = 0; i < philosopher; i++) {
chopsticks[i] = new chopstick();
}

for (int i = 0; i < philosopher; i++) {
philosophers[i] = new philosopher(i, chopsticks[i], chopsticks[(i + 1) % philosopher]);
philosophers[i].start();
}

while (true) {
try {
// sleep 1 sec
Thread.sleep(1000);

// check for deadlock
boolean deadlock = true;
for (chopstick f : chopsticks) {
if (f.isFree()) {
deadlock = false;
break;
}
}
if (deadlock) {
Thread.sleep(1000);
System.out.println("Everyone Eats");
break;
}
}
catch (Exception e) {
e.printStackTrace(System.out);
}
}

System.out.println("Exit The Program!");
System.exit(0);
}

In my code we can set and number of philosophers (but chopsticks and philosophers must be same) and one all philosophers are eat and any philosopher’s eat time more than 1000ms ( 1s ) my program is exit. We can set eat time 1000ms or any time I wish by editing the code.

Full Code

package com.company;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadLocalRandom;

public class Main {

static int philosopher = 5;
static philosopher philosophers[] = new philosopher[philosopher];
static chopstick chopsticks[] = new chopstick[philosopher];

static class chopstick {

public Semaphore mutex = new Semaphore(1);

void grab() {
try {
mutex.acquire();
}
catch (Exception e) {
e.printStackTrace(System.out);
}
}

void release() {
mutex.release();
}

boolean isFree() {
return mutex.availablePermits() > 0;
}

}

static class philosopher extends Thread {

public int number;
public chopstick leftchopstick;
public chopstick rightchopstick;

philosopher(int num, chopstick left, chopstick right) {
number = num;
leftchopstick = left;
rightchopstick = right;
}

public void run(){

while (true) {
leftchopstick.grab();
System.out.println("philosopher " + (number+1) + " grabs left chopstick.");
rightchopstick.grab();
System.out.println("philosopher " + (number+1) + " grabs right chopstick.");
eat();
leftchopstick.release();
System.out.println("philosopher " + (number+1) + " releases left chopstick.");
rightchopstick.release();
System.out.println("philosopher " + (number+1) + " releases right chopstick.");
}
}

void eat() {
try {
int sleepTime = ThreadLocalRandom.current().nextInt(0, 1000);
System.out.println("philosopher " + (number+1) + " eats for " + sleepTime);
Thread.sleep(sleepTime);
}
catch (Exception e) {
e.printStackTrace(System.out);
}
}

}

public static void main(String argv[]) {

for (int i = 0; i < philosopher; i++) {
chopsticks[i] = new chopstick();
}

for (int i = 0; i < philosopher; i++) {
philosophers[i] = new philosopher(i, chopsticks[i], chopsticks[(i + 1) % philosopher]);
philosophers[i].start();
}

while (true) {
try {
// sleep 1 sec
Thread.sleep(1000);

// check for deadlock
boolean deadlock = true;
for (chopstick f : chopsticks) {
if (f.isFree()) {
deadlock = false;
break;
}
}
if (deadlock) {
Thread.sleep(1000);
System.out.println("Everyone Eats");
break;
}
}
catch (Exception e) {
e.printStackTrace(System.out);
}
}

System.out.println("Exit The Program!");
System.exit(0);
}

}

--

--

Hasitha Chandula

Senior Full Stack Engineer specializing in TypeScript & Go. I create scalable web apps, love learning new tech, and thrive on teamwork.