diff --git a/mutex/README.md b/mutex/README.md deleted file mode 100644 index 997c37a35..000000000 --- a/mutex/README.md +++ /dev/null @@ -1,31 +0,0 @@ ---- -layout: pattern -title: Mutex -folder: mutex -permalink: /patterns/mutex/ -categories: Concurrency -tags: - - Decoupling ---- - -## Also known as - -* Mutual Exclusion Lock -* Binary Semaphore - -## Intent -Create a lock which only allows a single thread to access a resource at any one instant. - -## Class diagram -![alt text](./etc/mutex.png "Mutex") - -## Applicability -Use a Mutex when - -* You need to prevent two threads accessing a critical section at the same time -* Concurrent access to a resource could lead to a race condition - -## Credits - -* [Lock (computer science)](http://en.wikipedia.org/wiki/Lock_(computer_science)) -* [Semaphores](http://tutorials.jenkov.com/java-concurrency/semaphores.html) diff --git a/mutex/etc/mutex.png b/mutex/etc/mutex.png deleted file mode 100644 index 3b7c966f8..000000000 Binary files a/mutex/etc/mutex.png and /dev/null differ diff --git a/mutex/etc/mutex.urm.puml b/mutex/etc/mutex.urm.puml deleted file mode 100644 index 08cf43b90..000000000 --- a/mutex/etc/mutex.urm.puml +++ /dev/null @@ -1,27 +0,0 @@ -@startuml -package com.iluwatar.mutex { - class App { - + App() - + main(args : String[]) {static} - } - class Jar { - - beans : int - - lock : Lock - + Jar(beans : int, lock : Lock) - + takeBean() : boolean - } - interface Lock { - + acquire() {abstract} - + release() {abstract} - } - class Mutex { - - owner : Object - + Mutex() - + acquire() - + getOwner() : Object - + release() - } -} -Jar --> "-lock" Lock -Mutex ..|> Lock -@enduml \ No newline at end of file diff --git a/mutex/pom.xml b/mutex/pom.xml deleted file mode 100644 index 84455abb1..000000000 --- a/mutex/pom.xml +++ /dev/null @@ -1,61 +0,0 @@ - - - - 4.0.0 - - com.iluwatar - java-design-patterns - 1.24.0-SNAPSHOT - - mutex - - - org.junit.jupiter - junit-jupiter-engine - test - - - - - - org.apache.maven.plugins - maven-assembly-plugin - - - - - - com.iluwatar.mutex.App - - - - - - - - - diff --git a/mutex/src/main/java/com/iluwatar/mutex/App.java b/mutex/src/main/java/com/iluwatar/mutex/App.java deleted file mode 100644 index c50acc65a..000000000 --- a/mutex/src/main/java/com/iluwatar/mutex/App.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * The MIT License - * Copyright © 2014-2019 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; - -/** - * A Mutex prevents multiple threads from accessing a resource simultaneously. - * - *

