added ex R
This commit is contained in:
		
							
								
								
									
										28
									
								
								src/exercises/ex_r/Customer.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								src/exercises/ex_r/Customer.java
									
									
									
									
									
										Normal file
									
								
							| @@ -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; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										15
									
								
								src/exercises/ex_r/Package.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								src/exercises/ex_r/Package.java
									
									
									
									
									
										Normal file
									
								
							| @@ -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() + | ||||||
|  |                '}'; | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										19
									
								
								src/exercises/ex_r/PostOffice.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								src/exercises/ex_r/PostOffice.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,19 @@ | |||||||
|  | package exercises.ex_r; | ||||||
|  |  | ||||||
|  | import java.util.concurrent.BlockingQueue; | ||||||
|  | import java.util.concurrent.LinkedBlockingQueue; | ||||||
|  |  | ||||||
|  | public class PostOffice { | ||||||
|  |     private BlockingQueue<Package> 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; | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										22
									
								
								src/exercises/ex_r/Postman.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								src/exercises/ex_r/Postman.java
									
									
									
									
									
										Normal file
									
								
							| @@ -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; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										10
									
								
								src/exercises/ex_r/TestingBlockingQueue_PostOffice.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								src/exercises/ex_r/TestingBlockingQueue_PostOffice.java
									
									
									
									
									
										Normal file
									
								
							| @@ -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(); | ||||||
|  |     } | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user