32 |
|
private static class Sync extends AbstractQueuedSynchronizer { |
33 |
|
boolean isLocked() { return getState() == 1; } |
34 |
|
|
35 |
< |
public boolean tryAcquireExclusive(boolean isQueued, int acquires) { |
35 |
> |
public boolean tryAcquireExclusive(int acquires) { |
36 |
|
assert acquires == 1; // Does not use multiple acquires |
37 |
|
return compareAndSetState(0, 1); |
38 |
|
} |
57 |
|
|
58 |
|
private final Sync sync = new Sync(); |
59 |
|
public boolean tryLock() { |
60 |
< |
return sync.tryAcquireExclusive(false, 1); |
60 |
> |
return sync.tryAcquireExclusive(1); |
61 |
|
} |
62 |
|
public void lock() { |
63 |
|
sync.acquireExclusiveUninterruptibly(1); |
73 |
|
public boolean isLocked() { return sync.isLocked(); } |
74 |
|
public boolean hasQueuedThreads() { return sync.hasQueuedThreads(); } |
75 |
|
public boolean hasContended() { return sync.hasContended(); } |
76 |
+ |
public boolean isQueued(Thread t) { return sync.isQueued(t); } |
77 |
|
} |
78 |
|
|
79 |
|
/** |
150 |
|
t1.join(); |
151 |
|
t2.join(); |
152 |
|
} catch(Exception e){ |
153 |
+ |
unexpectedException(); |
154 |
+ |
} |
155 |
+ |
} |
156 |
+ |
|
157 |
+ |
/** |
158 |
+ |
* isQueued(null) throws NPE |
159 |
+ |
*/ |
160 |
+ |
public void testIsQueuedNPE() { |
161 |
+ |
final Mutex lock = new Mutex(); |
162 |
+ |
try { |
163 |
+ |
lock.isQueued(null); |
164 |
+ |
shouldThrow(); |
165 |
+ |
} catch (NullPointerException success) { |
166 |
+ |
} |
167 |
+ |
} |
168 |
+ |
|
169 |
+ |
/** |
170 |
+ |
* isQueued reports whether a thread is queued. |
171 |
+ |
*/ |
172 |
+ |
public void testIsQueued() { |
173 |
+ |
final Mutex lock = new Mutex(); |
174 |
+ |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
175 |
+ |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
176 |
+ |
try { |
177 |
+ |
assertFalse(lock.isQueued(t1)); |
178 |
+ |
assertFalse(lock.isQueued(t2)); |
179 |
+ |
lock.lock(); |
180 |
+ |
t1.start(); |
181 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
182 |
+ |
assertTrue(lock.isQueued(t1)); |
183 |
+ |
t2.start(); |
184 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
185 |
+ |
assertTrue(lock.isQueued(t1)); |
186 |
+ |
assertTrue(lock.isQueued(t2)); |
187 |
+ |
t1.interrupt(); |
188 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
189 |
+ |
assertFalse(lock.isQueued(t1)); |
190 |
+ |
assertTrue(lock.isQueued(t2)); |
191 |
+ |
lock.unlock(); |
192 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
193 |
+ |
assertFalse(lock.isQueued(t1)); |
194 |
+ |
assertFalse(lock.isQueued(t2)); |
195 |
+ |
t1.join(); |
196 |
+ |
t2.join(); |
197 |
+ |
} catch(Exception e){ |
198 |
|
unexpectedException(); |
199 |
|
} |
200 |
|
} |