From 3c6d89bdd82e669b11c508f6a075de82af7ca1b3 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Tue, 10 Dec 2024 15:35:35 +0100 Subject: [PATCH] added ex K --- src/exercises/ex_k/Customer.java | 31 ++++++++++++++++ src/exercises/ex_k/Package.java | 15 ++++++++ src/exercises/ex_k/PostOffice.java | 38 ++++++++++++++++++++ src/exercises/ex_k/Postman.java | 23 ++++++++++++ src/exercises/ex_k/TestingGuardedBlocks.java | 10 ++++++ 5 files changed, 117 insertions(+) create mode 100644 src/exercises/ex_k/Customer.java create mode 100644 src/exercises/ex_k/Package.java create mode 100644 src/exercises/ex_k/PostOffice.java create mode 100644 src/exercises/ex_k/Postman.java create mode 100644 src/exercises/ex_k/TestingGuardedBlocks.java diff --git a/src/exercises/ex_k/Customer.java b/src/exercises/ex_k/Customer.java new file mode 100644 index 0000000..d2cf243 --- /dev/null +++ b/src/exercises/ex_k/Customer.java @@ -0,0 +1,31 @@ +package exercises.ex_k; + +public class Customer implements Runnable { + private String name; + private PostOffice postOffice; + + public Customer(String name, PostOffice postOffice) { + this.name = name; + this.postOffice = postOffice; + } + + public String getName() { + return name; + } + + @Override + public void run() { + Package pkg; + while (true) { + pkg = new Package(this); + System.out.println("Registering " + pkg + "..."); + postOffice.registerPackage(pkg); + System.out.println("Registered " + pkg); + try { + Thread.sleep((long) (Math.random() * 2000 + 1000)); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + } +} diff --git a/src/exercises/ex_k/Package.java b/src/exercises/ex_k/Package.java new file mode 100644 index 0000000..831ec02 --- /dev/null +++ b/src/exercises/ex_k/Package.java @@ -0,0 +1,15 @@ +package exercises.ex_k; + +public class Package { + private Customer sender; + + public Package(Customer sender) { + this.sender = sender; + } + @Override + public String toString() { + return "Package{" + + "sender=" + sender.getName() + + '}'; + } +} \ No newline at end of file diff --git a/src/exercises/ex_k/PostOffice.java b/src/exercises/ex_k/PostOffice.java new file mode 100644 index 0000000..5feae8f --- /dev/null +++ b/src/exercises/ex_k/PostOffice.java @@ -0,0 +1,38 @@ +package exercises.ex_k; + +import java.util.LinkedList; +import java.util.List; + +public class PostOffice { + public static final int CAPACITY = 5; + private List packages = new LinkedList<>(); + + public synchronized void registerPackage(Package pkg) { + while (packages.size() >= CAPACITY) { + try { + System.out.println("[PostOffice] Storage is full, waiting"); + wait(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + packages.add(pkg); + System.out.println("[PostOffice] Storage: " + packages.size() + " / " + CAPACITY); + notifyAll(); + } + + public synchronized Package getNextPackage() { + while (packages.isEmpty()) { + try { + System.out.println("[PostOffice] Storage is empty, waiting"); + wait(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + Package pkg = packages.removeFirst(); + System.out.println("[PostOffice] Storage: " + packages.size() + " / " + CAPACITY); + notifyAll(); + return pkg; + } +} diff --git a/src/exercises/ex_k/Postman.java b/src/exercises/ex_k/Postman.java new file mode 100644 index 0000000..8febd33 --- /dev/null +++ b/src/exercises/ex_k/Postman.java @@ -0,0 +1,23 @@ +package exercises.ex_k; + +public class Postman implements Runnable { + private PostOffice postOffice; + + public Postman(PostOffice postOffice) { + this.postOffice = postOffice; + } + + @Override + public void run() { + Package pkg; + while (true) { + pkg = postOffice.getNextPackage(); + System.out.println("Delivering " + pkg); + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + } +} diff --git a/src/exercises/ex_k/TestingGuardedBlocks.java b/src/exercises/ex_k/TestingGuardedBlocks.java new file mode 100644 index 0000000..b14f227 --- /dev/null +++ b/src/exercises/ex_k/TestingGuardedBlocks.java @@ -0,0 +1,10 @@ +package exercises.ex_k; + +public class TestingGuardedBlocks { + public static void main(String[] args) { + PostOffice postOffice = new PostOffice(); + (new Thread(new Postman(postOffice))).start(); + (new Thread(new Customer("Mrs Darbellay", postOffice))).start(); + (new Thread(new Customer("Mrs Müller", postOffice))).start(); + } +}