added ex J
This commit is contained in:
		
							
								
								
									
										33
									
								
								src/exercises/ex_j/Diner.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								src/exercises/ex_j/Diner.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,33 @@ | ||||
| package exercises.ex_j; | ||||
|  | ||||
| class Diner { | ||||
|     private String name; | ||||
|     private boolean isHungry; | ||||
|  | ||||
|     public Diner(String name) { | ||||
|         this.name = name; | ||||
|         isHungry = true; | ||||
|     } | ||||
|  | ||||
|     public String getName() { | ||||
|         return name; | ||||
|     } | ||||
|  | ||||
|     public boolean isHungry() { | ||||
|         return isHungry; | ||||
|     } | ||||
|  | ||||
|     public void eatWith(Spoon spoon, Diner spouse) { | ||||
|         while (isHungry) { | ||||
|             if (spoon.getOwner() == this) { | ||||
|                 if (spouse.isHungry()) { | ||||
|                     System.out.println(getName() + ": You eat first my darling " + spouse.getName() + "!"); | ||||
|                     spoon.setOwner(spouse); | ||||
|                 } else { | ||||
|                     spoon.use(); | ||||
|                     this.isHungry = false; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										20
									
								
								src/exercises/ex_j/LivelockDinner.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								src/exercises/ex_j/LivelockDinner.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | ||||
| package exercises.ex_j; | ||||
|  | ||||
| public class LivelockDinner { | ||||
|     public static void main(String[] args) { | ||||
|         final Diner husband = new Diner("Bob"); | ||||
|         final Diner wife = new Diner("Alice"); | ||||
|         final Spoon spoon = new Spoon(husband); | ||||
|  | ||||
|         new Thread(new Runnable() { | ||||
|             public void run() { | ||||
|                 husband.eatWith(spoon, wife); | ||||
|             } | ||||
|         }).start(); | ||||
|         new Thread(new Runnable() { | ||||
|             public void run() { | ||||
|                 wife.eatWith(spoon, husband); | ||||
|             } | ||||
|         }).start(); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										21
									
								
								src/exercises/ex_j/Spoon.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								src/exercises/ex_j/Spoon.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | ||||
| package exercises.ex_j; | ||||
|  | ||||
| class Spoon { | ||||
|     private Diner owner; | ||||
|  | ||||
|     public synchronized Diner getOwner() { | ||||
|         return owner; | ||||
|     } | ||||
|  | ||||
|     public Spoon(Diner d) { | ||||
|         owner = d; | ||||
|     } | ||||
|  | ||||
|     public synchronized void setOwner(Diner d) { | ||||
|         owner = d; | ||||
|     } | ||||
|  | ||||
|     public synchronized void use() { | ||||
|         System.out.printf("%s has eaten!", owner.getName()); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user