added ex T
This commit is contained in:
		
							
								
								
									
										7
									
								
								src/exercises/Utils.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								src/exercises/Utils.java
									
									
									
									
									
										Normal file
									
								
							| @@ -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)); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										24
									
								
								src/exercises/ex_t/BowlSemaphore.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								src/exercises/ex_t/BowlSemaphore.java
									
									
									
									
									
										Normal file
									
								
							| @@ -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; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										29
									
								
								src/exercises/ex_t/Dog.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								src/exercises/ex_t/Dog.java
									
									
									
									
									
										Normal file
									
								
							| @@ -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 _) {} | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										28
									
								
								src/exercises/ex_t/Feeder.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								src/exercises/ex_t/Feeder.java
									
									
									
									
									
										Normal file
									
								
							| @@ -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 _) {} | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										16
									
								
								src/exercises/ex_t/TestingSemaphore_DogBreeder.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								src/exercises/ex_t/TestingSemaphore_DogBreeder.java
									
									
									
									
									
										Normal file
									
								
							| @@ -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(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user