Update according to review comments #397

- Added descriptions
- Added junit tests
- Added javadoc
- Added index.md
- Added class diagrams
This commit is contained in:
gwildor28
2016-03-24 18:13:37 +00:00
parent 3ed3bc1fa5
commit 6e4b269939
20 changed files with 273 additions and 34 deletions

View File

@ -23,7 +23,15 @@
package com.iluwatar.mutex;
/**
* App.
* A Mutex prevents multiple threads from accessing a resource simultaneously.
* <p>
* 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 {

View File

@ -23,12 +23,20 @@
package com.iluwatar.mutex;
/**
* Jar.
* 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 {
private Lock lock;
/**
* The lock which must be acquired to access the beans resource.
*/
private final Lock lock;
/**
* The resource within the jar.
*/
private int beans = 1000;
public Jar(Lock lock) {
@ -36,7 +44,7 @@ public class Jar {
}
/**
* takeBean method
* Method for a thief to take a bean.
*/
public boolean takeBean(Thief thief) {
boolean success = false;

View File

@ -23,7 +23,7 @@
package com.iluwatar.mutex;
/**
* Lock.
* Lock is an interface for a lock which can be acquired and released.
*/
public interface Lock {

View File

@ -23,11 +23,20 @@
package com.iluwatar.mutex;
/**
* Mutex.
* Mutex is an implementation of a mutual exclusion lock.
*/
public class Mutex implements Lock {
private Object owner = null;
/**
* The current owner of the lock.
*/
private Object 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) {
@ -37,6 +46,9 @@ public class Mutex implements Lock {
owner = Thread.currentThread();
}
/**
* Method called by a thread to release the lock.
*/
@Override
public synchronized void release() {
owner = null;

View File

@ -23,19 +23,30 @@
package com.iluwatar.mutex;
/**
* Thief.
* 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 String name;
private Jar jar;
/**
* 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() {
int beans = 0;

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.mutex;
import org.junit.Test;
import java.io.IOException;
public class AppTest{
@Test
public void test() throws IOException {
String[] args = {};
App.main(args);
}
}