Erste Version, unterstützt von Claude
parent
97a3a4da9d
commit
f5ff3d11f1
|
|
@ -4,9 +4,9 @@ import java.util.concurrent.locks.Condition;
|
|||
import java.util.concurrent.locks.Lock;
|
||||
|
||||
public class Philosopher extends Thread implements IPhilosopher {
|
||||
static final int MAX_THINKING_DURATION_MS = 1000;
|
||||
static final int MAX_EATING_DURATION_MS = 3000;
|
||||
static final int MAX_TAKING_TIME_MS = 1000;
|
||||
static final int MAX_THINKING_DURATION_MS = 1000; //aus Vorlesungsbeispiel
|
||||
static final int MAX_EATING_DURATION_MS = 3000; //aus Vorlesungsbeispiel
|
||||
boolean stop = false;
|
||||
private int seat;
|
||||
private Lock table;
|
||||
private Condition cond;
|
||||
|
|
@ -14,8 +14,6 @@ public class Philosopher extends Thread implements IPhilosopher {
|
|||
private Philosopher right;
|
||||
private boolean essen;
|
||||
private int gegessen = 0;
|
||||
boolean stop = false;
|
||||
|
||||
|
||||
@Override
|
||||
public void setLeft(IPhilosopher left) {
|
||||
|
|
@ -49,32 +47,28 @@ public class Philosopher extends Thread implements IPhilosopher {
|
|||
log(seat, "stopping");
|
||||
this.stop = true;
|
||||
interrupt();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
while (!stop) {
|
||||
log(seat, "überlegen");
|
||||
Thread.sleep(MAX_THINKING_DURATION_MS);
|
||||
try {
|
||||
startEating();
|
||||
|
||||
log(seat, "essen");
|
||||
Thread.sleep(MAX_EATING_DURATION_MS);
|
||||
gegessen = gegessen +1;
|
||||
log(seat, "gegessen "+gegessen);
|
||||
}
|
||||
finally {
|
||||
while (!stop) {
|
||||
log(seat, "überlegen");
|
||||
Thread.sleep(MAX_THINKING_DURATION_MS);
|
||||
try {
|
||||
startEating();
|
||||
log(seat, "essen");
|
||||
Thread.sleep(MAX_EATING_DURATION_MS);
|
||||
gegessen = gegessen + 1;
|
||||
log(seat, "gegessen " + gegessen);
|
||||
} finally {
|
||||
stopEating();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(InterruptedException e){
|
||||
} catch (InterruptedException e) {
|
||||
//Hier ist eine Exception gefunden worden
|
||||
System.out.println("Hier ist ein Thread unterbrochen worden");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void startEating() throws InterruptedException {
|
||||
|
|
@ -92,7 +86,6 @@ public class Philosopher extends Thread implements IPhilosopher {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
private void stopEating() throws InterruptedException {
|
||||
table.lock();
|
||||
try {
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Reference in New Issue