Added first construct: All setter, stop method, and run setup

main
2wenty1ne 2024-10-28 00:55:37 +01:00
parent 2f5e5dbe33
commit 5ad8774c53
1 changed files with 44 additions and 12 deletions

View File

@ -1,20 +1,59 @@
package pp;
import java.util.Random;
import java.util.concurrent.locks.Lock;
import static pp.PhilosopherExperiment.MAX_THINKING_DURATION_MS;
public class Philosopher extends Thread implements IPhilosopher {
private final Random random;
private int seat;
private Philosopher left;
private Philosopher right;
private Lock table;
private volatile boolean stop;
public Philosopher() {
this.stop = false;
this.random = new Random();
}
@Override
public void run() {
try {
while (!this.stop) {
// TODO
think();
eat();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
private void think() throws InterruptedException {
// TODO
Thread.sleep(this.random.nextInt(MAX_THINKING_DURATION_MS));
}
private void eat() {
// TODO
}
@Override
public void stopPhilosopher() {
this.stop = true;
interrupt();
}
@Override
public void setLeft(IPhilosopher left) {
// TODO Auto-generated method stub
// Cast auf Philosopher erforderlich
this.left = (Philosopher) left;
}
@Override
public void setRight(IPhilosopher right) {
// TODO Auto-generated method stub
// Cast auf Philosopher erforderlich
this.right = (Philosopher) right;
}
@Override
@ -24,13 +63,6 @@ public class Philosopher extends Thread implements IPhilosopher {
@Override
public void setTable(Lock table) {
// TODO Auto-generated method stub
}
@Override
public void stopPhilosopher() {
// TODO Auto-generated method stub
this.table = table;
}
}