parent
bcb6d6f045
commit
82f41974d1
|
@ -11,13 +11,14 @@ public class Philosopher extends Thread implements IPhilosopher {
|
||||||
private Philosopher right;
|
private Philosopher right;
|
||||||
private Lock table;
|
private Lock table;
|
||||||
private Condition canEat;
|
private Condition canEat;
|
||||||
private boolean eating = false;
|
private volatile boolean eating = false;
|
||||||
private boolean stopped = false;
|
private volatile boolean stopped = false;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setLeft(IPhilosopher left) {
|
public void setLeft(IPhilosopher left) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
// Cast auf Philosopher erforderlich
|
// Cast auf Philosopher erforderlich
|
||||||
|
if (left == null) throw new IllegalArgumentException("Left philosopher must not be null.");
|
||||||
this.left = (Philosopher) left;
|
this.left = (Philosopher) left;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,6 +26,7 @@ public class Philosopher extends Thread implements IPhilosopher {
|
||||||
public void setRight(IPhilosopher right) {
|
public void setRight(IPhilosopher right) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
// Cast auf Philosopher erforderlich
|
// Cast auf Philosopher erforderlich
|
||||||
|
if (right == null) throw new IllegalArgumentException("Right philosopher must not be null.");
|
||||||
this.right = (Philosopher) right;
|
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() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
while (!stopped) {
|
while (!stopped) {
|
||||||
think(); // denkt eine Weile
|
think();
|
||||||
beginEating(); // wartet, falls nötig, bis beide Nachbarn nicht essen
|
beginEating();
|
||||||
eat(); // isst eine zufällige Zeit
|
eat();
|
||||||
endEating(); // beendet das Essen und signalisiert Nachbarn
|
endEating();
|
||||||
}
|
}
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
Thread.currentThread().interrupt();
|
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() {
|
private void endEating() {
|
||||||
table.lock();
|
table.lock();
|
||||||
try {
|
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 {
|
private void eat() throws InterruptedException {
|
||||||
Thread.sleep((long) (Math.random() * MAX_EATING_DURATION_MS));
|
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 {
|
private void beginEating() throws InterruptedException {
|
||||||
table.lock();
|
table.lock();
|
||||||
try {
|
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 {
|
private void think() throws InterruptedException {
|
||||||
log(seat, "denkt");
|
log(seat, "denkt");
|
||||||
Thread.sleep((long) (Math.random() * MAX_THINKING_DURATION_MS));
|
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() {
|
public void signal() {
|
||||||
table.lock();
|
table.lock();
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in New Issue