From 28ab30e73537585b8f3c1558f925e42df2a386f4 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Tue, 7 Jan 2025 13:57:09 +0100 Subject: [PATCH] added ex T --- src/exercises/Utils.java | 7 +++++ src/exercises/ex_t/BowlSemaphore.java | 24 +++++++++++++++ src/exercises/ex_t/Dog.java | 29 +++++++++++++++++++ src/exercises/ex_t/Feeder.java | 28 ++++++++++++++++++ .../ex_t/TestingSemaphore_DogBreeder.java | 16 ++++++++++ 5 files changed, 104 insertions(+) create mode 100644 src/exercises/Utils.java create mode 100644 src/exercises/ex_t/BowlSemaphore.java create mode 100644 src/exercises/ex_t/Dog.java create mode 100644 src/exercises/ex_t/Feeder.java create mode 100644 src/exercises/ex_t/TestingSemaphore_DogBreeder.java diff --git a/src/exercises/Utils.java b/src/exercises/Utils.java new file mode 100644 index 0000000..007f3a5 --- /dev/null +++ b/src/exercises/Utils.java @@ -0,0 +1,7 @@ +package exercises; + +public class Utils { + public static void randomSleep(long min, long max) throws InterruptedException { + Thread.sleep((long) (min + Math.random() * max)); + } +} diff --git a/src/exercises/ex_t/BowlSemaphore.java b/src/exercises/ex_t/BowlSemaphore.java new file mode 100644 index 0000000..1bcd81e --- /dev/null +++ b/src/exercises/ex_t/BowlSemaphore.java @@ -0,0 +1,24 @@ +package exercises.ex_t; + +import java.util.concurrent.Semaphore; + +public class BowlSemaphore { + private boolean isFull = false; + private Semaphore semaphore = new Semaphore(1); + + public boolean fill() throws InterruptedException { + semaphore.acquire(); + boolean wasFull = isFull; + isFull = true; + semaphore.release(); + return wasFull; + } + + public boolean empty() throws InterruptedException { + semaphore.acquire(); + boolean wasFull = isFull; + isFull = false; + semaphore.release(); + return wasFull; + } +} diff --git a/src/exercises/ex_t/Dog.java b/src/exercises/ex_t/Dog.java new file mode 100644 index 0000000..78d97aa --- /dev/null +++ b/src/exercises/ex_t/Dog.java @@ -0,0 +1,29 @@ +package exercises.ex_t; + +import exercises.Utils; + +public class Dog implements Runnable { + private final String name; + private final BowlSemaphore[] bowls; + + public Dog(String name, BowlSemaphore[] bowls) { + this.name = name; + this.bowls = bowls; + } + + @Override + public void run() { + while (true) { + for (int i = 0; i < 3; i++) { + BowlSemaphore bowl = bowls[i]; + try { + if (bowl.empty()) { + System.out.println(name + " ate from bowl " + i); + Utils.randomSleep(10000, 20000); + break; + } + } catch (InterruptedException _) {} + } + } + } +} diff --git a/src/exercises/ex_t/Feeder.java b/src/exercises/ex_t/Feeder.java new file mode 100644 index 0000000..eeeb883 --- /dev/null +++ b/src/exercises/ex_t/Feeder.java @@ -0,0 +1,28 @@ +package exercises.ex_t; + +import exercises.Utils; + +public class Feeder implements Runnable { + private final String name; + private final BowlSemaphore[] bowls; + + public Feeder(String name, BowlSemaphore[] bowls) { + this.name = name; + this.bowls = bowls; + } + + @Override + public void run() { + while (true) { + for (int i = 0; i < 3; i++) { + BowlSemaphore bowl = bowls[i]; + try { + if (!bowl.fill()) { + System.out.println(name + " filled bowl " + i); + Utils.randomSleep(1000, 3000); + } + } catch (InterruptedException _) {} + } + } + } +} diff --git a/src/exercises/ex_t/TestingSemaphore_DogBreeder.java b/src/exercises/ex_t/TestingSemaphore_DogBreeder.java new file mode 100644 index 0000000..0704220 --- /dev/null +++ b/src/exercises/ex_t/TestingSemaphore_DogBreeder.java @@ -0,0 +1,16 @@ +package exercises.ex_t; + +public class TestingSemaphore_DogBreeder { + public static void main(String[] args) { + BowlSemaphore[] bowlsSemaphores = new BowlSemaphore[3]; + for (int i = 0; i < 3; i++) { + bowlsSemaphores[i] = new BowlSemaphore(); + } + + new Thread(new Feeder("Marco", bowlsSemaphores)).start(); + new Thread(new Feeder("Luisa", bowlsSemaphores)).start(); + for (int i = 1; i < 10; i++) { + new Thread(new Dog("Dog" + i, bowlsSemaphores)).start(); + } + } +}