From 5ad8774c5387cbf35827305dc1cda6159e225468 Mon Sep 17 00:00:00 2001 From: 2wenty1ne Date: Mon, 28 Oct 2024 00:55:37 +0100 Subject: [PATCH] Added first construct: All setter, stop method, and run setup --- .../src/main/java/pp/Philosopher.java | 56 +++++++++++++++---- 1 file changed, 44 insertions(+), 12 deletions(-) diff --git a/pp.A1-CondPhilosophers/src/main/java/pp/Philosopher.java b/pp.A1-CondPhilosophers/src/main/java/pp/Philosopher.java index 8139cfb..3a06a6c 100644 --- a/pp.A1-CondPhilosophers/src/main/java/pp/Philosopher.java +++ b/pp.A1-CondPhilosophers/src/main/java/pp/Philosopher.java @@ -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; } }