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;
|
import java.util.concurrent.locks.Lock;
|
||||||
|
|
||||||
public class Philosopher extends Thread implements IPhilosopher {
|
public class Philosopher extends Thread implements IPhilosopher {
|
||||||
static final int MAX_THINKING_DURATION_MS = 1000;
|
static final int MAX_THINKING_DURATION_MS = 1000; //aus Vorlesungsbeispiel
|
||||||
static final int MAX_EATING_DURATION_MS = 3000;
|
static final int MAX_EATING_DURATION_MS = 3000; //aus Vorlesungsbeispiel
|
||||||
static final int MAX_TAKING_TIME_MS = 1000;
|
boolean stop = false;
|
||||||
private int seat;
|
private int seat;
|
||||||
private Lock table;
|
private Lock table;
|
||||||
private Condition cond;
|
private Condition cond;
|
||||||
|
|
@ -14,8 +14,6 @@ public class Philosopher extends Thread implements IPhilosopher {
|
||||||
private Philosopher right;
|
private Philosopher right;
|
||||||
private boolean essen;
|
private boolean essen;
|
||||||
private int gegessen = 0;
|
private int gegessen = 0;
|
||||||
boolean stop = false;
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setLeft(IPhilosopher left) {
|
public void setLeft(IPhilosopher left) {
|
||||||
|
|
@ -49,7 +47,6 @@ public class Philosopher extends Thread implements IPhilosopher {
|
||||||
log(seat, "stopping");
|
log(seat, "stopping");
|
||||||
this.stop = true;
|
this.stop = true;
|
||||||
interrupt();
|
interrupt();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -60,21 +57,18 @@ public class Philosopher extends Thread implements IPhilosopher {
|
||||||
Thread.sleep(MAX_THINKING_DURATION_MS);
|
Thread.sleep(MAX_THINKING_DURATION_MS);
|
||||||
try {
|
try {
|
||||||
startEating();
|
startEating();
|
||||||
|
|
||||||
log(seat, "essen");
|
log(seat, "essen");
|
||||||
Thread.sleep(MAX_EATING_DURATION_MS);
|
Thread.sleep(MAX_EATING_DURATION_MS);
|
||||||
gegessen = gegessen + 1;
|
gegessen = gegessen + 1;
|
||||||
log(seat, "gegessen " + gegessen);
|
log(seat, "gegessen " + gegessen);
|
||||||
}
|
} finally {
|
||||||
finally {
|
|
||||||
stopEating();
|
stopEating();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} catch (InterruptedException e) {
|
||||||
catch(InterruptedException e){
|
|
||||||
//Hier ist eine Exception gefunden worden
|
//Hier ist eine Exception gefunden worden
|
||||||
|
System.out.println("Hier ist ein Thread unterbrochen worden");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startEating() throws InterruptedException {
|
private void startEating() throws InterruptedException {
|
||||||
|
|
@ -92,7 +86,6 @@ public class Philosopher extends Thread implements IPhilosopher {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void stopEating() throws InterruptedException {
|
private void stopEating() throws InterruptedException {
|
||||||
table.lock();
|
table.lock();
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
Binary file not shown.
Loading…
Reference in New Issue