From 2f5e5dbe339202fde42b66f75730d795d6b8be7b Mon Sep 17 00:00:00 2001 From: 2wenty1ne Date: Sat, 26 Oct 2024 17:49:49 +0200 Subject: [PATCH] Added given files --- pp.A1-CondPhilosophers/pom.xml | 61 +++++++++++++++++++ .../src/main/java/pp/IPhilosopher.java | 30 +++++++++ .../src/main/java/pp/Philosopher.java | 36 +++++++++++ .../main/java/pp/PhilosopherExperiment.java | 37 +++++++++++ 4 files changed, 164 insertions(+) create mode 100644 pp.A1-CondPhilosophers/pom.xml create mode 100644 pp.A1-CondPhilosophers/src/main/java/pp/IPhilosopher.java create mode 100644 pp.A1-CondPhilosophers/src/main/java/pp/Philosopher.java create mode 100644 pp.A1-CondPhilosophers/src/main/java/pp/PhilosopherExperiment.java diff --git a/pp.A1-CondPhilosophers/pom.xml b/pp.A1-CondPhilosophers/pom.xml new file mode 100644 index 0000000..4fcff74 --- /dev/null +++ b/pp.A1-CondPhilosophers/pom.xml @@ -0,0 +1,61 @@ + + 4.0.0 + pp + pp.A1-CondPhilosophers + 1.0-SNAPSHOT + jar + + + pp.PhilosopherExperiment + 10 + UTF-8 + + + + org.junit.jupiter + junit-jupiter + 5.10.0 + test + + + org.projectlombok + lombok + 1.18.30 + provided + + + net.jcip + jcip-annotations + 1.0 + provided + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0 + + + org.apache.maven.plugins + maven-compiler-plugin + 3.9.0 + + + org.codehaus.mojo + exec-maven-plugin + 3.1.0 + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.5.0 + + private + en_US + + + + + diff --git a/pp.A1-CondPhilosophers/src/main/java/pp/IPhilosopher.java b/pp.A1-CondPhilosophers/src/main/java/pp/IPhilosopher.java new file mode 100644 index 0000000..edb96c8 --- /dev/null +++ b/pp.A1-CondPhilosophers/src/main/java/pp/IPhilosopher.java @@ -0,0 +1,30 @@ +package pp; + +import java.util.concurrent.locks.Lock; + +public interface IPhilosopher { + + void run(); + + void setLeft(IPhilosopher left); + + void setRight(IPhilosopher right); + + void setSeat(int seat); + + void setTable(Lock table); + + void start(); + + void stopPhilosopher(); + + default void log(int seat, String message) { + synchronized (Philosopher.class) { + for (var i = 1; i <= seat; i++) { + System.out.print(" "); + } + System.out.println("P" + seat + ": " + message); + } + } + +} diff --git a/pp.A1-CondPhilosophers/src/main/java/pp/Philosopher.java b/pp.A1-CondPhilosophers/src/main/java/pp/Philosopher.java new file mode 100644 index 0000000..8139cfb --- /dev/null +++ b/pp.A1-CondPhilosophers/src/main/java/pp/Philosopher.java @@ -0,0 +1,36 @@ +package pp; + +import java.util.concurrent.locks.Lock; + +public class Philosopher extends Thread implements IPhilosopher { + private int seat; + + @Override + public void setLeft(IPhilosopher left) { + // TODO Auto-generated method stub + // Cast auf Philosopher erforderlich + } + + @Override + public void setRight(IPhilosopher right) { + // TODO Auto-generated method stub + // Cast auf Philosopher erforderlich + } + + @Override + public void setSeat(int seat) { + this.seat = seat; + } + + @Override + public void setTable(Lock table) { + // TODO Auto-generated method stub + + } + + @Override + public void stopPhilosopher() { + // TODO Auto-generated method stub + + } +} diff --git a/pp.A1-CondPhilosophers/src/main/java/pp/PhilosopherExperiment.java b/pp.A1-CondPhilosophers/src/main/java/pp/PhilosopherExperiment.java new file mode 100644 index 0000000..ad68ed4 --- /dev/null +++ b/pp.A1-CondPhilosophers/src/main/java/pp/PhilosopherExperiment.java @@ -0,0 +1,37 @@ +package pp; + +import java.util.concurrent.locks.ReentrantLock; + +public class PhilosopherExperiment { + static final int MAX_THINKING_DURATION_MS = 3000; + static final int MAX_EATING_DURATION_MS = 3000; + static final int MAX_TAKING_TIME_MS = 100; + static final int PHILOSOPHER_NUM = 5; + static final int EXP_DURATION_MS = 20000; + static IPhilosopher[] philosophers = new Philosopher[PHILOSOPHER_NUM]; + + public static void main(String... args) throws InterruptedException { + var table = new ReentrantLock(); + for (var i = 0; i < PHILOSOPHER_NUM; i++) { + philosophers[i] = new Philosopher(); + philosophers[i].setTable(table); + philosophers[i].setSeat(i); + } + philosophers[0].setLeft(philosophers[PHILOSOPHER_NUM - 1]); + philosophers[0].setRight(philosophers[1]); + for (var i = 1; i < (PHILOSOPHER_NUM - 1); i++) { + philosophers[i].setLeft(philosophers[i - 1]); + philosophers[i].setRight(philosophers[i + 1]); + } + philosophers[PHILOSOPHER_NUM - 1] + .setLeft(philosophers[PHILOSOPHER_NUM - 2]); + philosophers[PHILOSOPHER_NUM - 1].setRight(philosophers[0]); + for (var i = 0; i < PHILOSOPHER_NUM; i++) { + philosophers[i].start(); + } + Thread.sleep(EXP_DURATION_MS); + for (var i = 0; i < PHILOSOPHER_NUM; i++) { + philosophers[i].stopPhilosopher(); + } + } +}