Aufgabe 01

main
Yan Wittmann 2022-10-23 12:03:58 +02:00
parent 525a45d687
commit fa520c9ac1
2 changed files with 36 additions and 23 deletions

View File

@ -28,8 +28,8 @@ public class Philosopher extends Thread implements IPhilosopher {
private Lock table; private Lock table;
private Condition condition; private Condition condition;
private boolean stop = false; private volatile boolean stop = false;
private boolean isEating = false; private volatile boolean isEating = false;
private void log(String message) { private void log(String message) {
synchronized (Philosopher.class) { synchronized (Philosopher.class) {
@ -40,6 +40,15 @@ public class Philosopher extends Thread implements IPhilosopher {
} }
} }
private void err(String message) {
synchronized (Philosopher.class) {
for (var i = 1; i <= this.seat; i++) {
System.err.print(" ".repeat(40));
}
System.err.println(message);
}
}
@Override @Override
public void setLeft(IPhilosopher left) { public void setLeft(IPhilosopher left) {
this.left = (Philosopher) left; this.left = (Philosopher) left;
@ -70,36 +79,40 @@ public class Philosopher extends Thread implements IPhilosopher {
@Override @Override
public void run() { public void run() {
while (!this.stop) { while (!this.stop) {
log("Philosopher " + seat + " is thinking");
this.sleepRandomDuration(PhilosopherExperiment.MAX_THINKING_DURATION_MS); this.sleepRandomDuration(PhilosopherExperiment.MAX_THINKING_DURATION_MS);
log("Philosopher " + seat + " is done thinking");
try {
this.table.lock(); this.table.lock();
try {
while (this.left.isEating || this.right.isEating) { while (this.left.isEating || this.right.isEating) {
log("Philosopher " + seat + " is waiting for his neighbors to finish eating"); log("Philosopher " + seat + " is waiting");
this.left.condition.await(); this.condition.await();
this.right.condition.await(); log("Philosopher " + seat + " is done waiting");
} }
this.isEating = true;
log("Philosopher " + seat + " is taking chopsticks"); log("Philosopher " + seat + " is taking chopsticks");
this.sleepRandomDuration(PhilosopherExperiment.MAX_TAKING_TIME_MS); this.sleepRandomDuration(PhilosopherExperiment.MAX_TAKING_TIME_MS);
log("Philosopher " + seat + " is eating"); log("Philosopher " + seat + " took chopsticks");
this.isEating = true;
} catch (InterruptedException e) {
this.err("Philosopher " + seat + " was interrupted while waiting for the table");
} finally {
this.table.unlock();
}
log("Philosopher " + seat + " is eating");
this.sleepRandomDuration(PhilosopherExperiment.MAX_EATING_DURATION_MS); this.sleepRandomDuration(PhilosopherExperiment.MAX_EATING_DURATION_MS);
log("Philosopher " + seat + " is done eating"); log("Philosopher " + seat + " is done eating");
this.table.lock();
this.isEating = false; this.isEating = false;
this.left.condition.signalAll();
} catch (InterruptedException e) { this.right.condition.signalAll();
log("Philosopher " + seat + " was interrupted"); log("Philosopher " + seat + " is signaling");
throw new RuntimeException(e);
} finally {
this.condition.signalAll();
this.table.unlock(); this.table.unlock();
} }
}
log("Philosopher " + seat + " has stopped"); log("Philosopher " + seat + " has stopped");
} }

View File

@ -4,11 +4,11 @@ import java.util.concurrent.locks.ReentrantLock;
public class PhilosopherExperiment { public class PhilosopherExperiment {
static final int MAX_THINKING_DURATION_MS = 3000; // 3000 public static final int MAX_THINKING_DURATION_MS = 3000; // 3000
static final int MAX_EATING_DURATION_MS = 3000; // 3000 public static final int MAX_EATING_DURATION_MS = 3000; // 3000
static final int MAX_TAKING_TIME_MS = 100; // 100 public static final int MAX_TAKING_TIME_MS = 100; // 100
static final int PHILOSOPHER_NUM = 5; public static final int PHILOSOPHER_NUM = 5;
static final int EXP_DURATION_MS = 20000; public static final int EXP_DURATION_MS = 20000;
static IPhilosopher[] philosophers = new Philosopher[PHILOSOPHER_NUM]; static IPhilosopher[] philosophers = new Philosopher[PHILOSOPHER_NUM];