diff --git a/src/Praktikum03/BetterPhilosopher.java b/src/Praktikum03/BetterPhilosopher.java new file mode 100644 index 0000000..8900322 --- /dev/null +++ b/src/Praktikum03/BetterPhilosopher.java @@ -0,0 +1,23 @@ +package Praktikum03; + +public class BetterPhilosopher extends Philosopher { + + public BetterPhilosopher(int id, Fork left, Fork right) { + super(id, left, right); + } + @Override + public void run() { + + if (left.getId() < right.getId()) { + tryToTakeFork(left); + tryToTakeFork(right); + }else{ + tryToTakeFork(right); + tryToTakeFork(left); + } + + eat(); + + } + +} diff --git a/src/Praktikum03/Fork.java b/src/Praktikum03/Fork.java new file mode 100644 index 0000000..7671684 --- /dev/null +++ b/src/Praktikum03/Fork.java @@ -0,0 +1,33 @@ +package Praktikum03; + +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 isInUse(){ + return inUse; + } + + public synchronized boolean take() { + if(inUse){ + return false; + }else { + inUse = true; + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + System.out.println("Fehler"); + } + return true; + } + } + public synchronized void put(){ + inUse = false; + } +} diff --git a/src/Praktikum03/Philosopher.java b/src/Praktikum03/Philosopher.java new file mode 100644 index 0000000..b466b13 --- /dev/null +++ b/src/Praktikum03/Philosopher.java @@ -0,0 +1,47 @@ +package Praktikum03; + + +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() { + + tryToTakeFork(left); + tryToTakeFork(right); + + eat(); + + } + + protected void eat() { + System.out.println("Philosopher " + id + " is eating"); + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + left.put(); + right.put(); + System.out.println("Philosopher " + id + " finished eating"); + } + + protected void tryToTakeFork(Fork fork) { + System.out.println("Philosopher " + id + " is trying to get fork " + fork.getId()); + while(!fork.take()){ + Thread.yield(); + } + System.out.println("Philosopher " + id + " got fork " + fork.getId()); + } + + +} diff --git a/src/Praktikum03/Table.java b/src/Praktikum03/Table.java new file mode 100644 index 0000000..29d28bc --- /dev/null +++ b/src/Praktikum03/Table.java @@ -0,0 +1,19 @@ +package Praktikum03; + +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)}; + BetterPhilosopher[] philosophers = { + new BetterPhilosopher(0, forks[0], forks[1]), + new BetterPhilosopher(1, forks[1], forks[2]), + new BetterPhilosopher(2, forks[2], forks[3]), + new BetterPhilosopher(3, forks[3], forks[4]), + new BetterPhilosopher(4, forks[4], forks[0]) + }; + for (BetterPhilosopher philosopher : philosophers) { + Thread t = new Thread(philosopher); + t.start(); + } + } +} \ No newline at end of file diff --git a/test/Praktikum03/ForkTest.java b/test/Praktikum03/ForkTest.java new file mode 100644 index 0000000..0edb271 --- /dev/null +++ b/test/Praktikum03/ForkTest.java @@ -0,0 +1,31 @@ +package Praktikum03; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; +class ForkTest { + + @Test + void getId() { + Fork fork = new Fork(1); + assertEquals(1, fork.getId()); + } + + @Test + void take() throws InterruptedException { + Fork fork = new Fork(2); + assertTrue(fork.take()); + assertFalse(fork.take()); + fork.put(); + assertTrue(fork.take()); + } + + @Test + void put() throws InterruptedException { + Fork fork = new Fork(3); + fork.take(); + fork.put(); + assertFalse(fork.isInUse()); + + } +} \ No newline at end of file