pull/2/head
obaya 2025-05-05 10:15:12 +02:00
parent cdbcf46df5
commit 043fcbc994
1 changed files with 15 additions and 10 deletions

View File

@ -26,7 +26,7 @@ public class Philosopher extends Thread implements IPhilosopher {
@Override @Override
public void run() { public void run() {
try { try {
while (this.stopped == false) { while (!this.stopped) {
think(); think();
eat(); eat();
} }
@ -35,7 +35,7 @@ public class Philosopher extends Thread implements IPhilosopher {
} }
private void eat() throws InterruptedException { private void eat() throws InterruptedException {
table.lock(); this.table.lock();
try { try {
while (this.links.eating || this.rechts.eating) { while (this.links.eating || this.rechts.eating) {
@ -49,17 +49,22 @@ public class Philosopher extends Thread implements IPhilosopher {
Thread.sleep(this.random.nextInt(PhilosopherExperiment.MAX_EATING_DURATION_MS)); Thread.sleep(this.random.nextInt(PhilosopherExperiment.MAX_EATING_DURATION_MS));
} }
private void think() { private void think() throws InterruptedException {
table.lock(); this.table.lock();
this.links.canEat.signal();
this.rechts.canEat.signal();
table.unlock(); table.unlock();
try { try {
Thread.sleep(random.nextInt(PhilosopherExperiment.MAX_THINKING_DURATION_MS)); if (this.eating)
log(seat, " : denkt gerade !"); this.eating = false;
} catch (InterruptedException e) { this.links.canEat.signal();
e.printStackTrace(); this.rechts.canEat.signal();
} finally {
this.table.unlock();
} }
Thread.sleep(random.nextInt(PhilosopherExperiment.MAX_THINKING_DURATION_MS));
} }
@Override @Override