diff --git a/mutex/pom.xml b/mutex/pom.xml
new file mode 100644
index 000000000..5b7058083
--- /dev/null
+++ b/mutex/pom.xml
@@ -0,0 +1,35 @@
+
+
+
+ 4.0.0
+
+ com.iluwatar
+ java-design-patterns
+ 1.11.0-SNAPSHOT
+
+ mutex
+
diff --git a/mutex/src/main/java/com/iluwatar/mutex/App.java b/mutex/src/main/java/com/iluwatar/mutex/App.java
new file mode 100644
index 000000000..e2d10763f
--- /dev/null
+++ b/mutex/src/main/java/com/iluwatar/mutex/App.java
@@ -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();
+ }
+
+}
diff --git a/mutex/src/main/java/com/iluwatar/mutex/Jar.java b/mutex/src/main/java/com/iluwatar/mutex/Jar.java
new file mode 100644
index 000000000..6cbe009d8
--- /dev/null
+++ b/mutex/src/main/java/com/iluwatar/mutex/Jar.java
@@ -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;
+ }
+
+}
diff --git a/mutex/src/main/java/com/iluwatar/mutex/Lock.java b/mutex/src/main/java/com/iluwatar/mutex/Lock.java
new file mode 100644
index 000000000..b748f3761
--- /dev/null
+++ b/mutex/src/main/java/com/iluwatar/mutex/Lock.java
@@ -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();
+
+}
diff --git a/mutex/src/main/java/com/iluwatar/mutex/Mutex.java b/mutex/src/main/java/com/iluwatar/mutex/Mutex.java
new file mode 100644
index 000000000..a8cf6a919
--- /dev/null
+++ b/mutex/src/main/java/com/iluwatar/mutex/Mutex.java
@@ -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();
+ }
+
+}
diff --git a/mutex/src/main/java/com/iluwatar/mutex/Thief.java b/mutex/src/main/java/com/iluwatar/mutex/Thief.java
new file mode 100644
index 000000000..8fbc75f92
--- /dev/null
+++ b/mutex/src/main/java/com/iluwatar/mutex/Thief.java
@@ -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.");
+ }
+
+}
diff --git a/pom.xml b/pom.xml
index 555844660..fa0fe771c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -123,6 +123,8 @@
feature-toggle
value-object
monad
+ mutex
+ semaphore
diff --git a/semaphore/pom.xml b/semaphore/pom.xml
new file mode 100644
index 000000000..66a961c3f
--- /dev/null
+++ b/semaphore/pom.xml
@@ -0,0 +1,35 @@
+
+
+
+ 4.0.0
+
+ com.iluwatar
+ java-design-patterns
+ 1.11.0-SNAPSHOT
+
+ semaphore
+
diff --git a/semaphore/src/main/java/com/iluwatar/semaphore/App.java b/semaphore/src/main/java/com/iluwatar/semaphore/App.java
new file mode 100644
index 000000000..4dd5a764a
--- /dev/null
+++ b/semaphore/src/main/java/com/iluwatar/semaphore/App.java
@@ -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();
+ }
+
+}
diff --git a/semaphore/src/main/java/com/iluwatar/semaphore/Customer.java b/semaphore/src/main/java/com/iluwatar/semaphore/Customer.java
new file mode 100644
index 000000000..a3fc65ac3
--- /dev/null
+++ b/semaphore/src/main/java/com/iluwatar/semaphore/Customer.java
@@ -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);
+
+ }
+
+}
diff --git a/semaphore/src/main/java/com/iluwatar/semaphore/Fruit.java b/semaphore/src/main/java/com/iluwatar/semaphore/Fruit.java
new file mode 100644
index 000000000..ecf8d1d29
--- /dev/null
+++ b/semaphore/src/main/java/com/iluwatar/semaphore/Fruit.java
@@ -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 "";
+ }
+ }
+
+}
diff --git a/semaphore/src/main/java/com/iluwatar/semaphore/FruitBowl.java b/semaphore/src/main/java/com/iluwatar/semaphore/FruitBowl.java
new file mode 100644
index 000000000..7760b8e3a
--- /dev/null
+++ b/semaphore/src/main/java/com/iluwatar/semaphore/FruitBowl.java
@@ -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 = 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";
+ }
+}
diff --git a/semaphore/src/main/java/com/iluwatar/semaphore/FruitShop.java b/semaphore/src/main/java/com/iluwatar/semaphore/FruitShop.java
new file mode 100644
index 000000000..fff764bad
--- /dev/null
+++ b/semaphore/src/main/java/com/iluwatar/semaphore/FruitShop.java
@@ -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;
+ }
+ }
+
+}
diff --git a/semaphore/src/main/java/com/iluwatar/semaphore/Lock.java b/semaphore/src/main/java/com/iluwatar/semaphore/Lock.java
new file mode 100644
index 000000000..99b8f85b5
--- /dev/null
+++ b/semaphore/src/main/java/com/iluwatar/semaphore/Lock.java
@@ -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();
+
+}
diff --git a/semaphore/src/main/java/com/iluwatar/semaphore/Semaphore.java b/semaphore/src/main/java/com/iluwatar/semaphore/Semaphore.java
new file mode 100644
index 000000000..5873df2b5
--- /dev/null
+++ b/semaphore/src/main/java/com/iluwatar/semaphore/Semaphore.java
@@ -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();
+ }
+
+}