Erste Version, unterstützt von Claude
parent
d601ef8b71
commit
97a3a4da9d
|
|
@ -1,39 +1,105 @@
|
|||
package pp;
|
||||
|
||||
import java.util.concurrent.locks.Condition;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
public class Philosopher extends Thread implements IPhilosopher {
|
||||
private int seat;
|
||||
static final int MAX_THINKING_DURATION_MS = 1000;
|
||||
static final int MAX_EATING_DURATION_MS = 3000;
|
||||
static final int MAX_TAKING_TIME_MS = 1000;
|
||||
private int seat;
|
||||
private Lock table;
|
||||
private Condition cond;
|
||||
private Philosopher left;
|
||||
private Philosopher right;
|
||||
private boolean essen;
|
||||
private int gegessen = 0;
|
||||
boolean stop = false;
|
||||
|
||||
@Override
|
||||
public void setLeft(IPhilosopher left) {
|
||||
// TODO Auto-generated method stub
|
||||
// Cast auf Philosopher erforderlich
|
||||
Philosopher x = (Philosopher) left;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRight(IPhilosopher right) {
|
||||
// TODO Auto-generated method stub
|
||||
// Cast auf Philosopher erforderlich
|
||||
Philosopher y = (Philosopher) right;
|
||||
}
|
||||
@Override
|
||||
public void setLeft(IPhilosopher left) {
|
||||
// TODO Auto-generated method stub
|
||||
// Cast auf Philosopher erforderlich
|
||||
this.left = (Philosopher) left;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSeat(int seat) {
|
||||
this.seat = seat;
|
||||
}
|
||||
@Override
|
||||
public void setRight(IPhilosopher right) {
|
||||
// TODO Auto-generated method stub
|
||||
// Cast auf Philosopher erforderlich
|
||||
this.right = (Philosopher) right;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTable(Lock table) {
|
||||
// TODO Auto-generated method stub
|
||||
@Override
|
||||
public void setSeat(int seat) {
|
||||
this.seat = seat;
|
||||
}
|
||||
|
||||
}
|
||||
@Override
|
||||
public void setTable(Lock table) {
|
||||
// TODO Auto-generated method stub
|
||||
this.table = table;
|
||||
this.cond = table.newCondition();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stopPhilosopher() {
|
||||
// TODO Auto-generated method stub
|
||||
@Override
|
||||
public void stopPhilosopher() {
|
||||
// TODO Auto-generated method stub
|
||||
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 {
|
||||
stopEating();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(InterruptedException e){
|
||||
//Hier ist eine Exception gefunden worden
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void startEating() throws InterruptedException {
|
||||
table.lock();
|
||||
try {
|
||||
while (left.essen | right.essen) {
|
||||
if (left.essen) {
|
||||
left.cond.await();
|
||||
}
|
||||
right.cond.await();
|
||||
}
|
||||
essen = true;
|
||||
} finally {
|
||||
table.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void stopEating() throws InterruptedException {
|
||||
table.lock();
|
||||
try {
|
||||
essen = false;
|
||||
cond.signalAll();
|
||||
} finally {
|
||||
table.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue