Added mutex and semaphore modules to demonstrate locks
Added two modules to demonstrate locks. Mutex demonstrates a simple mutual exclusion lock. Semaphore demonstrates a semaphore for controlling access to a pool of resources. The main class of both programs is App.java.
This commit is contained in:
		
							
								
								
									
										35
									
								
								mutex/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								mutex/pom.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,35 @@ | |||||||
|  | <?xml version="1.0"?> | ||||||
|  | <!-- | ||||||
|  |  | ||||||
|  |     The MIT License | ||||||
|  |     Copyright (c) 2014 Ilkka Sepp<70>l<EFBFBD> | ||||||
|  |  | ||||||
|  |     Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||
|  |     of this software and associated documentation files (the "Software"), to deal | ||||||
|  |     in the Software without restriction, including without limitation the rights | ||||||
|  |     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||
|  |     copies of the Software, and to permit persons to whom the Software is | ||||||
|  |     furnished to do so, subject to the following conditions: | ||||||
|  |  | ||||||
|  |     The above copyright notice and this permission notice shall be included in | ||||||
|  |     all copies or substantial portions of the Software. | ||||||
|  |  | ||||||
|  |     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||||
|  |     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||||
|  |     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||||
|  |     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||||
|  |     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||||
|  |     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||||
|  |     THE SOFTWARE. | ||||||
|  |  | ||||||
|  | --> | ||||||
|  | <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" | ||||||
|  |     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||||||
|  |   <modelVersion>4.0.0</modelVersion> | ||||||
|  |   <parent> | ||||||
|  |     <groupId>com.iluwatar</groupId> | ||||||
|  |     <artifactId>java-design-patterns</artifactId> | ||||||
|  |     <version>1.11.0-SNAPSHOT</version> | ||||||
|  |   </parent> | ||||||
|  |   <artifactId>mutex</artifactId> | ||||||
|  | </project> | ||||||
							
								
								
									
										42
									
								
								mutex/src/main/java/com/iluwatar/mutex/App.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								mutex/src/main/java/com/iluwatar/mutex/App.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | |||||||