In this example we have two thieves who are taking beans from a jar. Only one thief can take - * a bean at a time. This is ensured by a Mutex lock which must be acquired in order to access the - * jar. Each thief attempts to acquire the lock, take a bean and then release the lock. If the lock - * has already been acquired, the thief will be prevented from continuing (blocked) until the lock - * has been released. The thieves stop taking beans once there are no beans left to take. - */ -public class App { - - /** - * main method. - */ - public static void main(String[] args) { - var mutex = new Mutex(); - var jar = new Jar(1000, mutex); - var peter = new Thief("Peter", jar); - var john = new Thief("John", jar); - peter.start(); - john.start(); - } - -} diff --git a/mutex/src/main/java/com/iluwatar/mutex/Jar.java b/mutex/src/main/java/com/iluwatar/mutex/Jar.java deleted file mode 100644 index 4a0861e1a..000000000 --- a/mutex/src/main/java/com/iluwatar/mutex/Jar.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * The MIT License - * Copyright © 2014-2019 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; - -/** - * A Jar has a resource of beans which can only be accessed by a single Thief (thread) at any one - * time. A Mutex lock is used to prevent more than one Thief taking a bean simultaneously. - */ -public class Jar { - - /** - * The lock which must be acquired to access the beans resource. - */ - private final Lock lock; - - /** - * The resource within the jar. - */ - private int beans; - - public Jar(int beans, Lock lock) { - this.beans = beans; - this.lock = lock; - } - - /** - * Method for a thief to take a bean. - */ - public boolean takeBean() { - var success = false; - try { - lock.acquire(); - success = beans > 0; - if (success) { - beans = beans - 1; - } - } catch (Exception e) { - e.printStackTrace(); - } finally { - lock.release(); - } - - return success; - } - -} diff --git a/mutex/src/main/java/com/iluwatar/mutex/Lock.java b/mutex/src/main/java/com/iluwatar/mutex/Lock.java deleted file mode 100644 index bd28c3c08..000000000 --- a/mutex/src/main/java/com/iluwatar/mutex/Lock.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * The MIT License - * Copyright © 2014-2019 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 is an interface for a lock which can be acquired and released. - */ -public interface Lock { - - void acquire() throws InterruptedException; - - void release(); - -} diff --git a/mutex/src/main/java/com/iluwatar/mutex/Mutex.java b/mutex/src/main/java/com/iluwatar/mutex/Mutex.java deleted file mode 100644 index 6c62cc8ea..000000000 --- a/mutex/src/main/java/com/iluwatar/mutex/Mutex.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * The MIT License - * Copyright © 2014-2019 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 is an implementation of a mutual exclusion lock. - */ -public class Mutex implements Lock { - - /** - * The current owner of the lock. - */ - private Object owner; - - /** - * Returns the current owner of the Mutex, or null if available. - */ - public Object getOwner() { - return owner; - } - - /** - * Method called by a thread to acquire the lock. If the lock has already been acquired this will - * wait until the lock has been released to re-attempt the acquire. - */ - @Override - public synchronized void acquire() throws InterruptedException { - while (owner != null) { - wait(); - } - - owner = Thread.currentThread(); - } - - /** - * Method called by a thread to release the lock. - */ - @Override - public synchronized void release() { - if (Thread.currentThread() == owner) { - owner = null; - notify(); - } - } - -} diff --git a/mutex/src/main/java/com/iluwatar/mutex/Thief.java b/mutex/src/main/java/com/iluwatar/mutex/Thief.java deleted file mode 100644 index a9a715970..000000000 --- a/mutex/src/main/java/com/iluwatar/mutex/Thief.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * The MIT License - * Copyright © 2014-2019 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; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Thief is a class which continually tries to acquire a jar and take a bean from it. When the jar - * is empty the thief stops. - */ -public class Thief extends Thread { - - private static final Logger LOGGER = LoggerFactory.getLogger(Thief.class); - - /** - * The name of the thief. - */ - private final String name; - - /** - * The jar. - */ - private final Jar jar; - - public Thief(String name, Jar jar) { - this.name = name; - this.jar = jar; - } - - /** - * In the run method the thief repeatedly tries to take a bean until none are left. - */ - @Override - public void run() { - var beans = 0; - - while (jar.takeBean()) { - beans = beans + 1; - LOGGER.info("{} took a bean.", name); - } - - LOGGER.info("{} took {} beans.", name, beans); - } - -} diff --git a/mutex/src/test/java/com/iluwatar/mutex/AppTest.java b/mutex/src/test/java/com/iluwatar/mutex/AppTest.java deleted file mode 100644 index 7866b22a8..000000000 --- a/mutex/src/test/java/com/iluwatar/mutex/AppTest.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * The MIT License - * Copyright © 2014-2019 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; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; - -/** - * Application Test Entrypoint - */ -class AppTest { - - @Test - void shouldExecuteApplicationWithoutException() { - assertDoesNotThrow(() -> App.main(new String[]{})); - } -} diff --git a/mutex/src/test/java/com/iluwatar/mutex/JarTest.java b/mutex/src/test/java/com/iluwatar/mutex/JarTest.java deleted file mode 100644 index 786f96e44..000000000 --- a/mutex/src/test/java/com/iluwatar/mutex/JarTest.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * The MIT License - * Copyright © 2014-2019 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; - -import static org.junit.jupiter.api.Assertions.assertFalse; - -import java.util.stream.IntStream; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -/** - * Test case for taking beans from a Jar - */ -public class JarTest { - - @Test - public void testTakeBeans() { - var mutex = new Mutex(); - var jar = new Jar(10, mutex); - IntStream.range(0, 10).mapToObj(i -> jar.takeBean()).forEach(Assertions::assertTrue); - assertFalse(jar.takeBean()); - } - -} diff --git a/mutex/src/test/java/com/iluwatar/mutex/MutexTest.java b/mutex/src/test/java/com/iluwatar/mutex/MutexTest.java deleted file mode 100644 index d6d0cc1d7..000000000 --- a/mutex/src/test/java/com/iluwatar/mutex/MutexTest.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * The MIT License - * Copyright © 2014-2019 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; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.fail; - -import org.junit.jupiter.api.Test; - -/** - * Test case for acquiring and releasing a Mutex - */ -public class MutexTest { - - @Test - public void acquireReleaseTest() { - var mutex = new Mutex(); - assertNull(mutex.getOwner()); - try { - mutex.acquire(); - assertEquals(mutex.getOwner(), Thread.currentThread()); - } catch (InterruptedException e) { - fail(e.toString()); - } - mutex.release(); - assertNull(mutex.getOwner()); - } - -} \ No newline at end of file diff --git a/pom.xml b/pom.xml index eee0cb154..a3fb757a3 100644 --- a/pom.xml +++ b/pom.xml @@ -141,8 +141,6 @@ module monad mute-idiom - mutex - semaphore hexagonal abstract-document aggregator-microservices diff --git a/semaphore/README.md b/semaphore/README.md deleted file mode 100644 index be882e04a..000000000 --- a/semaphore/README.md +++ /dev/null @@ -1,33 +0,0 @@ ---- -layout: pattern -title: Semaphore -folder: semaphore -permalink: /patterns/semaphore/ -categories: Concurrency -tags: - - Performance ---- - -## Also known as -Counting Semaphore - -## Intent -Create a lock which mediates access to a pool of resources. -Only a limited number of threads, specified at the creation -of the semaphore, can access the resources at any given time. -A semaphore which only allows one concurrent access to a resource -is called a binary semaphore. - -## Class diagram -![alt text](./etc/semaphore.png "Semaphore") - -## Applicability -Use a Semaphore when - -* You have a pool of resources to allocate to different threads -* Concurrent access to a resource could lead to a race condition - -## Credits - -* [Semaphore(programming)] (http://en.wikipedia.org/wiki/Semaphore_(programming)) -* [Semaphores] (http://tutorials.jenkov.com/java-concurrency/semaphores.html) diff --git a/semaphore/etc/semaphore.png b/semaphore/etc/semaphore.png deleted file mode 100644 index a2ff6d0c4..000000000 Binary files a/semaphore/etc/semaphore.png and /dev/null differ diff --git a/semaphore/etc/semaphore.urm.puml b/semaphore/etc/semaphore.urm.puml deleted file mode 100644 index 168fd17e7..000000000 --- a/semaphore/etc/semaphore.urm.puml +++ /dev/null @@ -1,56 +0,0 @@ -@startuml -package com.iluwatar.semaphore { - class App { - + App() - + main(args : String[]) {static} - } - class Fruit { - - type : FruitType - + Fruit(type : FruitType) - + getType() : FruitType - + toString() : String - } - enum FruitType { - + APPLE {static} - + LEMON {static} - + ORANGE {static} - + valueOf(name : String) : FruitType {static} - + values() : FruitType[] {static} - } - class FruitBowl { - - fruit : List - + FruitBowl() - + countFruit() : int - + put(f : Fruit) - + take() : Fruit - + toString() : String - } - class FruitShop { - - available : boolean[] - - bowls : FruitBowl[] - - semaphore : Semaphore - + FruitShop() - + countFruit() : int - + returnBowl(bowl : FruitBowl) - + takeBowl() : FruitBowl - } - interface Lock { - + acquire() {abstract} - + release() {abstract} - } - class Semaphore { - - counter : int - - licenses : int - + Semaphore(licenses : int) - + acquire() - + getAvailableLicenses() : int - + getNumLicenses() : int - + release() - } -} -FruitType ..+ Fruit -Fruit --> "-type" FruitType -FruitShop --> "-semaphore" Semaphore -FruitBowl --> "-fruit" Fruit -Semaphore ..|> Lock -@enduml \ No newline at end of file diff --git a/semaphore/pom.xml b/semaphore/pom.xml deleted file mode 100644 index 64fd44db6..000000000 --- a/semaphore/pom.xml +++ /dev/null @@ -1,61 +0,0 @@ - - - - 4.0.0 - - com.iluwatar - java-design-patterns - 1.24.0-SNAPSHOT - - semaphore - - - org.junit.jupiter - junit-jupiter-engine - test - - - - - - org.apache.maven.plugins - maven-assembly-plugin - - - - - - com.iluwatar.semaphore.App - - - - - - - - - diff --git a/semaphore/src/main/java/com/iluwatar/semaphore/App.java b/semaphore/src/main/java/com/iluwatar/semaphore/App.java deleted file mode 100644 index ebb35037f..000000000 --- a/semaphore/src/main/java/com/iluwatar/semaphore/App.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * The MIT License - * Copyright © 2014-2019 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; - -/** - * A Semaphore mediates access by a group of threads to a pool of resources. - * - *

