Skip to content

Commit 6e4b269

Browse files
committed
Update according to review comments iluwatar#397
- Added descriptions - Added junit tests - Added javadoc - Added index.md - Added class diagrams
1 parent 3ed3bc1 commit 6e4b269

20 files changed

Lines changed: 273 additions & 34 deletions

File tree

mutex/etc/mutex.png

12.4 KB
Loading

mutex/index.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
layout: pattern
3+
title: Mutex
4+
folder: mutex
5+
permalink: /patterns/mutex/
6+
categories: Lock
7+
tags:
8+
- Java
9+
- Difficulty-Beginner
10+
---
11+
12+
## Also known as
13+
Mutual Exclusion Lock
14+
Binary Semaphore
15+
16+
## Intent
17+
Create a lock which only allows a single thread to access a resource at any one instant.
18+
19+
![alt text](./etc/mutex.png "Mutex")
20+
21+
## Applicability
22+
Use a Mutex when
23+
24+
* you need to prevent two threads accessing a critical section at the same time
25+
* concurrent access to a resource could lead to a race condition
26+
27+
## Credits
28+
29+
* [Lock (computer science)] (http://en.wikipedia.org/wiki/Lock_(computer_science))
30+
* [Semaphores] (http://tutorials.jenkov.com/java-concurrency/semaphores.html)

mutex/pom.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<!--
33
44
The MIT License
5-
Copyright (c) 2014 Ilkka Seppälä
5+
Copyright (c) 2014 Ilkka Sepp�l�
66
77
Permission is hereby granted, free of charge, to any person obtaining a copy
88
of this software and associated documentation files (the "Software"), to deal
@@ -32,4 +32,11 @@
3232
<version>1.11.0-SNAPSHOT</version>
3333
</parent>
3434
<artifactId>mutex</artifactId>
35+
<dependencies>
36+
<dependency>
37+
<groupId>junit</groupId>
38+
<artifactId>junit</artifactId>
39+
<scope>test</scope>
40+
</dependency>
41+
</dependencies>
3542
</project>

mutex/src/main/java/com/iluwatar/mutex/App.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,15 @@
2323
package com.iluwatar.mutex;
2424

2525
/**
26-
* App.
26+
* A Mutex prevents multiple threads from accessing a resource simultaneously.
27+
* <p>
28+
* In this example we have two thieves who are taking beans from a jar.
29+
* Only one thief can take a bean at a time. This is ensured by a Mutex lock
30+
* which must be acquired in order to access the jar. Each thief attempts to
31+
* acquire the lock, take a bean and then release the lock. If the lock has
32+
* already been acquired, the thief will be prevented from continuing (blocked)
33+
* until the lock has been released. The thieves stop taking beans once there
34+
* are no beans left to take.
2735
*/
2836
public class App {
2937

mutex/src/main/java/com/iluwatar/mutex/Jar.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,28 @@
2323
package com.iluwatar.mutex;
2424

2525
/**
26-
* Jar.
26+
* A Jar has a resource of beans which can only be accessed by a single Thief
27+
* (thread) at any one time. A Mutex lock is used to prevent more than one Thief
28+
* taking a bean simultaneously.
2729
*/
2830
public class Jar {
2931

30-
private Lock lock;
32+
/**
33+
* The lock which must be acquired to access the beans resource.
34+
*/
35+
private final Lock lock;
3136

37+
/**
38+
* The resource within the jar.
39+
*/
3240
private int beans = 1000;
3341

3442
public Jar(Lock lock) {
3543
this.lock = lock;
3644
}
3745

3846
/**
39-
* takeBean method
47+
* Method for a thief to take a bean.
4048
*/
4149
public boolean takeBean(Thief thief) {
4250
boolean success = false;

mutex/src/main/java/com/iluwatar/mutex/Lock.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
package com.iluwatar.mutex;
2424

2525
/**
26-
* Lock.
26+
* Lock is an interface for a lock which can be acquired and released.
2727
*/
2828
public interface Lock {
2929

mutex/src/main/java/com/iluwatar/mutex/Mutex.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,20 @@
2323
package com.iluwatar.mutex;
2424

2525
/**
26-
* Mutex.
26+
* Mutex is an implementation of a mutual exclusion lock.
2727
*/
2828
public class Mutex implements Lock {
29-
private Object owner = null;
3029

30+
/**
31+
* The current owner of the lock.
32+
*/
33+
private Object owner;
34+
35+
/**
36+
* Method called by a thread to acquire the lock. If the lock has already
37+
* been acquired this will wait until the lock has been released to
38+
* re-attempt the acquire.
39+
*/
3140
@Override
3241
public synchronized void acquire() throws InterruptedException {
3342
while (owner != null) {
@@ -37,6 +46,9 @@ public synchronized void acquire() throws InterruptedException {
3746
owner = Thread.currentThread();
3847
}
3948

49+
/**
50+
* Method called by a thread to release the lock.
51+
*/
4052
@Override
4153
public synchronized void release() {
4254
owner = null;

mutex/src/main/java/com/iluwatar/mutex/Thief.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,30 @@
2323
package com.iluwatar.mutex;
2424

2525
/**
26-
* Thief.
26+
* Thief is a class which continually tries to acquire a jar and take a bean
27+
* from it. When the jar is empty the thief stops.
2728
*/
2829
public class Thief extends Thread {
2930

30-
private String name;
31-
32-
private Jar jar;
31+
/**
32+
* The name of the thief.
33+
*/
34+
private final String name;
35+
36+
/**
37+
* The jar
38+
*/
39+
private final Jar jar;
3340

3441
public Thief(String name, Jar jar) {
3542
this.name = name;
3643
this.jar = jar;
3744
}
3845

46+
/**
47+
* In the run method the thief repeatedly tries to take a bean until none
48+
* are left.
49+
*/
3950
@Override
4051
public void run() {
4152
int beans = 0;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.mutex;
24+
25+
import org.junit.Test;
26+
import java.io.IOException;
27+
28+
public class AppTest{
29+
@Test
30+
public void test() throws IOException {
31+
String[] args = {};
32+
App.main(args);
33+
}
34+
}

semaphore/etc/semaphore.png

29.4 KB
Loading

0 commit comments

Comments
 (0)