|  | /** | ||||||
|  |  * The MIT License | ||||||
|  |  * Copyright (c) 2014 Ilkka Seppälä | ||||||
|  |  * | ||||||
|  |  * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||
|  |  * of this software and associated documentation files (the "Software"), to deal | ||||||
|  |  * in the Software without restriction, including without limitation the rights | ||||||
|  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||
|  |  * copies of the Software, and to permit persons to whom the Software is | ||||||
|  |  * furnished to do so, subject to the following conditions: | ||||||
|  |  * | ||||||
|  |  * The above copyright notice and this permission notice shall be included in | ||||||
|  |  * all copies or substantial portions of the Software. | ||||||
|  |  * | ||||||
|  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||||
|  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||||
|  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||||
|  |  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||||
|  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||||
|  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||||
|  |  * THE SOFTWARE. | ||||||
|  |  */ | ||||||
|  | package com.iluwatar.mutex; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * App. | ||||||
|  |  */ | ||||||
|  | public class App { | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * main method | ||||||
|  |    */ | ||||||
|  |   public static void main(String[] args) { | ||||||
|  |     Mutex mutex = new Mutex(); | ||||||
|  |     Jar jar = new Jar(mutex); | ||||||
|  |     Thief peter = new Thief("Peter", jar); | ||||||
|  |     Thief john = new Thief("John", jar); | ||||||
|  |     peter.start(); | ||||||
|  |     john.start(); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  | } | ||||||
							
								
								
									
										58
									
								
								mutex/src/main/java/com/iluwatar/mutex/Jar.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								mutex/src/main/java/com/iluwatar/mutex/Jar.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,58 @@ | |||||||
|  | /** | ||||||
|  |  * The MIT License | ||||||
|  |  * Copyright (c) 2014 Ilkka Seppälä | ||||||
|  |  * | ||||||
|  |  * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||
|  |  * of this software and associated documentation files (the "Software"), to deal | ||||||
|  |  * in the Software without restriction, including without limitation the rights | ||||||
|  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||
|  |  * copies of the Software, and to permit persons to whom the Software is | ||||||
|  |  * furnished to do so, subject to the following conditions: | ||||||
|  |  * | ||||||
|  |  * The above copyright notice and this permission notice shall be included in | ||||||
|  |  * all copies or substantial portions of the Software. | ||||||
|  |  * | ||||||
|  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||||
|  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||||
|  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||||
|  |  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||||
|  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||||
|  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||||
|  |  * THE SOFTWARE. | ||||||
|  |  */ | ||||||
|  | package com.iluwatar.mutex; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Jar. | ||||||
|  |  */ | ||||||
|  | public class Jar { | ||||||
|  |  | ||||||
|  |   private Lock lock; | ||||||
|  |  | ||||||
|  |   private int beans = 1000; | ||||||
|  |  | ||||||
|  |   public Jar(Lock lock) { | ||||||
|  |     this.lock = lock; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * takeBean method | ||||||
|  |    */ | ||||||
|  |   public boolean takeBean(Thief thief) { | ||||||
|  |     boolean success = false; | ||||||
|  |     try { | ||||||
|  |       lock.acquire(); | ||||||
|  |       success = beans > 0; | ||||||
|  |       if (success) { | ||||||
|  |         beans = beans - 1; | ||||||
|  |       } | ||||||
|  |     } catch (Exception e) { | ||||||
|  |       e.printStackTrace(); | ||||||
|  |     } finally { | ||||||
|  |       lock.release(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     return success; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  | } | ||||||
							
								
								
									
										34
									
								
								mutex/src/main/java/com/iluwatar/mutex/Lock.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								mutex/src/main/java/com/iluwatar/mutex/Lock.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,34 @@ | |||||||
|  | /** | ||||||
|  |  * The MIT License | ||||||
|  |  * Copyright (c) 2014 Ilkka Seppälä | ||||||
|  |  * | ||||||
|  |  * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||
|  |  * of this software and associated documentation files (the "Software"), to deal | ||||||
|  |  * in the Software without restriction, including without limitation the rights | ||||||
|  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||
|  |  * copies of the Software, and to permit persons to whom the Software is | ||||||
|  |  * furnished to do so, subject to the following conditions: | ||||||
|  |  * | ||||||
|  |  * The above copyright notice and this permission notice shall be included in | ||||||
|  |  * all copies or substantial portions of the Software. | ||||||
|  |  * | ||||||
|  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||||
|  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||||
|  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||||
|  |  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||||
|  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||||
|  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||||
|  |  * THE SOFTWARE. | ||||||
|  |  */ | ||||||
|  | package com.iluwatar.mutex; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Lock. | ||||||
|  |  */ | ||||||
|  | public interface Lock { | ||||||
|  |  | ||||||
|  |   void acquire() throws InterruptedException; | ||||||
|  |  | ||||||
|  |   void release(); | ||||||
|  |  | ||||||
|  | } | ||||||
							
								
								
									
										46
									
								
								mutex/src/main/java/com/iluwatar/mutex/Mutex.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								mutex/src/main/java/com/iluwatar/mutex/Mutex.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,46 @@ | |||||||
|  | /** | ||||||
|  |  * The MIT License | ||||||
|  |  * Copyright (c) 2014 Ilkka Seppälä | ||||||
|  |  * | ||||||
|  |  * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||
|  |  * of this software and associated documentation files (the "Software"), to deal | ||||||
|  |  * in the Software without restriction, including without limitation the rights | ||||||
|  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||
|  |  * copies of the Software, and to permit persons to whom the Software is | ||||||
|  |  * furnished to do so, subject to the following conditions: | ||||||
|  |  * | ||||||
|  |  * The above copyright notice and this permission notice shall be included in | ||||||
|  |  * all copies or substantial portions of the Software. | ||||||
|  |  * | ||||||
|  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||||
|  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||||
|  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||||
|  |  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||||
|  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||||
|  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||||
|  |  * THE SOFTWARE. | ||||||
|  |  */ | ||||||
|  | package com.iluwatar.mutex; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Mutex. | ||||||
|  |  */ | ||||||
|  | public class Mutex implements Lock { | ||||||
|  |   private Object owner = null; | ||||||
|  |  | ||||||
|  |   @Override | ||||||
|  |   public synchronized void acquire() throws InterruptedException { | ||||||
|  |     while (owner != null) { | ||||||
|  |       wait(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     owner = Thread.currentThread(); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   @Override | ||||||
|  |   public synchronized void release() { | ||||||
|  |     owner = null; | ||||||
|  |     notify(); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  | } | ||||||
							
								
								
									
										51
									
								
								mutex/src/main/java/com/iluwatar/mutex/Thief.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								mutex/src/main/java/com/iluwatar/mutex/Thief.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,51 @@ | |||||||
|  | /** | ||||||
|  |  * The MIT License | ||||||
|  |  * Copyright (c) 2014 Ilkka Seppälä | ||||||
|  |  * | ||||||
|  |  * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||
|  |  * of this software and associated documentation files (the "Software"), to deal | ||||||
|  |  * in the Software without restriction, including without limitation the rights | ||||||
|  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||
|  |  * copies of the Software, and to permit persons to whom the Software is | ||||||
|  |  * furnished to do so, subject to the following conditions: | ||||||
|  |  * | ||||||
|  |  * The above copyright notice and this permission notice shall be included in | ||||||
|  |  * all copies or substantial portions of the Software. | ||||||
|  |  * | ||||||
|  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||||
|  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||||
|  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||||
|  |  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||||
|  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||||
|  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||||
|  |  * THE SOFTWARE. | ||||||
|  |  */ | ||||||
|  | package com.iluwatar.mutex; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Thief. | ||||||
|  |  */ | ||||||
|  | public class Thief extends Thread { | ||||||
|  |  | ||||||
|  |   private String name; | ||||||
|  |  | ||||||
|  |   private Jar jar; | ||||||
|  |  | ||||||
|  |   public Thief(String name, Jar jar) { | ||||||
|  |     this.name = name; | ||||||
|  |     this.jar = jar; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   @Override | ||||||
|  |   public void run() { | ||||||
|  |     int beans = 0; | ||||||
|  |  | ||||||
|  |     while (jar.takeBean(this)) { | ||||||
|  |       beans = beans + 1; | ||||||
|  |       System.out.println(name + " took a bean."); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     System.out.println(name + " took " + beans + " beans."); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  | } | ||||||
							
								
								
									
										2
									
								
								pom.xml
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								pom.xml
									
									
									
									
									
								
							| @@ -123,6 +123,8 @@ | |||||||
| 		<module>feature-toggle</module> | 		<module>feature-toggle</module> | ||||||
| 		<module>value-object</module> | 		<module>value-object</module> | ||||||
| 		<module>monad</module> | 		<module>monad</module> | ||||||
|  | 		<module>mutex</module> | ||||||
|  | 		<module>semaphore</module> | ||||||
| 	</modules> | 	</modules> | ||||||
|  |  | ||||||
| 	<dependencyManagement> | 	<dependencyManagement> | ||||||
|   | |||||||
							
								
								
									
										35
									
								
								semaphore/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								semaphore/pom.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,35 @@ | |||||||
|  | <?xml version="1.0"?> | ||||||
|  | <!-- | ||||||
|  |  | ||||||
|  |     The MIT License | ||||||
|  |     Copyright (c) 2014 Ilkka Sepp<70>l<EFBFBD> | ||||||
|  |  | ||||||
|  |     Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||
|  |     of this software and associated documentation files (the "Software"), to deal | ||||||
|  |     in the Software without restriction, including without limitation the rights | ||||||
|  |     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||
|  |     copies of the Software, and to permit persons to whom the Software is | ||||||
|  |     furnished to do so, subject to the following conditions: | ||||||
|  |  | ||||||
|  |     The above copyright notice and this permission notice shall be included in | ||||||
|  |     all copies or substantial portions of the Software. | ||||||
|  |  | ||||||
|  |     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||||
|  |     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||||
|  |     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||||
|  |     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||||
|  |     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||||
|  |     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||||
|  |     THE SOFTWARE. | ||||||
|  |  | ||||||
|  | --> | ||||||
|  | <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" | ||||||
|  |     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||||||
|  |   <modelVersion>4.0.0</modelVersion> | ||||||
|  |   <parent> | ||||||
|  |     <groupId>com.iluwatar</groupId> | ||||||
|  |     <artifactId>java-design-patterns</artifactId> | ||||||
|  |     <version>1.11.0-SNAPSHOT</version> | ||||||
|  |   </parent> | ||||||
|  |   <artifactId>semaphore</artifactId> | ||||||
|  | </project> | ||||||
							
								
								
									
										43
									
								
								semaphore/src/main/java/com/iluwatar/semaphore/App.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								semaphore/src/main/java/com/iluwatar/semaphore/App.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,43 @@ | |||||||
|  | /** | ||||||
|  |  * The MIT License | ||||||
|  |  * Copyright (c) 2014 Ilkka Seppälä | ||||||
|  |  * | ||||||
|  |  * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||
|  |  * of this software and associated documentation files (the "Software"), to deal | ||||||
|  |  * in the Software without restriction, including without limitation the rights | ||||||
|  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||
|  |  * copies of the Software, and to permit persons to whom the Software is | ||||||
|  |  * furnished to do so, subject to the following conditions: | ||||||
|  |  * | ||||||
|  |  * The above copyright notice and this permission notice shall be included in | ||||||
|  |  * all copies or substantial portions of the Software. | ||||||
|  |  * | ||||||
|  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||||
|  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||||
|  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||||
|  |  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||||
|  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||||
|  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||||
|  |  * THE SOFTWARE. | ||||||
|  |  */ | ||||||
|  | package com.iluwatar.semaphore; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * App. | ||||||
|  |  */ | ||||||
|  | public class App { | ||||||
|  |      | ||||||
|  |   /** | ||||||
|  |    * main method | ||||||
|  |    */ | ||||||
|  |   public static void main(String[] args) { | ||||||
|  |     FruitShop shop = new FruitShop(); | ||||||
|  |     new Customer("Peter", shop).start(); | ||||||
|  |     new Customer("Paul", shop).start(); | ||||||
|  |     new Customer("Mary", shop).start(); | ||||||
|  |     new Customer("John", shop).start(); | ||||||
|  |     new Customer("Ringo", shop).start(); | ||||||
|  |     new Customer("George", shop).start(); | ||||||
|  |   } | ||||||
|  |    | ||||||
|  | } | ||||||
							
								
								
									
										63
									
								
								semaphore/src/main/java/com/iluwatar/semaphore/Customer.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								semaphore/src/main/java/com/iluwatar/semaphore/Customer.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,63 @@ | |||||||
|  | /** | ||||||
|  |  * The MIT License | ||||||
|  |  * Copyright (c) 2014 Ilkka Seppälä | ||||||
|  |  * | ||||||
|  |  * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||
|  |  * of this software and associated documentation files (the "Software"), to deal | ||||||
|  |  * in the Software without restriction, including without limitation the rights | ||||||
|  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||
|  |  * copies of the Software, and to permit persons to whom the Software is | ||||||
|  |  * furnished to do so, subject to the following conditions: | ||||||
|  |  * | ||||||
|  |  * The above copyright notice and this permission notice shall be included in | ||||||
|  |  * all copies or substantial portions of the Software. | ||||||
|  |  * | ||||||
|  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||||
|  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||||
|  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||||
|  |  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||||
|  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||||
|  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||||
|  |  * THE SOFTWARE. | ||||||
|  |  */ | ||||||
|  | package com.iluwatar.semaphore; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Customer. | ||||||
|  |  */ | ||||||
|  | public class Customer extends Thread { | ||||||
|  |      | ||||||
|  |   private String name; | ||||||
|  |   private FruitShop fruitShop; | ||||||
|  |   private FruitBowl fruitBowl; | ||||||
|  |    | ||||||
|  |   /** | ||||||
|  |    * Customer constructor | ||||||
|  |    */ | ||||||
|  |   public Customer(String name, FruitShop fruitShop) { | ||||||
|  |     this.name = name; | ||||||
|  |     this.fruitShop = fruitShop; | ||||||
|  |     this.fruitBowl = new FruitBowl(); | ||||||
|  |   } | ||||||
|  |    | ||||||
|  |   /** | ||||||
|  |    * run method | ||||||
|  |    */    | ||||||
|  |   public void run() { | ||||||
|  |          | ||||||
|  |     while (fruitShop.countFruit() > 0) { | ||||||
|  |       FruitBowl bowl = fruitShop.takeBowl(); | ||||||
|  |       Fruit fruit; | ||||||
|  |              | ||||||
|  |       if (bowl != null && (fruit = bowl.take()) != null) { | ||||||
|  |         System.out.println(name + " took an " + fruit); | ||||||
|  |         fruitBowl.put(fruit); | ||||||
|  |         fruitShop.returnBowl(bowl); | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |          | ||||||
|  |     System.out.println(name + " took " + fruitBowl); | ||||||
|  |          | ||||||
|  |   } | ||||||
|  |      | ||||||
|  | } | ||||||
							
								
								
									
										60
									
								
								semaphore/src/main/java/com/iluwatar/semaphore/Fruit.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								semaphore/src/main/java/com/iluwatar/semaphore/Fruit.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,60 @@ | |||||||
|  | /** | ||||||
|  |  * The MIT License | ||||||
|  |  * Copyright (c) 2014 Ilkka Seppälä | ||||||
|  |  * | ||||||
|  |  * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||
|  |  * of this software and associated documentation files (the "Software"), to deal | ||||||
|  |  * in the Software without restriction, including without limitation the rights | ||||||
|  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||
|  |  * copies of the Software, and to permit persons to whom the Software is | ||||||
|  |  * furnished to do so, subject to the following conditions: | ||||||
|  |  * | ||||||
|  |  * The above copyright notice and this permission notice shall be included in | ||||||
|  |  * all copies or substantial portions of the Software. | ||||||
|  |  * | ||||||
|  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||||
|  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||||
|  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||||
|  |  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||||
|  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||||
|  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||||
|  |  * THE SOFTWARE. | ||||||
|  |  */ | ||||||
|  | package com.iluwatar.semaphore; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Fruit. | ||||||
|  |  */ | ||||||
|  | public class Fruit { | ||||||
|  |  | ||||||
|  |   public static enum FruitType { | ||||||
|  |     ORANGE, APPLE, LEMON | ||||||
|  |   } | ||||||
|  |      | ||||||
|  |   private FruitType type; | ||||||
|  |      | ||||||
|  |   public Fruit(FruitType type) { | ||||||
|  |     this.type = type; | ||||||
|  |   } | ||||||
|  |      | ||||||
|  |   public FruitType getType() { | ||||||
|  |     return type; | ||||||
|  |   } | ||||||
|  |    | ||||||
|  |   /** | ||||||
|  |    * toString method | ||||||
|  |    */  | ||||||
|  |   public String toString() { | ||||||
|  |     switch (type) { | ||||||
|  |       case ORANGE: | ||||||
|  |         return "Orange"; | ||||||
|  |       case APPLE:  | ||||||
|  |         return "Apple"; | ||||||
|  |       case LEMON:  | ||||||
|  |         return "Lemon"; | ||||||
|  |       default:  | ||||||
|  |         return ""; | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|  | } | ||||||
| @@ -0,0 +1,78 @@ | |||||||
|  | /** | ||||||
|  |  * The MIT License | ||||||
|  |  * Copyright (c) 2014 Ilkka Seppälä | ||||||
|  |  * | ||||||
|  |  * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||
|  |  * of this software and associated documentation files (the "Software"), to deal | ||||||
|  |  * in the Software without restriction, including without limitation the rights | ||||||
|  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||
|  |  * copies of the Software, and to permit persons to whom the Software is | ||||||
|  |  * furnished to do so, subject to the following conditions: | ||||||
|  |  * | ||||||
|  |  * The above copyright notice and this permission notice shall be included in | ||||||
|  |  * all copies or substantial portions of the Software. | ||||||
|  |  * | ||||||
|  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||||
|  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||||
|  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||||
|  |  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||||
|  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||||
|  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||||
|  |  * THE SOFTWARE. | ||||||
|  |  */ | ||||||
|  | package com.iluwatar.semaphore; | ||||||
|  |  | ||||||
|  | import java.util.ArrayList; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * FruitBowl. | ||||||
|  |  */ | ||||||
|  | public class FruitBowl { | ||||||
|  |      | ||||||
|  |   private ArrayList<Fruit> fruit = new ArrayList<>(); | ||||||
|  |  | ||||||
|  |   public int countFruit() { | ||||||
|  |     return fruit.size(); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   public void put(Fruit f) { | ||||||
|  |     fruit.add(f); | ||||||
|  |   } | ||||||
|  |    | ||||||
|  |   /** | ||||||
|  |    * take method | ||||||
|  |    */    | ||||||
|  |   public Fruit take() { | ||||||
|  |     if (fruit.isEmpty()) { | ||||||
|  |       return null; | ||||||
|  |     } else { | ||||||
|  |       return fruit.remove(0); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |    | ||||||
|  |   /** | ||||||
|  |    * toString method | ||||||
|  |    */    | ||||||
|  |   public String toString() { | ||||||
|  |     int apples = 0; | ||||||
|  |     int oranges = 0; | ||||||
|  |     int lemons = 0; | ||||||
|  |          | ||||||
|  |     for (Fruit f : fruit) { | ||||||
|  |       switch (f.getType()) { | ||||||
|  |         case APPLE: | ||||||
|  |           apples++; | ||||||
|  |           break; | ||||||
|  |         case ORANGE: | ||||||
|  |           oranges++; | ||||||
|  |           break; | ||||||
|  |         case LEMON: | ||||||
|  |           lemons++; | ||||||
|  |           break; | ||||||
|  |         default: | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |          | ||||||
|  |     return apples + " Apples, " + oranges + " Oranges, and " + lemons + " Lemons"; | ||||||
|  |   } | ||||||
|  | } | ||||||
							
								
								
									
										103
									
								
								semaphore/src/main/java/com/iluwatar/semaphore/FruitShop.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										103
									
								
								semaphore/src/main/java/com/iluwatar/semaphore/FruitShop.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,103 @@ | |||||||
|  | /** | ||||||
|  |  * The MIT License | ||||||
|  |  * Copyright (c) 2014 Ilkka Seppälä | ||||||
|  |  * | ||||||
|  |  * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||
|  |  * of this software and associated documentation files (the "Software"), to deal | ||||||
|  |  * in the Software without restriction, including without limitation the rights | ||||||
|  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||
|  |  * copies of the Software, and to permit persons to whom the Software is | ||||||
|  |  * furnished to do so, subject to the following conditions: | ||||||
|  |  * | ||||||
|  |  * The above copyright notice and this permission notice shall be included in | ||||||
|  |  * all copies or substantial portions of the Software. | ||||||
|  |  * | ||||||
|  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||||
|  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||||
|  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||||
|  |  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||||
|  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||||
|  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||||
|  |  * THE SOFTWARE. | ||||||
|  |  */ | ||||||
|  | package com.iluwatar.semaphore; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * FruitShop. | ||||||
|  |  */ | ||||||
|  | public class FruitShop { | ||||||
|  |      | ||||||
|  |   private FruitBowl[] bowls = { | ||||||
|  |     new FruitBowl(), | ||||||
|  |     new FruitBowl(), | ||||||
|  |     new FruitBowl() | ||||||
|  |   }; | ||||||
|  |      | ||||||
|  |   private boolean[] available = { | ||||||
|  |     true, | ||||||
|  |     true, | ||||||
|  |     true | ||||||
|  |   }; | ||||||
|  |      | ||||||
|  |   private Semaphore semaphore; | ||||||
|  |    | ||||||
|  |   /** | ||||||
|  |    * FruitShop constructor | ||||||
|  |    */ | ||||||
|  |   public FruitShop() { | ||||||
|  |     for (int i = 0; i < 100; i++) { | ||||||
|  |       bowls[0].put(new Fruit(Fruit.FruitType.APPLE)); | ||||||
|  |       bowls[1].put(new Fruit(Fruit.FruitType.ORANGE)); | ||||||
|  |       bowls[2].put(new Fruit(Fruit.FruitType.LEMON)); | ||||||
|  |     } | ||||||
|  |          | ||||||
|  |     semaphore = new Semaphore(3); | ||||||
|  |   } | ||||||
|  |      | ||||||
|  |   public synchronized int countFruit() { | ||||||
|  |     return bowls[0].countFruit() + bowls[1].countFruit() + bowls[2].countFruit(); | ||||||
|  |   } | ||||||
|  |    | ||||||
|  |   /** | ||||||
|  |    * takeBowl method | ||||||
|  |    */    | ||||||
|  |   public synchronized FruitBowl takeBowl() { | ||||||
|  |          | ||||||
|  |     FruitBowl bowl = null; | ||||||
|  |          | ||||||
|  |     try { | ||||||
|  |       semaphore.acquire(); | ||||||
|  |              | ||||||
|  |       if (available[0]) { | ||||||
|  |         bowl = bowls[0]; | ||||||
|  |         available[0] = false; | ||||||
|  |       } else if (available[1]) { | ||||||
|  |         bowl = bowls[1]; | ||||||
|  |         available[1] = false; | ||||||
|  |       } else if (available[2]) { | ||||||
|  |         bowl = bowls[2]; | ||||||
|  |         available[2] = false; | ||||||
|  |       } | ||||||
|  |              | ||||||
|  |     } catch (InterruptedException e) { | ||||||
|  |       e.printStackTrace(); | ||||||
|  |     } finally { | ||||||
|  |       semaphore.release(); | ||||||
|  |     } | ||||||
|  |     return bowl; | ||||||
|  |   } | ||||||
|  |    | ||||||
|  |   /** | ||||||
|  |    * returnBowl method | ||||||
|  |    */    | ||||||
|  |   public synchronized void returnBowl(FruitBowl bowl) { | ||||||
|  |     if (bowl == bowls[0]) { | ||||||
|  |       available[0] = true; | ||||||
|  |     } else if (bowl == bowls[1]) { | ||||||
|  |       available[1] = true; | ||||||
|  |     } else if (bowl == bowls[2]) { | ||||||
|  |       available [2] = true; | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |      | ||||||
|  | } | ||||||
							
								
								
									
										34
									
								
								semaphore/src/main/java/com/iluwatar/semaphore/Lock.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								semaphore/src/main/java/com/iluwatar/semaphore/Lock.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,34 @@ | |||||||
|  | /** | ||||||
|  |  * The MIT License | ||||||
|  |  * Copyright (c) 2014 Ilkka Seppälä | ||||||
|  |  * | ||||||
|  |  * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||
|  |  * of this software and associated documentation files (the "Software"), to deal | ||||||
|  |  * in the Software without restriction, including without limitation the rights | ||||||
|  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||
|  |  * copies of the Software, and to permit persons to whom the Software is | ||||||
|  |  * furnished to do so, subject to the following conditions: | ||||||
|  |  * | ||||||
|  |  * The above copyright notice and this permission notice shall be included in | ||||||
|  |  * all copies or substantial portions of the Software. | ||||||
|  |  * | ||||||
|  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||||
|  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||||
|  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||||
|  |  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||||
|  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||||
|  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||||
|  |  * THE SOFTWARE. | ||||||
|  |  */ | ||||||
|  | package com.iluwatar.semaphore; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Lock. | ||||||
|  |  */ | ||||||
|  | public interface Lock { | ||||||
|  |      | ||||||
|  |   void acquire() throws InterruptedException; | ||||||
|  |    | ||||||
|  |   void release(); | ||||||
|  |      | ||||||
|  | } | ||||||
| @@ -0,0 +1,51 @@ | |||||||
|  | /** | ||||||
|  |  * The MIT License | ||||||
|  |  * Copyright (c) 2014 Ilkka Seppälä | ||||||
|  |  * | ||||||
|  |  * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||
|  |  * of this software and associated documentation files (the "Software"), to deal | ||||||
|  |  * in the Software without restriction, including without limitation the rights | ||||||
|  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||
|  |  * copies of the Software, and to permit persons to whom the Software is | ||||||
|  |  * furnished to do so, subject to the following conditions: | ||||||
|  |  * | ||||||
|  |  * The above copyright notice and this permission notice shall be included in | ||||||
|  |  * all copies or substantial portions of the Software. | ||||||
|  |  * | ||||||
|  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||||
|  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||||
|  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||||
|  |  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||||
|  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||||
|  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||||
|  |  * THE SOFTWARE. | ||||||
|  |  */ | ||||||
|  | package com.iluwatar.semaphore; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Semaphore. | ||||||
|  |  */ | ||||||
|  | public class Semaphore implements Lock { | ||||||
|  |  | ||||||
|  |   private int counter; | ||||||
|  |      | ||||||
|  |   public Semaphore(int counter) { | ||||||
|  |     this.counter = counter;     | ||||||
|  |   } | ||||||
|  |    | ||||||
|  |   /** | ||||||
|  |    * acquire method | ||||||
|  |    */ | ||||||
|  |   public synchronized void acquire() throws InterruptedException { | ||||||
|  |     while (counter == 0) { | ||||||
|  |       wait(); | ||||||
|  |     } | ||||||
|  |     counter = counter - 1; | ||||||
|  |   } | ||||||
|  |    | ||||||
|  |   public synchronized void release() { | ||||||
|  |     counter = counter + 1; | ||||||
|  |     notify(); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user