Assigment finished (wahrscheinlich lol)
parent
5ad8774c53
commit
fad4d1925e
|
@ -1,47 +1,72 @@
|
||||||
package pp;
|
package pp;
|
||||||
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
import java.util.concurrent.locks.Condition;
|
||||||
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.Lock;
|
||||||
|
|
||||||
|
import static pp.PhilosopherExperiment.MAX_EATING_DURATION_MS;
|
||||||
import static pp.PhilosopherExperiment.MAX_THINKING_DURATION_MS;
|
import static pp.PhilosopherExperiment.MAX_THINKING_DURATION_MS;
|
||||||
|
|
||||||
public class Philosopher extends Thread implements IPhilosopher {
|
public class Philosopher extends Thread implements IPhilosopher {
|
||||||
private final Random random;
|
private final Random random;
|
||||||
|
private volatile boolean stop;
|
||||||
|
|
||||||
private int seat;
|
private int seat;
|
||||||
private Philosopher left;
|
private Philosopher left;
|
||||||
private Philosopher right;
|
private Philosopher right;
|
||||||
private Lock table;
|
private Lock table;
|
||||||
private volatile boolean stop;
|
public boolean eating;
|
||||||
|
public Condition condition;
|
||||||
|
|
||||||
|
|
||||||
public Philosopher() {
|
public Philosopher() {
|
||||||
this.stop = false;
|
|
||||||
this.random = new Random();
|
this.random = new Random();
|
||||||
|
this.stop = false;
|
||||||
|
this.eating = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
log("starting");
|
||||||
try {
|
try {
|
||||||
while (!this.stop) {
|
while (!this.stop) {
|
||||||
// TODO
|
|
||||||
think();
|
think();
|
||||||
eat();
|
eat();
|
||||||
}
|
}
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
}
|
}
|
||||||
|
log("stopped;");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void think() throws InterruptedException {
|
private void think() throws InterruptedException {
|
||||||
// TODO
|
|
||||||
Thread.sleep(this.random.nextInt(MAX_THINKING_DURATION_MS));
|
Thread.sleep(this.random.nextInt(MAX_THINKING_DURATION_MS));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void eat() {
|
private void eat() throws InterruptedException {
|
||||||
// TODO
|
this.table.lock();
|
||||||
|
try {
|
||||||
|
while (this.left.eating || this.right.eating) {
|
||||||
|
this.condition.await();
|
||||||
|
}
|
||||||
|
log("left nor right eating");
|
||||||
|
this.eating = true;
|
||||||
|
log("eating");
|
||||||
|
Thread.sleep(this.random.nextInt(MAX_EATING_DURATION_MS));
|
||||||
|
|
||||||
|
this.eating = false;
|
||||||
|
this.left.condition.signal();
|
||||||
|
this.right.condition.signal();
|
||||||
|
|
||||||
|
log("done eating");
|
||||||
|
} finally {
|
||||||
|
table.unlock();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stopPhilosopher() {
|
public void stopPhilosopher() {
|
||||||
|
log("stopping");
|
||||||
this.stop = true;
|
this.stop = true;
|
||||||
interrupt();
|
interrupt();
|
||||||
}
|
}
|
||||||
|
@ -64,5 +89,15 @@ public class Philosopher extends Thread implements IPhilosopher {
|
||||||
@Override
|
@Override
|
||||||
public void setTable(Lock table) {
|
public void setTable(Lock table) {
|
||||||
this.table = table;
|
this.table = table;
|
||||||
|
this.condition = this.table.newCondition();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void log(String message) {
|
||||||
|
synchronized (Philosopher.class) {
|
||||||
|
for (var i = 1; i <= this.seat; i++) {
|
||||||
|
System.out.print(" ");
|
||||||
|
}
|
||||||
|
System.out.println(threadId() + " " + message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue