11 |
|
import java.io.*; |
12 |
|
|
13 |
|
/** |
14 |
< |
* A sample user extension of AbstractQueuedSynchronizer. |
14 |
> |
* A sample user extension of AbstractQueuedSynchronizer. |
15 |
|
*/ |
16 |
|
public final class Mutex extends AbstractQueuedSynchronizer implements Lock, java.io.Serializable { |
17 |
|
public boolean isHeldExclusively() { return getState() == 1; } |
18 |
< |
|
18 |
> |
|
19 |
|
public boolean tryAcquire(int acquires) { |
20 |
|
return compareAndSetState(0, 1); |
21 |
|
} |
22 |
< |
|
22 |
> |
|
23 |
|
public boolean tryRelease(int releases) { |
24 |
|
setState(0); |
25 |
|
return true; |
26 |
|
} |
27 |
|
public Condition newCondition() { return new ConditionObject(); } |
28 |
< |
|
28 |
> |
|
29 |
|
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException { |
30 |
|
s.defaultReadObject(); |
31 |
|
setState(0); // reset to unlocked state |
32 |
|
} |
33 |
|
|
34 |
< |
public void lock() { |
35 |
< |
acquire(1); |
34 |
> |
public void lock() { |
35 |
> |
acquire(1); |
36 |
|
} |
37 |
< |
public boolean tryLock() { |
37 |
> |
public boolean tryLock() { |
38 |
|
return tryAcquire(1); |
39 |
|
} |
40 |
< |
public void lockInterruptibly() throws InterruptedException { |
40 |
> |
public void lockInterruptibly() throws InterruptedException { |
41 |
|
acquireInterruptibly(1); |
42 |
|
} |
43 |
|
public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException { |