Update
parent
043fcbc994
commit
591c8fec1b
|
@ -32,6 +32,7 @@ public class Philosopher extends Thread implements IPhilosopher {
|
||||||
}
|
}
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
}
|
}
|
||||||
|
log(seat,"beendet");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void eat() throws InterruptedException {
|
private void eat() throws InterruptedException {
|
||||||
|
@ -39,10 +40,11 @@ public class Philosopher extends Thread implements IPhilosopher {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
while (this.links.eating || this.rechts.eating) {
|
while (this.links.eating || this.rechts.eating) {
|
||||||
log(seat, " : isst gerade!");
|
log(seat,"warten!");
|
||||||
this.canEat.await();
|
this.canEat.await();
|
||||||
}
|
}
|
||||||
this.eating = true;
|
this.eating = true;
|
||||||
|
log(seat, " : isst gerade!");
|
||||||
} finally {
|
} finally {
|
||||||
table.unlock();
|
table.unlock();
|
||||||
}
|
}
|
||||||
|
@ -52,12 +54,12 @@ public class Philosopher extends Thread implements IPhilosopher {
|
||||||
private void think() throws InterruptedException {
|
private void think() throws InterruptedException {
|
||||||
this.table.lock();
|
this.table.lock();
|
||||||
|
|
||||||
table.unlock();
|
|
||||||
try {
|
try {
|
||||||
if (this.eating)
|
if (this.eating)
|
||||||
this.eating = false;
|
this.eating = false;
|
||||||
this.links.canEat.signal();
|
this.links.canEat.signal();
|
||||||
this.rechts.canEat.signal();
|
this.rechts.canEat.signal();
|
||||||
|
log(seat,"denken");
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
this.table.unlock();
|
this.table.unlock();
|
||||||
|
@ -100,6 +102,37 @@ public class Philosopher extends Thread implements IPhilosopher {
|
||||||
interrupt();
|
interrupt();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws InterruptedException {
|
||||||
|
int numPhilosophers = 5;
|
||||||
|
Lock table = new ReentrantLock();
|
||||||
|
Philosopher[] philosophers = new Philosopher[numPhilosophers];
|
||||||
|
|
||||||
|
for (int i = 0; i < numPhilosophers; i++) {
|
||||||
|
philosophers[i] = new Philosopher();
|
||||||
|
philosophers[i].setSeat(i);
|
||||||
|
philosophers[i].setTable(table);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < numPhilosophers; i++) {
|
||||||
|
philosophers[i].setLeft(philosophers[(i + numPhilosophers - 1) % numPhilosophers]);
|
||||||
|
philosophers[i].setRight(philosophers[(i + 1) % numPhilosophers]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Philosopher p : philosophers) {
|
||||||
|
p.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
Thread.sleep(10_000); // Laufzeit: 10 Sekunden
|
||||||
|
|
||||||
|
for (Philosopher p : philosophers) {
|
||||||
|
p.stopPhilosopher();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Philosopher p : philosophers) {
|
||||||
|
p.join();
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Experiment beendet!");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue