Created
February 23, 2022 21:32
-
-
Save sgtcortez/1a8c1c74ea9ab254ac9c18c1fb594c6d to your computer and use it in GitHub Desktop.
Dining Philosophers problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.Semaphore; | |
public class Resolution { | |
private static final int EAT_TIME = 3 * 1000; | |
private static final int THINK_TIME = 2 * 1000; | |
private static final int WAIT_TIME = 1 * 1000; | |
private static final int ITERATIONS_NUMBER = 1000; | |
private final Semaphore[] semaphores; | |
private final Philosophe[] philosophes; | |
public Resolution(final int forks) { | |
semaphores = new Semaphore[forks]; | |
philosophes = new Philosophe[forks]; | |
for (int index = 0; index < forks; index++) { | |
semaphores[index] = new Semaphore(1); | |
philosophes[index] = new Philosophe(index); | |
philosophes[index].start(); | |
} | |
} | |
private int getLeftFork(int philosopheIndex) { | |
return philosopheIndex == 0 ? philosophes.length - 1 : philosopheIndex - 1; | |
} | |
private void waitForForks(int philosopheIndex) { | |
semaphores[getLeftFork(philosopheIndex)].acquireUninterruptibly(); | |
semaphores[philosopheIndex].acquireUninterruptibly(); | |
} | |
private void releaseForks(final int philosopheIndex) { | |
semaphores[getLeftFork(philosopheIndex)].release(); | |
semaphores[philosopheIndex].release(); | |
} | |
private void hungry(final int philosophe) { | |
final Philosophe current = philosophes[philosophe]; | |
current.state = PhilosopheState.WAITING; | |
System.out.printf("Philosophe: %s is hungry ...\n", current.getName()); | |
safeSleep(WAIT_TIME); | |
} | |
private void eat(final int philosophe) { | |
final Philosophe current = philosophes[philosophe]; | |
waitForForks(philosophe); | |
current.state = PhilosopheState.EATING; | |
current.eatTimes++; | |
System.out.printf("Philosophe: %s is eating ...\t Already ate %d times.\n", current.getName(), current.eatTimes); | |
safeSleep(EAT_TIME); | |
releaseForks(philosophe); | |
} | |
private void think(final int philosophe) { | |
final Philosophe current = philosophes[philosophe]; | |
current.state = PhilosopheState.THINKING; | |
System.out.printf("Philosophe: %s is thinking ...\n", current.getName()); | |
safeSleep(THINK_TIME); | |
} | |
enum PhilosopheState { | |
EATING, WAITING, THINKING | |
} | |
private void safeSleep(int time) { | |
try { | |
Thread.sleep(time); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
class Philosophe extends Thread { | |
private PhilosopheState state; | |
private final int id; | |
private int eatTimes; | |
public Philosophe(final int id) { | |
this.id = id; | |
eatTimes = 0; | |
} | |
@Override | |
public void run() { | |
for (int index = 0; index < ITERATIONS_NUMBER; index++) { | |
think(id); | |
hungry(id); | |
eat(id); | |
} | |
System.out.printf("Philosophe: %s is done ...\n", getName()); | |
} | |
} | |
public static void main(String[] args) { | |
final int philosophersNumber = 5; | |
final Resolution resolution = new Resolution(philosophersNumber); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment