From cdbcf46df5f16d2efbf378a7727af09b801883dd Mon Sep 17 00:00:00 2001 From: obaya Date: Sun, 4 May 2025 22:15:15 +0200 Subject: [PATCH] Final Version --- .../src/main/java/pp/Philosopher.java | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/pp.A1-CondPhilosophers/src/main/java/pp/Philosopher.java b/pp.A1-CondPhilosophers/src/main/java/pp/Philosopher.java index 68dd21d..2b01cc4 100644 --- a/pp.A1-CondPhilosophers/src/main/java/pp/Philosopher.java +++ b/pp.A1-CondPhilosophers/src/main/java/pp/Philosopher.java @@ -10,7 +10,7 @@ public class Philosopher extends Thread implements IPhilosopher { private Philosopher rechts; private Philosopher links; private Lock table; - private Condition condition; + private Condition canEat; private volatile boolean stopped; private volatile boolean eating; private final Random random; @@ -21,10 +21,6 @@ public class Philosopher extends Thread implements IPhilosopher { this.stopped = false; } - public static void main(String... args) { - - // TODO - } @Override @@ -35,7 +31,6 @@ public class Philosopher extends Thread implements IPhilosopher { eat(); } } catch (InterruptedException e) { - // Thread was interrupted, exit gracefully } } @@ -45,9 +40,8 @@ public class Philosopher extends Thread implements IPhilosopher { while (this.links.eating || this.rechts.eating) { log(seat, " : isst gerade!"); - this.condition.await(); + this.canEat.await(); } - this.eating = true; } finally { table.unlock(); @@ -57,8 +51,8 @@ public class Philosopher extends Thread implements IPhilosopher { private void think() { table.lock(); - this.links.condition.signal(); - this.rechts.condition.signal(); + this.links.canEat.signal(); + this.rechts.canEat.signal(); table.unlock(); try { Thread.sleep(random.nextInt(PhilosopherExperiment.MAX_THINKING_DURATION_MS)); @@ -91,13 +85,12 @@ public class Philosopher extends Thread implements IPhilosopher { public void setTable(Lock table) { // TODO Auto-generated method stub this.table = table; - condition = this.table.newCondition(); - + canEat = this.table.newCondition(); + } @Override public void stopPhilosopher() { - this.stopped = true; interrupt(); }