Final Version

pull/2/head
obaya 2025-05-04 22:15:15 +02:00
parent 285200c86e
commit cdbcf46df5
1 changed files with 6 additions and 13 deletions

View File

@ -10,7 +10,7 @@ public class Philosopher extends Thread implements IPhilosopher {
private Philosopher rechts;
private Philosopher links;
private Lock table;
private Condition condition;
private Condition canEat;
private volatile boolean stopped;
private volatile boolean eating;
private final Random random;
@ -21,10 +21,6 @@ public class Philosopher extends Thread implements IPhilosopher {
this.stopped = false;
}
public static void main(String... args) {
// TODO
}
@Override
@ -35,7 +31,6 @@ public class Philosopher extends Thread implements IPhilosopher {
eat();
}
} catch (InterruptedException e) {
// Thread was interrupted, exit gracefully
}
}
@ -45,9 +40,8 @@ public class Philosopher extends Thread implements IPhilosopher {
while (this.links.eating || this.rechts.eating) {
log(seat, " : isst gerade!");
this.condition.await();
this.canEat.await();
}
this.eating = true;
} finally {
table.unlock();
@ -57,8 +51,8 @@ public class Philosopher extends Thread implements IPhilosopher {
private void think() {
table.lock();
this.links.condition.signal();
this.rechts.condition.signal();
this.links.canEat.signal();
this.rechts.canEat.signal();
table.unlock();
try {
Thread.sleep(random.nextInt(PhilosopherExperiment.MAX_THINKING_DURATION_MS));
@ -91,13 +85,12 @@ public class Philosopher extends Thread implements IPhilosopher {
public void setTable(Lock table) {
// TODO Auto-generated method stub
this.table = table;
condition = this.table.newCondition();
canEat = this.table.newCondition();
}
@Override
public void stopPhilosopher() {
this.stopped = true;
interrupt();
}