From 7feec0d3cd88883d902b2696a9d29905166ef860 Mon Sep 17 00:00:00 2001 From: kelesec93669 Date: Tue, 5 Dec 2023 15:43:23 +0100 Subject: [PATCH] DiningPhilo --- src/praktikum04/BetterPhilosopher.java | 30 ++++++++++++++++ src/praktikum04/Fork.java | 41 ++++++++++++++++++++++ src/praktikum04/Philosopher.java | 47 ++++++++++++++++++++++++++ src/praktikum04/Table.java | 20 +++++++++++ test/praktikum04/ForkTest.java | 32 ++++++++++++++++++ 5 files changed, 170 insertions(+) create mode 100644 src/praktikum04/BetterPhilosopher.java create mode 100644 src/praktikum04/Fork.java create mode 100644 src/praktikum04/Philosopher.java create mode 100644 src/praktikum04/Table.java create mode 100644 test/praktikum04/ForkTest.java diff --git a/src/praktikum04/BetterPhilosopher.java b/src/praktikum04/BetterPhilosopher.java new file mode 100644 index 0000000..11f6541 --- /dev/null +++ b/src/praktikum04/BetterPhilosopher.java @@ -0,0 +1,30 @@ +package praktikum04; + +public class BetterPhilosopher extends Philosopher{ + + public BetterPhilosopher(int id, Fork left, Fork right) { + super(id, left, right); + } + + public void allocateForks() { + Fork fork1 = left; + Fork fork2 = right; + + if(left.getId() > right.getId()) { + fork1 = right; + fork2 = left; + } + + System.out.println("Philosopher" + id + "is trying to get a fork" + fork1.getId()); + while(!fork1.take()) + Thread.yield(); + System.out.println("Philosopher" + id + "got a fork" + fork1.getId()); + + System.out.println("Philosopher" + id + "is trying to get a fork" + fork2.getId()); + while(!fork2.take()) + Thread.yield(); + System.out.println("Philosopher" + id + "got a fork" + fork2.getId()); + + } + +} diff --git a/src/praktikum04/Fork.java b/src/praktikum04/Fork.java new file mode 100644 index 0000000..b25314e --- /dev/null +++ b/src/praktikum04/Fork.java @@ -0,0 +1,41 @@ +package praktikum04; + +public class Fork { + + + private boolean inUse = false; + private final int id; + + public Fork(int id) { + this.id = id; + } + + public int getId() { + + return id; + + } + + public boolean take() { + synchronized (this) { + if(inUse) { + return false; + } + inUse = true; + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + System.out.println("Gabel" + id + "konnte nicht angenommen werden"); + + } + return true; + } + } + + public void put() { + synchronized (this) { + inUse = false; + } + + } +} diff --git a/src/praktikum04/Philosopher.java b/src/praktikum04/Philosopher.java new file mode 100644 index 0000000..8e8839c --- /dev/null +++ b/src/praktikum04/Philosopher.java @@ -0,0 +1,47 @@ +package praktikum04; + +import java.awt.desktop.AppHiddenListener; + +public class Philosopher implements Runnable{ + + protected int id ; + protected final Fork left; + + protected final Fork right; + + public Philosopher(int id, Fork left, Fork right) { + this.id = id; + this.left = left; + this.right = right; + } + + @Override + public void run() { + + allocateForks(); + System.out.println("Philosopher " + id + " is eating"); + + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + System.out.println("Philosopher" + id + "was interrupted"); + } + left.put(); + right.put(); + + System.out.println("Philosopher" + id + "is done eating"); + + + } + + protected void allocateForks() { + System.out.println("Philosopher" + id + "is trying to get a fork." + left.getId()); + while (!left.take()) + Thread.yield(); + System.out.println("Philosopher" + id + "got fork" + left.getId()); + System.out.println("Philosopher" + id + "is trying to get a fork" + right.getId()); + while (!right.take()) + Thread.yield(); + System.out.println("Philosopher" + id + "got fork" + right.getId()); + } +} diff --git a/src/praktikum04/Table.java b/src/praktikum04/Table.java new file mode 100644 index 0000000..5e301ba --- /dev/null +++ b/src/praktikum04/Table.java @@ -0,0 +1,20 @@ +package praktikum04; + +public class Table { + + public static void main(String[] args) { + Fork[] forks = { new Fork(0), new Fork(1), new Fork(2), + new Fork(3), new Fork(4)}; + Philosopher[] philosophers = { + new Philosopher(0, forks[0], forks[1]), + new Philosopher(1, forks[1], forks[2]), + new Philosopher(2, forks[2], forks[3]), + new Philosopher(3, forks[3], forks[4]), + new Philosopher(4, forks[4], forks[0]) + }; + for (Philosopher philosopher : philosophers) { + Thread t = new Thread(philosopher); + t.start(); + } + } +} diff --git a/test/praktikum04/ForkTest.java b/test/praktikum04/ForkTest.java new file mode 100644 index 0000000..012e167 --- /dev/null +++ b/test/praktikum04/ForkTest.java @@ -0,0 +1,32 @@ +package praktikum04; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ForkTest { + + @Test + void getId() { + + Fork fork = new Fork(1); + fork.getId(); + } + + @Test + void take() { + + Fork fork = new Fork(1); + } + + @Test + void put() { + + Fork fork = new Fork(1); + fork.take(); + + fork.put(); + + + } +} \ No newline at end of file