diff --git a/pp.A1-CondPhilosophers/src/main/java/pp/Philosopher.java b/pp.A1-CondPhilosophers/src/main/java/pp/Philosopher.java index 2b01cc4..d470137 100644 --- a/pp.A1-CondPhilosophers/src/main/java/pp/Philosopher.java +++ b/pp.A1-CondPhilosophers/src/main/java/pp/Philosopher.java @@ -26,7 +26,7 @@ public class Philosopher extends Thread implements IPhilosopher { @Override public void run() { try { - while (this.stopped == false) { + while (!this.stopped) { think(); eat(); } @@ -35,7 +35,7 @@ public class Philosopher extends Thread implements IPhilosopher { } private void eat() throws InterruptedException { - table.lock(); + this.table.lock(); try { while (this.links.eating || this.rechts.eating) { @@ -49,17 +49,22 @@ public class Philosopher extends Thread implements IPhilosopher { Thread.sleep(this.random.nextInt(PhilosopherExperiment.MAX_EATING_DURATION_MS)); } - private void think() { - table.lock(); - this.links.canEat.signal(); - this.rechts.canEat.signal(); + private void think() throws InterruptedException { + this.table.lock(); + table.unlock(); try { - Thread.sleep(random.nextInt(PhilosopherExperiment.MAX_THINKING_DURATION_MS)); - log(seat, " : denkt gerade !"); - } catch (InterruptedException e) { - e.printStackTrace(); + if (this.eating) + this.eating = false; + this.links.canEat.signal(); + this.rechts.canEat.signal(); + + } finally { + this.table.unlock(); } + + Thread.sleep(random.nextInt(PhilosopherExperiment.MAX_THINKING_DURATION_MS)); + } @Override