In this example a group of customers are taking fruit from a fruit shop. There is a bowl each - * of apples, oranges and lemons. Only one customer can access a bowl simultaneously. A Semaphore is - * used to indicate how many resources are currently available and must be acquired in order for a - * bowl to be given to a customer. Customers continually try to take fruit until there is no fruit - * left in the shop. - */ -public class App { - - /** - * main method. - */ - public static void main(String[] args) { - var 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(); - } - -} diff --git a/semaphore/src/main/java/com/iluwatar/semaphore/Customer.java b/semaphore/src/main/java/com/iluwatar/semaphore/Customer.java deleted file mode 100644 index 47f503a06..000000000 --- a/semaphore/src/main/java/com/iluwatar/semaphore/Customer.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * The MIT License - * Copyright © 2014-2019 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 org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * A Customer attempts to repeatedly take Fruit from the FruitShop by taking Fruit from FruitBowl - * instances. - */ -public class Customer extends Thread { - - private static final Logger LOGGER = LoggerFactory.getLogger(Customer.class); - - /** - * Name of the Customer. - */ - private final String name; - - /** - * The FruitShop he is using. - */ - private final FruitShop fruitShop; - - /** - * Their bowl of Fruit. - */ - private final FruitBowl fruitBowl; - - /** - * Customer constructor. - */ - public Customer(String name, FruitShop fruitShop) { - this.name = name; - this.fruitShop = fruitShop; - this.fruitBowl = new FruitBowl(); - } - - /** - * The Customer repeatedly takes Fruit from the FruitShop until no Fruit remains. - */ - public void run() { - - while (fruitShop.countFruit() > 0) { - var bowl = fruitShop.takeBowl(); - if (bowl != null) { - var fruit = bowl.take(); - if (fruit != null) { - LOGGER.info("{} took an {}", name, fruit); - fruitBowl.put(fruit); - fruitShop.returnBowl(bowl); - } - } - } - - LOGGER.info("{} took {}", name, fruitBowl); - - } - -} diff --git a/semaphore/src/main/java/com/iluwatar/semaphore/Fruit.java b/semaphore/src/main/java/com/iluwatar/semaphore/Fruit.java deleted file mode 100644 index 1f4026b92..000000000 --- a/semaphore/src/main/java/com/iluwatar/semaphore/Fruit.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * The MIT License - * Copyright © 2014-2019 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 is a resource stored in a FruitBowl. - */ -public class Fruit { - - /** - * Enumeration of Fruit Types. - */ - public enum FruitType { - ORANGE, APPLE, LEMON - } - - private final 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 ""; - } - } - -} diff --git a/semaphore/src/main/java/com/iluwatar/semaphore/FruitBowl.java b/semaphore/src/main/java/com/iluwatar/semaphore/FruitBowl.java deleted file mode 100644 index 5c2901efe..000000000 --- a/semaphore/src/main/java/com/iluwatar/semaphore/FruitBowl.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * The MIT License - * Copyright © 2014-2019 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; -import java.util.List; - -/** - * A FruitBowl contains Fruit. - */ -public class FruitBowl { - - private final List fruit = new ArrayList<>(); - - /** - * Returns the amount of fruits left in bowl. - * - * @return The amount of Fruit left in the bowl. - */ - public int countFruit() { - return fruit.size(); - } - - /** - * Put an item of Fruit into the bowl. - * - * @param f fruit - */ - public void put(Fruit f) { - fruit.add(f); - } - - /** - * Take an item of Fruit out of the bowl. - * - * @return The Fruit taken out of the bowl, or null if empty. - */ - public Fruit take() { - if (fruit.isEmpty()) { - return null; - } else { - return fruit.remove(0); - } - } - - /** - * toString method. - */ - public String toString() { - var apples = 0; - var oranges = 0; - var lemons = 0; - - for (var 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"; - } -} diff --git a/semaphore/src/main/java/com/iluwatar/semaphore/FruitShop.java b/semaphore/src/main/java/com/iluwatar/semaphore/FruitShop.java deleted file mode 100644 index c74145610..000000000 --- a/semaphore/src/main/java/com/iluwatar/semaphore/FruitShop.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * The MIT License - * Copyright © 2014-2019 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; - -/** - * A FruitShop contains three FruitBowl instances and controls access to them. - */ -public class FruitShop { - - /** - * The FruitBowl instances stored in the class. - */ - private final FruitBowl[] bowls = { - new FruitBowl(), - new FruitBowl(), - new FruitBowl() - }; - - /** - * Access flags for each of the FruitBowl instances. - */ - private final boolean[] available = { - true, - true, - true - }; - - /** - * The Semaphore that controls access to the class resources. - */ - private final Semaphore semaphore; - - /** - * FruitShop constructor. - */ - public FruitShop() { - for (var 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); - } - - /** - * Returns the amount of fruits left in shop. - * - * @return The amount of Fruit left in the shop. - */ - public synchronized int countFruit() { - return bowls[0].countFruit() + bowls[1].countFruit() + bowls[2].countFruit(); - } - - /** - * Method called by Customer to get a FruitBowl from the shop. This method will try to acquire the - * Semaphore before returning the first available FruitBowl. - */ - 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; - } - - /** - * Method called by a Customer instance to return a FruitBowl to the shop. This method releases - * the Semaphore, making the FruitBowl available to another Customer. - */ - 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; - } - } - -} diff --git a/semaphore/src/main/java/com/iluwatar/semaphore/Lock.java b/semaphore/src/main/java/com/iluwatar/semaphore/Lock.java deleted file mode 100644 index 5679e2a05..000000000 --- a/semaphore/src/main/java/com/iluwatar/semaphore/Lock.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * The MIT License - * Copyright © 2014-2019 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 is an interface for a lock which can be acquired and released. - */ -public interface Lock { - - void acquire() throws InterruptedException; - - void release(); - -} diff --git a/semaphore/src/main/java/com/iluwatar/semaphore/Semaphore.java b/semaphore/src/main/java/com/iluwatar/semaphore/Semaphore.java deleted file mode 100644 index 48b008fdd..000000000 --- a/semaphore/src/main/java/com/iluwatar/semaphore/Semaphore.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * The MIT License - * Copyright © 2014-2019 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 is an implementation of a semaphore lock. - */ -public class Semaphore implements Lock { - - private final int licenses; - /** - * The number of concurrent resource accesses which are allowed. - */ - private int counter; - - public Semaphore(int licenses) { - this.licenses = licenses; - this.counter = licenses; - } - - /** - * Returns the number of licenses managed by the Semaphore. - */ - public int getNumLicenses() { - return licenses; - } - - /** - * Returns the number of available licenses. - */ - public int getAvailableLicenses() { - return counter; - } - - /** - * Method called by a thread to acquire the lock. If there are no resources available this will - * wait until the lock has been released to re-attempt the acquire. - */ - public synchronized void acquire() throws InterruptedException { - while (counter == 0) { - wait(); - } - counter = counter - 1; - } - - /** - * Method called by a thread to release the lock. - */ - public synchronized void release() { - if (counter < licenses) { - counter = counter + 1; - notify(); - } - } - -} diff --git a/semaphore/src/test/java/com/iluwatar/semaphore/AppTest.java b/semaphore/src/test/java/com/iluwatar/semaphore/AppTest.java deleted file mode 100644 index 302796238..000000000 --- a/semaphore/src/test/java/com/iluwatar/semaphore/AppTest.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * The MIT License - * Copyright © 2014-2019 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 org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; - -/** - * Application Test Entrypoint - */ -class AppTest { - - @Test - void shouldExecuteWithoutException() { - assertDoesNotThrow(() -> App.main(new String[]{})); - } -} diff --git a/semaphore/src/test/java/com/iluwatar/semaphore/FruitBowlTest.java b/semaphore/src/test/java/com/iluwatar/semaphore/FruitBowlTest.java deleted file mode 100644 index bc21f9196..000000000 --- a/semaphore/src/test/java/com/iluwatar/semaphore/FruitBowlTest.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * The MIT License - * Copyright © 2014-2019 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 static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; - -import org.junit.jupiter.api.Test; - -/** - * Test taking from and putting Fruit into a FruitBowl - */ -public class FruitBowlTest { - - @Test - public void fruitBowlTest() { - var fbowl = new FruitBowl(); - - assertEquals(0, fbowl.countFruit()); - - for (var i = 1; i <= 10; i++) { - fbowl.put(new Fruit(Fruit.FruitType.LEMON)); - assertEquals(i, fbowl.countFruit()); - } - - for (var i = 9; i >= 0; i--) { - assertNotNull(fbowl.take()); - assertEquals(i, fbowl.countFruit()); - } - - assertNull(fbowl.take()); - } -} diff --git a/semaphore/src/test/java/com/iluwatar/semaphore/SemaphoreTest.java b/semaphore/src/test/java/com/iluwatar/semaphore/SemaphoreTest.java deleted file mode 100644 index 6d64066fb..000000000 --- a/semaphore/src/test/java/com/iluwatar/semaphore/SemaphoreTest.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * The MIT License - * Copyright © 2014-2019 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 static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; - -import org.junit.jupiter.api.Test; - -/** - * Test case for acquiring and releasing a Semaphore - */ -public class SemaphoreTest { - - @Test - public void acquireReleaseTest() { - var sphore = new Semaphore(3); - - assertEquals(3, sphore.getAvailableLicenses()); - - for (var i = 2; i >= 0; i--) { - try { - sphore.acquire(); - assertEquals(i, sphore.getAvailableLicenses()); - } catch (InterruptedException e) { - fail(e.toString()); - } - } - - for (var i = 1; i <= 3; i++) { - sphore.release(); - assertEquals(i, sphore.getAvailableLicenses()); - } - - sphore.release(); - assertEquals(3, sphore.getAvailableLicenses()); - } -}