Update
parent
cdbcf46df5
commit
043fcbc994
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue