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