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