parent
bcb6d6f045
commit
82f41974d1
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue