From 82f41974d11f9305f6b1e28c4f848daf05d34921 Mon Sep 17 00:00:00 2001 From: Shahnam Javidnia <3015418@stud.hs-mannheim.de> Date: Sat, 3 May 2025 20:56:09 +0200 Subject: [PATCH] dont need comment anymore adding volatile --- .../src/main/java/pp/Philosopher.java | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/pp.A1-CondPhilosophers/src/main/java/pp/Philosopher.java b/pp.A1-CondPhilosophers/src/main/java/pp/Philosopher.java index eb28836..c1514c1 100644 --- a/pp.A1-CondPhilosophers/src/main/java/pp/Philosopher.java +++ b/pp.A1-CondPhilosophers/src/main/java/pp/Philosopher.java @@ -11,13 +11,14 @@ public class Philosopher extends Thread implements IPhilosopher { private Philosopher right; private Lock table; private Condition canEat; - private boolean eating = false; - private boolean stopped = false; + private volatile boolean eating = false; + private volatile boolean stopped = false; @Override public void setLeft(IPhilosopher left) { // TODO Auto-generated method stub // Cast auf Philosopher erforderlich + if (left == null) throw new IllegalArgumentException("Left philosopher must not be null."); this.left = (Philosopher) left; } @@ -25,6 +26,7 @@ public class Philosopher extends Thread implements IPhilosopher { public void setRight(IPhilosopher right) { // TODO Auto-generated method stub // Cast auf Philosopher erforderlich + if (right == null) throw new IllegalArgumentException("Right philosopher must not be null."); this.right = (Philosopher) right; } @@ -50,22 +52,20 @@ public class Philosopher extends Thread implements IPhilosopher { } - // Hauptlogik des Philosophen: Der Philosoph denkt, versucht zu essen, isst und beendet das Essen. public void run() { try { while (!stopped) { - think(); // denkt eine Weile - beginEating(); // wartet, falls nötig, bis beide Nachbarn nicht essen - eat(); // isst eine zufällige Zeit - endEating(); // beendet das Essen und signalisiert Nachbarn + think(); + beginEating(); + eat(); + endEating(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } - // Diese Methode wird aufgerufen, wenn der Philosoph mit dem Essen fertig ist. - // Sie setzt seinen Status zurück und signalisiert den Nachbarn, dass sie es nun versuchen können. + private void endEating() { table.lock(); try { @@ -78,14 +78,11 @@ public class Philosopher extends Thread implements IPhilosopher { } } - // Diese Methode simuliert das tatsächliche Essen mit einer zufälligen Dauer. private void eat() throws InterruptedException { Thread.sleep((long) (Math.random() * MAX_EATING_DURATION_MS)); } - // Diese Methode prüft, ob einer der Nachbarn gerade isst. - // Wenn ja, wartet dieser Philosoph auf seine Bedingung. - // Wenn beide frei sind, beginnt er zu essen. + private void beginEating() throws InterruptedException { table.lock(); try { @@ -101,8 +98,7 @@ public class Philosopher extends Thread implements IPhilosopher { } - // Der Philosoph denkt für eine gewisse Zeit (zufällig), danach signalisiert er den Nachbarn, - // dass er nun fertig ist mit Denken, was eventuell anderen hilft zu essen. + private void think() throws InterruptedException { log(seat, "denkt"); Thread.sleep((long) (Math.random() * MAX_THINKING_DURATION_MS)); @@ -115,8 +111,6 @@ public class Philosopher extends Thread implements IPhilosopher { } } - // Diese Methode wird von anderen Philosophen aufgerufen, - // um diesen Philosophen zu wecken (z.B nach Ende einer Essensphase). public void signal() { table.lock(); try {