diff --git a/src/exercises/ex_r/Customer.java b/src/exercises/ex_r/Customer.java new file mode 100644 index 0000000..40207df --- /dev/null +++ b/src/exercises/ex_r/Customer.java @@ -0,0 +1,28 @@ +package exercises.ex_r; + +public class Customer implements Runnable { + private String name; + private PostOffice office; + + public Customer(String name, PostOffice office) { + this.name = name; + this.office = office; + } + + public String getName() { + return name; + } + + @Override + public void run() { + while (true) { + Package pkg = new Package(this); + office.registerPackage(pkg); + try { + Thread.sleep((long) (1000L + 5000L * Math.random())); + } catch (InterruptedException e) { + break; + } + } + } +} diff --git a/src/exercises/ex_r/Package.java b/src/exercises/ex_r/Package.java new file mode 100644 index 0000000..7a9ba1a --- /dev/null +++ b/src/exercises/ex_r/Package.java @@ -0,0 +1,15 @@ +package exercises.ex_r; + +public class Package { + private Customer sender; + public Package(Customer sender) { + this.sender = sender; + } + + @Override + public String toString() { + return "Package{" + + "sender=" + sender.getName() + + '}'; + } +} diff --git a/src/exercises/ex_r/PostOffice.java b/src/exercises/ex_r/PostOffice.java new file mode 100644 index 0000000..dbd2e9e --- /dev/null +++ b/src/exercises/ex_r/PostOffice.java @@ -0,0 +1,19 @@ +package exercises.ex_r; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; + +public class PostOffice { + private BlockingQueue packages = new LinkedBlockingQueue<>(); + + public void registerPackage(Package pkg) { + packages.add(pkg); + System.out.println("Registered " + pkg + " (now " + packages.size() + ")"); + } + + public Package takePackage() throws InterruptedException { + Package pkg = packages.take(); + System.out.println("Withdrawn " + pkg + " (now " + packages.size() + ")"); + return pkg; + } +} diff --git a/src/exercises/ex_r/Postman.java b/src/exercises/ex_r/Postman.java new file mode 100644 index 0000000..34ef57a --- /dev/null +++ b/src/exercises/ex_r/Postman.java @@ -0,0 +1,22 @@ +package exercises.ex_r; + +public class Postman implements Runnable { + private PostOffice office; + + public Postman(PostOffice office) { + this.office = office; + } + + @Override + public void run() { + while (true) { + try { + Package pkg = office.takePackage(); + Thread.sleep((long) (1000L + 2000L * Math.random())); + System.out.println("Delivered " + pkg); + } catch (InterruptedException e) { + break; + } + } + } +} diff --git a/src/exercises/ex_r/TestingBlockingQueue_PostOffice.java b/src/exercises/ex_r/TestingBlockingQueue_PostOffice.java new file mode 100644 index 0000000..6031794 --- /dev/null +++ b/src/exercises/ex_r/TestingBlockingQueue_PostOffice.java @@ -0,0 +1,10 @@ +package exercises.ex_r; + +public class TestingBlockingQueue_PostOffice { + 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(); + } +}