2wenty1ne 2024-10-22 16:41:53 +02:00
commit cc3050c940
3 changed files with 101 additions and 0 deletions

View File

@ -0,0 +1,3 @@
public class Chopstick {
}

View File

@ -0,0 +1,74 @@
import java.util.Random;
public class Philosopher extends Thread {
static final int MAX_THINKING_DURATION_MS = 1000;
static final int MAX_EATING_DURATION_MS = 3000;
static final int MAX_TAKING_TIME_MS = 1000;
private final Chopstick left;
private final Chopstick right;
private final Random random;
private int eaten;
private final int seat;
private volatile boolean stop;
private void log(String message) {
synchronized (Philosopher.class) {
for (var i = 1; i <= this.seat; i++) {
System.out.print(" ");
}
System.out.println(threadId() + " " + message);
}
}
public void stopPhilosopher() {
log("stopping");
this.stop = true;
interrupt();
}
public Philosopher(int seat, Chopstick left, Chopstick right) {
this.stop = false;
this.seat = seat;
this.left = left;
this.right = right;
this.random = new Random();
this.eaten = 0;
}
@Override
public void run() {
log("starting");
try {
while (!this.stop) {
think();
eat();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
log("stopped; eaten=" + this.eaten);
}
private void think() throws InterruptedException {
Thread.sleep(this.random.nextInt(MAX_THINKING_DURATION_MS));
}
private void eat() throws InterruptedException {
log("try taking left");
synchronized (this.left) {
log("left acquired");
Thread.sleep(this.random.nextInt(MAX_TAKING_TIME_MS));
log("try taking right");
synchronized (this.right) {
log("right acquired");
log("eating");
this.eaten++;
Thread.sleep(this.random.nextInt(MAX_EATING_DURATION_MS));
}
log("right released");
}
log("left released");
}
}

View File

@ -0,0 +1,24 @@
public class PhilosopherExperiment {
static final int PHILOSOPHER_NUM = 3;
static final int EXP_DURATION_MS = 20000;
public static void main(String... args) throws InterruptedException {
var chopsticks = new Chopstick[PHILOSOPHER_NUM];
var philosophers = new Philosopher[PHILOSOPHER_NUM];
for (var i = 0; i < PHILOSOPHER_NUM; i++) {
chopsticks[i] = new Chopstick();
}
for (var i = 0; i < PHILOSOPHER_NUM; i++) {
philosophers[i] = new Philosopher(i, chopsticks[i],
chopsticks[(i + 1) % PHILOSOPHER_NUM]);
}
for (var i = 0; i < PHILOSOPHER_NUM; i++) {
philosophers[i].start();
}
Thread.sleep(EXP_DURATION_MS);
for (var i = 0; i < PHILOSOPHER_NUM; i++) {
philosophers[i].stopPhilosopher();
}
}
}