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:
gwildor28
2016-03-07 19:40:50 +00:00
parent 35d6a54831
commit 3ed3bc1fa5
15 changed files with 735 additions and 0 deletions

View 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();
}
}

View 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);
}
}

View 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 "";
}
}
}

View File

@ -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";
}
}

View 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;
}
}
}

View 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();
}

View 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.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();
}
}