-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mutex.java
59 lines (52 loc) · 1.18 KB
/
Mutex.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package ml.alternet.concurrent;
/**
* A mutex, based on a string ID, safe for synchronization for a given user
* context.
*
* <h3>Usage</h3>
*
* <pre>
* MutextContext mutexContext = new MutextContext();
* ...
*
* String id = someObject.getCanonicalID();
* Mutex mutex = mutexContext.getMutex(id);
* synchronized(mutex) {
* ...
* }
* </pre>
*
* Instead of synchronizing on a <tt>Mutex</tt> it is also possible to get a
* lock (exist in several flavours)
*
* @see MutexContext
* @see MutexContext#getLock(String)
* @see MutexContext#getReadWriteLock(String)
* @see MutexContext#getStampedLock(String)
*
* @author Philippe Poulard
*/
public class Mutex {
private final String id;
Mutex(String id) {
this.id = id;
}
@Override
public boolean equals(Object o) {
if (o == null) {
return false;
}
if (this.getClass() == o.getClass()) {
return this.id.equals(o.toString());
}
return false;
}
@Override
public int hashCode() {
return this.id.hashCode();
}
@Override
public String toString() {
return this.id;
}
}