-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
SimpleMutex.java
49 lines (41 loc) · 976 Bytes
/
SimpleMutex.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
package nallar.tickthreading.util.concurrent;
import java.util.concurrent.*;
import java.util.concurrent.locks.*;
public final class SimpleMutex implements Lock {
private boolean locked = false;
@Override
public synchronized void lock() {
while (locked) {
try {
wait();
} catch (InterruptedException ignored) {
}
}
locked = true;
}
@Override
public synchronized void unlock() {
locked = false;
notify();
}
@Override
public void lockInterruptibly() throws InterruptedException {
throw new UnsupportedOperationException();
}
@Override
public synchronized boolean tryLock() {
if (!locked) {
locked = true;
return true;
}
return false;
}
@Override
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
throw new UnsupportedOperationException();
}
@Override
public Condition newCondition() {
throw new UnsupportedOperationException();
}
}