30 |
|
* ReentrantReadWriteLock, and Semaphore |
31 |
|
*/ |
32 |
|
static class Mutex extends AbstractQueuedSynchronizer { |
33 |
< |
public boolean isLocked() { return getState() == 1; } |
33 |
> |
public boolean isHeldExclusively() { return getState() == 1; } |
34 |
|
|
35 |
< |
public boolean tryAcquireExclusive(int acquires) { |
35 |
> |
public boolean tryAcquire(int acquires) { |
36 |
|
assertTrue(acquires == 1); |
37 |
|
return compareAndSetState(0, 1); |
38 |
|
} |
39 |
|
|
40 |
< |
public boolean tryReleaseExclusive(int releases) { |
40 |
> |
public boolean tryRelease(int releases) { |
41 |
> |
if (getState() == 0) throw new IllegalMonitorStateException(); |
42 |
|
setState(0); |
43 |
|
return true; |
44 |
|
} |
45 |
|
|
46 |
< |
public void checkConditionAccess(Thread thread) { |
46 |
< |
if (getState() == 0) throw new IllegalMonitorStateException(); |
47 |
< |
} |
48 |
< |
|
49 |
< |
public ConditionObject newCondition() { return new ConditionObject(); } |
50 |
< |
|
51 |
< |
public void lock() { |
52 |
< |
acquireExclusiveUninterruptibly(1); |
53 |
< |
} |
46 |
> |
public AbstractQueuedSynchronizer.ConditionObject newCondition() { return new AbstractQueuedSynchronizer.ConditionObject(); } |
47 |
|
|
48 |
|
} |
49 |
|
|
65 |
|
} |
66 |
|
|
67 |
|
/** |
68 |
< |
* A runnable calling acquireExclusiveInterruptibly |
68 |
> |
* A runnable calling acquireInterruptibly |
69 |
|
*/ |
70 |
< |
class InterruptibleLockRunnable implements Runnable { |
71 |
< |
final Mutex lock; |
72 |
< |
InterruptibleLockRunnable(Mutex l) { lock = l; } |
70 |
> |
class InterruptibleSyncRunnable implements Runnable { |
71 |
> |
final Mutex sync; |
72 |
> |
InterruptibleSyncRunnable(Mutex l) { sync = l; } |
73 |
|
public void run() { |
74 |
|
try { |
75 |
< |
lock.acquireExclusiveInterruptibly(1); |
75 |
> |
sync.acquireInterruptibly(1); |
76 |
|
} catch(InterruptedException success){} |
77 |
|
} |
78 |
|
} |
79 |
|
|
80 |
|
|
81 |
|
/** |
82 |
< |
* A runnable calling acquireExclusiveInterruptibly that expects to be |
82 |
> |
* A runnable calling acquireInterruptibly that expects to be |
83 |
|
* interrupted |
84 |
|
*/ |
85 |
< |
class InterruptedLockRunnable implements Runnable { |
86 |
< |
final Mutex lock; |
87 |
< |
InterruptedLockRunnable(Mutex l) { lock = l; } |
85 |
> |
class InterruptedSyncRunnable implements Runnable { |
86 |
> |
final Mutex sync; |
87 |
> |
InterruptedSyncRunnable(Mutex l) { sync = l; } |
88 |
|
public void run() { |
89 |
|
try { |
90 |
< |
lock.acquireExclusiveInterruptibly(1); |
90 |
> |
sync.acquireInterruptibly(1); |
91 |
|
threadShouldThrow(); |
92 |
|
} catch(InterruptedException success){} |
93 |
|
} |
94 |
|
} |
95 |
+ |
|
96 |
+ |
/** |
97 |
+ |
* isHeldExclusively is false upon construction |
98 |
+ |
*/ |
99 |
+ |
public void testIsHeldExclusively() { |
100 |
+ |
Mutex rl = new Mutex(); |
101 |
+ |
assertFalse(rl.isHeldExclusively()); |
102 |
+ |
} |
103 |
|
|
104 |
|
/** |
105 |
< |
* acquireExclusiveUninterruptiblying an releaseExclusiveed lock succeeds |
105 |
> |
* acquiring released sync succeeds |
106 |
|
*/ |
107 |
< |
public void testAcquireExclusiveUninterruptibly() { |
107 |
> |
public void testAcquire() { |
108 |
|
Mutex rl = new Mutex(); |
109 |
< |
rl.acquireExclusiveUninterruptibly(1); |
110 |
< |
assertTrue(rl.isLocked()); |
111 |
< |
rl.releaseExclusive(1); |
109 |
> |
rl.acquire(1); |
110 |
> |
assertTrue(rl.isHeldExclusively()); |
111 |
> |
rl.release(1); |
112 |
> |
assertFalse(rl.isHeldExclusively()); |
113 |
|
} |
114 |
|
|
115 |
|
/** |
116 |
< |
* tryAcquireExclusive on an releaseExclusiveed lock succeeds |
116 |
> |
* tryAcquire on an released sync succeeds |
117 |
|
*/ |
118 |
< |
public void testTryAcquireExclusive() { |
118 |
> |
public void testTryAcquire() { |
119 |
|
Mutex rl = new Mutex(); |
120 |
< |
assertTrue(rl.tryAcquireExclusive(1)); |
121 |
< |
assertTrue(rl.isLocked()); |
122 |
< |
rl.releaseExclusive(1); |
120 |
> |
assertTrue(rl.tryAcquire(1)); |
121 |
> |
assertTrue(rl.isHeldExclusively()); |
122 |
> |
rl.release(1); |
123 |
|
} |
124 |
|
|
125 |
|
/** |
126 |
|
* hasQueuedThreads reports whether there are waiting threads |
127 |
|
*/ |
128 |
|
public void testhasQueuedThreads() { |
129 |
< |
final Mutex lock = new Mutex(); |
130 |
< |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
131 |
< |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
129 |
> |
final Mutex sync = new Mutex(); |
130 |
> |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
131 |
> |
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
132 |
|
try { |
133 |
< |
assertFalse(lock.hasQueuedThreads()); |
134 |
< |
lock.acquireExclusiveUninterruptibly(1); |
133 |
> |
assertFalse(sync.hasQueuedThreads()); |
134 |
> |
sync.acquire(1); |
135 |
|
t1.start(); |
136 |
|
Thread.sleep(SHORT_DELAY_MS); |
137 |
< |
assertTrue(lock.hasQueuedThreads()); |
137 |
> |
assertTrue(sync.hasQueuedThreads()); |
138 |
|
t2.start(); |
139 |
|
Thread.sleep(SHORT_DELAY_MS); |
140 |
< |
assertTrue(lock.hasQueuedThreads()); |
140 |
> |
assertTrue(sync.hasQueuedThreads()); |
141 |
|
t1.interrupt(); |
142 |
|
Thread.sleep(SHORT_DELAY_MS); |
143 |
< |
assertTrue(lock.hasQueuedThreads()); |
144 |
< |
lock.releaseExclusive(1); |
143 |
> |
assertTrue(sync.hasQueuedThreads()); |
144 |
> |
sync.release(1); |
145 |
|
Thread.sleep(SHORT_DELAY_MS); |
146 |
< |
assertFalse(lock.hasQueuedThreads()); |
146 |
> |
assertFalse(sync.hasQueuedThreads()); |
147 |
|
t1.join(); |
148 |
|
t2.join(); |
149 |
|
} catch(Exception e){ |
155 |
|
* isQueued(null) throws NPE |
156 |
|
*/ |
157 |
|
public void testIsQueuedNPE() { |
158 |
< |
final Mutex lock = new Mutex(); |
158 |
> |
final Mutex sync = new Mutex(); |
159 |
|
try { |
160 |
< |
lock.isQueued(null); |
160 |
> |
sync.isQueued(null); |
161 |
|
shouldThrow(); |
162 |
|
} catch (NullPointerException success) { |
163 |
|
} |
167 |
|
* isQueued reports whether a thread is queued. |
168 |
|
*/ |
169 |
|
public void testIsQueued() { |
170 |
< |
final Mutex lock = new Mutex(); |
171 |
< |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
172 |
< |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
173 |
< |
try { |
174 |
< |
assertFalse(lock.isQueued(t1)); |
175 |
< |
assertFalse(lock.isQueued(t2)); |
176 |
< |
lock.acquireExclusiveUninterruptibly(1); |
170 |
> |
final Mutex sync = new Mutex(); |
171 |
> |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
172 |
> |
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
173 |
> |
try { |
174 |
> |
assertFalse(sync.isQueued(t1)); |
175 |
> |
assertFalse(sync.isQueued(t2)); |
176 |
> |
sync.acquire(1); |
177 |
|
t1.start(); |
178 |
|
Thread.sleep(SHORT_DELAY_MS); |
179 |
< |
assertTrue(lock.isQueued(t1)); |
179 |
> |
assertTrue(sync.isQueued(t1)); |
180 |
|
t2.start(); |
181 |
|
Thread.sleep(SHORT_DELAY_MS); |
182 |
< |
assertTrue(lock.isQueued(t1)); |
183 |
< |
assertTrue(lock.isQueued(t2)); |
182 |
> |
assertTrue(sync.isQueued(t1)); |
183 |
> |
assertTrue(sync.isQueued(t2)); |
184 |
|
t1.interrupt(); |
185 |
|
Thread.sleep(SHORT_DELAY_MS); |
186 |
< |
assertFalse(lock.isQueued(t1)); |
187 |
< |
assertTrue(lock.isQueued(t2)); |
188 |
< |
lock.releaseExclusive(1); |
186 |
> |
assertFalse(sync.isQueued(t1)); |
187 |
> |
assertTrue(sync.isQueued(t2)); |
188 |
> |
sync.release(1); |
189 |
|
Thread.sleep(SHORT_DELAY_MS); |
190 |
< |
assertFalse(lock.isQueued(t1)); |
191 |
< |
assertFalse(lock.isQueued(t2)); |
190 |
> |
assertFalse(sync.isQueued(t1)); |
191 |
> |
assertFalse(sync.isQueued(t2)); |
192 |
|
t1.join(); |
193 |
|
t2.join(); |
194 |
|
} catch(Exception e){ |
197 |
|
} |
198 |
|
|
199 |
|
/** |
200 |
< |
* getFirstQueuedThread returns first waiting thread or null is none |
200 |
> |
* getFirstQueuedThread returns first waiting thread or null if none |
201 |
|
*/ |
202 |
|
public void testGetFirstQueuedThread() { |
203 |
< |
final Mutex lock = new Mutex(); |
204 |
< |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
205 |
< |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
203 |
> |
final Mutex sync = new Mutex(); |
204 |
> |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
205 |
> |
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
206 |
|
try { |
207 |
< |
assertNull(lock.getFirstQueuedThread()); |
208 |
< |
lock.acquireExclusiveUninterruptibly(1); |
207 |
> |
assertNull(sync.getFirstQueuedThread()); |
208 |
> |
sync.acquire(1); |
209 |
|
t1.start(); |
210 |
|
Thread.sleep(SHORT_DELAY_MS); |
211 |
< |
assertEquals(t1, lock.getFirstQueuedThread()); |
211 |
> |
assertEquals(t1, sync.getFirstQueuedThread()); |
212 |
|
t2.start(); |
213 |
|
Thread.sleep(SHORT_DELAY_MS); |
214 |
< |
assertEquals(t1, lock.getFirstQueuedThread()); |
214 |
> |
assertEquals(t1, sync.getFirstQueuedThread()); |
215 |
|
t1.interrupt(); |
216 |
|
Thread.sleep(SHORT_DELAY_MS); |
217 |
< |
assertEquals(t2, lock.getFirstQueuedThread()); |
218 |
< |
lock.releaseExclusive(1); |
217 |
> |
assertEquals(t2, sync.getFirstQueuedThread()); |
218 |
> |
sync.release(1); |
219 |
|
Thread.sleep(SHORT_DELAY_MS); |
220 |
< |
assertNull(lock.getFirstQueuedThread()); |
220 |
> |
assertNull(sync.getFirstQueuedThread()); |
221 |
|
t1.join(); |
222 |
|
t2.join(); |
223 |
|
} catch(Exception e){ |
230 |
|
* hasContended reports false if no thread has ever blocked, else true |
231 |
|
*/ |
232 |
|
public void testHasContended() { |
233 |
< |
final Mutex lock = new Mutex(); |
234 |
< |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
235 |
< |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
233 |
> |
final Mutex sync = new Mutex(); |
234 |
> |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
235 |
> |
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
236 |
> |
try { |
237 |
> |
assertFalse(sync.hasContended()); |
238 |
> |
sync.acquire(1); |
239 |
> |
t1.start(); |
240 |
> |
Thread.sleep(SHORT_DELAY_MS); |
241 |
> |
assertTrue(sync.hasContended()); |
242 |
> |
t2.start(); |
243 |
> |
Thread.sleep(SHORT_DELAY_MS); |
244 |
> |
assertTrue(sync.hasContended()); |
245 |
> |
t1.interrupt(); |
246 |
> |
Thread.sleep(SHORT_DELAY_MS); |
247 |
> |
assertTrue(sync.hasContended()); |
248 |
> |
sync.release(1); |
249 |
> |
Thread.sleep(SHORT_DELAY_MS); |
250 |
> |
assertTrue(sync.hasContended()); |
251 |
> |
t1.join(); |
252 |
> |
t2.join(); |
253 |
> |
} catch(Exception e){ |
254 |
> |
unexpectedException(); |
255 |
> |
} |
256 |
> |
} |
257 |
> |
|
258 |
> |
/** |
259 |
> |
* getQueuedThreads includes waiting threads |
260 |
> |
*/ |
261 |
> |
public void testGetQueuedThreads() { |
262 |
> |
final Mutex sync = new Mutex(); |
263 |
> |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
264 |
> |
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
265 |
> |
try { |
266 |
> |
assertTrue(sync.getQueuedThreads().isEmpty()); |
267 |
> |
sync.acquire(1); |
268 |
> |
assertTrue(sync.getQueuedThreads().isEmpty()); |
269 |
> |
t1.start(); |
270 |
> |
Thread.sleep(SHORT_DELAY_MS); |
271 |
> |
assertTrue(sync.getQueuedThreads().contains(t1)); |
272 |
> |
t2.start(); |
273 |
> |
Thread.sleep(SHORT_DELAY_MS); |
274 |
> |
assertTrue(sync.getQueuedThreads().contains(t1)); |
275 |
> |
assertTrue(sync.getQueuedThreads().contains(t2)); |
276 |
> |
t1.interrupt(); |
277 |
> |
Thread.sleep(SHORT_DELAY_MS); |
278 |
> |
assertFalse(sync.getQueuedThreads().contains(t1)); |
279 |
> |
assertTrue(sync.getQueuedThreads().contains(t2)); |
280 |
> |
sync.release(1); |
281 |
> |
Thread.sleep(SHORT_DELAY_MS); |
282 |
> |
assertTrue(sync.getQueuedThreads().isEmpty()); |
283 |
> |
t1.join(); |
284 |
> |
t2.join(); |
285 |
> |
} catch(Exception e){ |
286 |
> |
unexpectedException(); |
287 |
> |
} |
288 |
> |
} |
289 |
> |
|
290 |
> |
/** |
291 |
> |
* getExclusiveQueuedThreads includes waiting threads |
292 |
> |
*/ |
293 |
> |
public void testGetExclusiveQueuedThreads() { |
294 |
> |
final Mutex sync = new Mutex(); |
295 |
> |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
296 |
> |
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
297 |
|
try { |
298 |
< |
assertFalse(lock.hasContended()); |
299 |
< |
lock.acquireExclusiveUninterruptibly(1); |
298 |
> |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
299 |
> |
sync.acquire(1); |
300 |
> |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
301 |
|
t1.start(); |
302 |
|
Thread.sleep(SHORT_DELAY_MS); |
303 |
< |
assertTrue(lock.hasContended()); |
303 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
304 |
|
t2.start(); |
305 |
|
Thread.sleep(SHORT_DELAY_MS); |
306 |
< |
assertTrue(lock.hasContended()); |
306 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
307 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
308 |
|
t1.interrupt(); |
309 |
|
Thread.sleep(SHORT_DELAY_MS); |
310 |
< |
assertTrue(lock.hasContended()); |
311 |
< |
lock.releaseExclusive(1); |
310 |
> |
assertFalse(sync.getExclusiveQueuedThreads().contains(t1)); |
311 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
312 |
> |
sync.release(1); |
313 |
|
Thread.sleep(SHORT_DELAY_MS); |
314 |
< |
assertTrue(lock.hasContended()); |
314 |
> |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
315 |
|
t1.join(); |
316 |
|
t2.join(); |
317 |
|
} catch(Exception e){ |
320 |
|
} |
321 |
|
|
322 |
|
/** |
323 |
< |
* acquireExclusiveNanos is interruptible. |
323 |
> |
* getSharedQueuedThreads does not include exclusively waiting threads |
324 |
> |
*/ |
325 |
> |
public void testGetSharedQueuedThreads() { |
326 |
> |
final Mutex sync = new Mutex(); |
327 |
> |
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
328 |
> |
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
329 |
> |
try { |
330 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
331 |
> |
sync.acquire(1); |
332 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
333 |
> |
t1.start(); |
334 |
> |
Thread.sleep(SHORT_DELAY_MS); |
335 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
336 |
> |
t2.start(); |
337 |
> |
Thread.sleep(SHORT_DELAY_MS); |
338 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
339 |
> |
t1.interrupt(); |
340 |
> |
Thread.sleep(SHORT_DELAY_MS); |
341 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
342 |
> |
sync.release(1); |
343 |
> |
Thread.sleep(SHORT_DELAY_MS); |
344 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
345 |
> |
t1.join(); |
346 |
> |
t2.join(); |
347 |
> |
} catch(Exception e){ |
348 |
> |
unexpectedException(); |
349 |
> |
} |
350 |
> |
} |
351 |
> |
|
352 |
> |
/** |
353 |
> |
* tryAcquireNanos is interruptible. |
354 |
|
*/ |
355 |
|
public void testInterruptedException2() { |
356 |
< |
final Mutex lock = new Mutex(); |
357 |
< |
lock.acquireExclusiveUninterruptibly(1); |
356 |
> |
final Mutex sync = new Mutex(); |
357 |
> |
sync.acquire(1); |
358 |
|
Thread t = new Thread(new Runnable() { |
359 |
|
public void run() { |
360 |
|
try { |
361 |
< |
lock.acquireExclusiveNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); |
361 |
> |
sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); |
362 |
|
threadShouldThrow(); |
363 |
|
} catch(InterruptedException success){} |
364 |
|
} |
373 |
|
|
374 |
|
|
375 |
|
/** |
376 |
< |
* TryAcquireExclusive on a locked lock fails |
376 |
> |
* TryAcquire on exclusively held sync fails |
377 |
|
*/ |
378 |
< |
public void testTryAcquireExclusiveWhenLocked() { |
379 |
< |
final Mutex lock = new Mutex(); |
380 |
< |
lock.acquireExclusiveUninterruptibly(1); |
378 |
> |
public void testTryAcquireWhenSynced() { |
379 |
> |
final Mutex sync = new Mutex(); |
380 |
> |
sync.acquire(1); |
381 |
|
Thread t = new Thread(new Runnable() { |
382 |
|
public void run() { |
383 |
< |
threadAssertFalse(lock.tryAcquireExclusive(1)); |
383 |
> |
threadAssertFalse(sync.tryAcquire(1)); |
384 |
|
} |
385 |
|
}); |
386 |
|
try { |
387 |
|
t.start(); |
388 |
|
t.join(); |
389 |
< |
lock.releaseExclusive(1); |
389 |
> |
sync.release(1); |
390 |
|
} catch(Exception e){ |
391 |
|
unexpectedException(); |
392 |
|
} |
393 |
|
} |
394 |
|
|
395 |
|
/** |
396 |
< |
* acquireExclusiveNanos on a locked lock times out |
396 |
> |
* tryAcquireNanos on an exclusively held sync times out |
397 |
|
*/ |
398 |
< |
public void testAcquireExclusiveNanos_Timeout() { |
399 |
< |
final Mutex lock = new Mutex(); |
400 |
< |
lock.acquireExclusiveUninterruptibly(1); |
398 |
> |
public void testAcquireNanos_Timeout() { |
399 |
> |
final Mutex sync = new Mutex(); |
400 |
> |
sync.acquire(1); |
401 |
|
Thread t = new Thread(new Runnable() { |
402 |
|
public void run() { |
403 |
|
try { |
404 |
< |
threadAssertFalse(lock.acquireExclusiveNanos(1, 1000 * 1000)); |
404 |
> |
threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000)); |
405 |
|
} catch (Exception ex) { |
406 |
|
threadUnexpectedException(); |
407 |
|
} |
410 |
|
try { |
411 |
|
t.start(); |
412 |
|
t.join(); |
413 |
< |
lock.releaseExclusive(1); |
413 |
> |
sync.release(1); |
414 |
|
} catch(Exception e){ |
415 |
|
unexpectedException(); |
416 |
|
} |
421 |
|
* getState is true when acquired and false when not |
422 |
|
*/ |
423 |
|
public void testGetState() { |
424 |
< |
final Mutex lock = new Mutex(); |
425 |
< |
lock.acquireExclusiveUninterruptibly(1); |
426 |
< |
assertTrue(lock.isLocked()); |
427 |
< |
lock.releaseExclusive(1); |
428 |
< |
assertFalse(lock.isLocked()); |
424 |
> |
final Mutex sync = new Mutex(); |
425 |
> |
sync.acquire(1); |
426 |
> |
assertTrue(sync.isHeldExclusively()); |
427 |
> |
sync.release(1); |
428 |
> |
assertFalse(sync.isHeldExclusively()); |
429 |
|
Thread t = new Thread(new Runnable() { |
430 |
|
public void run() { |
431 |
< |
lock.acquireExclusiveUninterruptibly(1); |
431 |
> |
sync.acquire(1); |
432 |
|
try { |
433 |
|
Thread.sleep(SMALL_DELAY_MS); |
434 |
|
} |
435 |
|
catch(Exception e) { |
436 |
|
threadUnexpectedException(); |
437 |
|
} |
438 |
< |
lock.releaseExclusive(1); |
438 |
> |
sync.release(1); |
439 |
|
} |
440 |
|
}); |
441 |
|
try { |
442 |
|
t.start(); |
443 |
|
Thread.sleep(SHORT_DELAY_MS); |
444 |
< |
assertTrue(lock.isLocked()); |
444 |
> |
assertTrue(sync.isHeldExclusively()); |
445 |
|
t.join(); |
446 |
< |
assertFalse(lock.isLocked()); |
446 |
> |
assertFalse(sync.isHeldExclusively()); |
447 |
|
} catch(Exception e){ |
448 |
|
unexpectedException(); |
449 |
|
} |
451 |
|
|
452 |
|
|
453 |
|
/** |
454 |
< |
* acquireExclusiveInterruptibly is interruptible. |
454 |
> |
* acquireInterruptibly is interruptible. |
455 |
|
*/ |
456 |
|
public void testAcquireInterruptibly1() { |
457 |
< |
final Mutex lock = new Mutex(); |
458 |
< |
lock.acquireExclusiveUninterruptibly(1); |
459 |
< |
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
457 |
> |
final Mutex sync = new Mutex(); |
458 |
> |
sync.acquire(1); |
459 |
> |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
460 |
|
try { |
461 |
|
t.start(); |
462 |
|
t.interrupt(); |
463 |
< |
lock.releaseExclusive(1); |
463 |
> |
sync.release(1); |
464 |
|
t.join(); |
465 |
|
} catch(Exception e){ |
466 |
|
unexpectedException(); |
468 |
|
} |
469 |
|
|
470 |
|
/** |
471 |
< |
* acquireExclusiveInterruptibly succeeds when released, else is interruptible |
471 |
> |
* acquireInterruptibly succeeds when released, else is interruptible |
472 |
|
*/ |
473 |
|
public void testAcquireInterruptibly2() { |
474 |
< |
final Mutex lock = new Mutex(); |
474 |
> |
final Mutex sync = new Mutex(); |
475 |
|
try { |
476 |
< |
lock.acquireExclusiveInterruptibly(1); |
476 |
> |
sync.acquireInterruptibly(1); |
477 |
|
} catch(Exception e) { |
478 |
|
unexpectedException(); |
479 |
|
} |
480 |
< |
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
480 |
> |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
481 |
|
try { |
482 |
|
t.start(); |
483 |
|
t.interrupt(); |
484 |
< |
assertTrue(lock.isLocked()); |
484 |
> |
assertTrue(sync.isHeldExclusively()); |
485 |
|
t.join(); |
486 |
|
} catch(Exception e){ |
487 |
|
unexpectedException(); |
489 |
|
} |
490 |
|
|
491 |
|
/** |
492 |
< |
* owns is true for a condition created by lock else false |
492 |
> |
* owns is true for a condition created by sync else false |
493 |
|
*/ |
494 |
|
public void testOwns() { |
495 |
< |
final Mutex lock = new Mutex(); |
496 |
< |
final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition(); |
497 |
< |
final Mutex lock2 = new Mutex(); |
498 |
< |
assertTrue(lock.owns(c)); |
499 |
< |
assertFalse(lock2.owns(c)); |
495 |
> |
final Mutex sync = new Mutex(); |
496 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
497 |
> |
final Mutex sync2 = new Mutex(); |
498 |
> |
assertTrue(sync.owns(c)); |
499 |
> |
assertFalse(sync2.owns(c)); |
500 |
|
} |
501 |
|
|
502 |
|
/** |
503 |
< |
* Calling await without holding lock throws IllegalMonitorStateException |
503 |
> |
* Calling await without holding sync throws IllegalMonitorStateException |
504 |
|
*/ |
505 |
|
public void testAwait_IllegalMonitor() { |
506 |
< |
final Mutex lock = new Mutex(); |
507 |
< |
final Condition c = lock.newCondition(); |
506 |
> |
final Mutex sync = new Mutex(); |
507 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
508 |
|
try { |
509 |
|
c.await(); |
510 |
|
shouldThrow(); |
517 |
|
} |
518 |
|
|
519 |
|
/** |
520 |
< |
* Calling signal without holding lock throws IllegalMonitorStateException |
520 |
> |
* Calling signal without holding sync throws IllegalMonitorStateException |
521 |
|
*/ |
522 |
|
public void testSignal_IllegalMonitor() { |
523 |
< |
final Mutex lock = new Mutex(); |
524 |
< |
final Condition c = lock.newCondition(); |
523 |
> |
final Mutex sync = new Mutex(); |
524 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
525 |
|
try { |
526 |
|
c.signal(); |
527 |
|
shouldThrow(); |
537 |
|
* awaitNanos without a signal times out |
538 |
|
*/ |
539 |
|
public void testAwaitNanos_Timeout() { |
540 |
< |
final Mutex lock = new Mutex(); |
541 |
< |
final Condition c = lock.newCondition(); |
540 |
> |
final Mutex sync = new Mutex(); |
541 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
542 |
|
try { |
543 |
< |
lock.acquireExclusiveUninterruptibly(1); |
543 |
> |
sync.acquire(1); |
544 |
|
long t = c.awaitNanos(100); |
545 |
|
assertTrue(t <= 0); |
546 |
< |
lock.releaseExclusive(1); |
546 |
> |
sync.release(1); |
547 |
|
} |
548 |
|
catch (Exception ex) { |
549 |
|
unexpectedException(); |
551 |
|
} |
552 |
|
|
553 |
|
/** |
554 |
< |
* timed await without a signal times out |
554 |
> |
* Timed await without a signal times out |
555 |
|
*/ |
556 |
|
public void testAwait_Timeout() { |
557 |
< |
final Mutex lock = new Mutex(); |
558 |
< |
final Condition c = lock.newCondition(); |
557 |
> |
final Mutex sync = new Mutex(); |
558 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
559 |
|
try { |
560 |
< |
lock.acquireExclusiveUninterruptibly(1); |
560 |
> |
sync.acquire(1); |
561 |
|
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
562 |
< |
lock.releaseExclusive(1); |
562 |
> |
sync.release(1); |
563 |
|
} |
564 |
|
catch (Exception ex) { |
565 |
|
unexpectedException(); |
570 |
|
* awaitUntil without a signal times out |
571 |
|
*/ |
572 |
|
public void testAwaitUntil_Timeout() { |
573 |
< |
final Mutex lock = new Mutex(); |
574 |
< |
final Condition c = lock.newCondition(); |
573 |
> |
final Mutex sync = new Mutex(); |
574 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
575 |
|
try { |
576 |
< |
lock.acquireExclusiveUninterruptibly(1); |
576 |
> |
sync.acquire(1); |
577 |
|
java.util.Date d = new java.util.Date(); |
578 |
|
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
579 |
< |
lock.releaseExclusive(1); |
579 |
> |
sync.release(1); |
580 |
|
} |
581 |
|
catch (Exception ex) { |
582 |
|
unexpectedException(); |
587 |
|
* await returns when signalled |
588 |
|
*/ |
589 |
|
public void testAwait() { |
590 |
< |
final Mutex lock = new Mutex(); |
591 |
< |
final Condition c = lock.newCondition(); |
590 |
> |
final Mutex sync = new Mutex(); |
591 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
592 |
> |
Thread t = new Thread(new Runnable() { |
593 |
> |
public void run() { |
594 |
> |
try { |
595 |
> |
sync.acquire(1); |
596 |
> |
c.await(); |
597 |
> |
sync.release(1); |
598 |
> |
} |
599 |
> |
catch(InterruptedException e) { |
600 |
> |
threadUnexpectedException(); |
601 |
> |
} |
602 |
> |
} |
603 |
> |
}); |
604 |
> |
|
605 |
> |
try { |
606 |
> |
t.start(); |
607 |
> |
Thread.sleep(SHORT_DELAY_MS); |
608 |
> |
sync.acquire(1); |
609 |
> |
c.signal(); |
610 |
> |
sync.release(1); |
611 |
> |
t.join(SHORT_DELAY_MS); |
612 |
> |
assertFalse(t.isAlive()); |
613 |
> |
} |
614 |
> |
catch (Exception ex) { |
615 |
> |
unexpectedException(); |
616 |
> |
} |
617 |
> |
} |
618 |
> |
|
619 |
> |
|
620 |
> |
|
621 |
> |
/** |
622 |
> |
* hasWaiters throws NPE if null |
623 |
> |
*/ |
624 |
> |
public void testHasWaitersNPE() { |
625 |
> |
final Mutex sync = new Mutex(); |
626 |
> |
try { |
627 |
> |
sync.hasWaiters(null); |
628 |
> |
shouldThrow(); |
629 |
> |
} catch (NullPointerException success) { |
630 |
> |
} catch (Exception ex) { |
631 |
> |
unexpectedException(); |
632 |
> |
} |
633 |
> |
} |
634 |
> |
|
635 |
> |
/** |
636 |
> |
* getWaitQueueLength throws NPE if null |
637 |
> |
*/ |
638 |
> |
public void testGetWaitQueueLengthNPE() { |
639 |
> |
final Mutex sync = new Mutex(); |
640 |
> |
try { |
641 |
> |
sync.getWaitQueueLength(null); |
642 |
> |
shouldThrow(); |
643 |
> |
} catch (NullPointerException success) { |
644 |
> |
} catch (Exception ex) { |
645 |
> |
unexpectedException(); |
646 |
> |
} |
647 |
> |
} |
648 |
> |
|
649 |
> |
|
650 |
> |
/** |
651 |
> |
* getWaitingThreads throws NPE if null |
652 |
> |
*/ |
653 |
> |
public void testGetWaitingThreadsNPE() { |
654 |
> |
final Mutex sync = new Mutex(); |
655 |
> |
try { |
656 |
> |
sync.getWaitingThreads(null); |
657 |
> |
shouldThrow(); |
658 |
> |
} catch (NullPointerException success) { |
659 |
> |
} catch (Exception ex) { |
660 |
> |
unexpectedException(); |
661 |
> |
} |
662 |
> |
} |
663 |
> |
|
664 |
> |
|
665 |
> |
/** |
666 |
> |
* hasWaiters throws IAE if not owned |
667 |
> |
*/ |
668 |
> |
public void testHasWaitersIAE() { |
669 |
> |
final Mutex sync = new Mutex(); |
670 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
671 |
> |
final Mutex sync2 = new Mutex(); |
672 |
> |
try { |
673 |
> |
sync2.hasWaiters(c); |
674 |
> |
shouldThrow(); |
675 |
> |
} catch (IllegalArgumentException success) { |
676 |
> |
} catch (Exception ex) { |
677 |
> |
unexpectedException(); |
678 |
> |
} |
679 |
> |
} |
680 |
> |
|
681 |
> |
/** |
682 |
> |
* hasWaiters throws IMSE if not synced |
683 |
> |
*/ |
684 |
> |
public void testHasWaitersIMSE() { |
685 |
> |
final Mutex sync = new Mutex(); |
686 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
687 |
> |
try { |
688 |
> |
sync.hasWaiters(c); |
689 |
> |
shouldThrow(); |
690 |
> |
} catch (IllegalMonitorStateException success) { |
691 |
> |
} catch (Exception ex) { |
692 |
> |
unexpectedException(); |
693 |
> |
} |
694 |
> |
} |
695 |
> |
|
696 |
> |
|
697 |
> |
/** |
698 |
> |
* getWaitQueueLength throws IAE if not owned |
699 |
> |
*/ |
700 |
> |
public void testGetWaitQueueLengthIAE() { |
701 |
> |
final Mutex sync = new Mutex(); |
702 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
703 |
> |
final Mutex sync2 = new Mutex(); |
704 |
> |
try { |
705 |
> |
sync2.getWaitQueueLength(c); |
706 |
> |
shouldThrow(); |
707 |
> |
} catch (IllegalArgumentException success) { |
708 |
> |
} catch (Exception ex) { |
709 |
> |
unexpectedException(); |
710 |
> |
} |
711 |
> |
} |
712 |
> |
|
713 |
> |
/** |
714 |
> |
* getWaitQueueLength throws IMSE if not synced |
715 |
> |
*/ |
716 |
> |
public void testGetWaitQueueLengthIMSE() { |
717 |
> |
final Mutex sync = new Mutex(); |
718 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
719 |
> |
try { |
720 |
> |
sync.getWaitQueueLength(c); |
721 |
> |
shouldThrow(); |
722 |
> |
} catch (IllegalMonitorStateException success) { |
723 |
> |
} catch (Exception ex) { |
724 |
> |
unexpectedException(); |
725 |
> |
} |
726 |
> |
} |
727 |
> |
|
728 |
> |
|
729 |
> |
/** |
730 |
> |
* getWaitingThreads throws IAE if not owned |
731 |
> |
*/ |
732 |
> |
public void testGetWaitingThreadsIAE() { |
733 |
> |
final Mutex sync = new Mutex(); |
734 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
735 |
> |
final Mutex sync2 = new Mutex(); |
736 |
> |
try { |
737 |
> |
sync2.getWaitingThreads(c); |
738 |
> |
shouldThrow(); |
739 |
> |
} catch (IllegalArgumentException success) { |
740 |
> |
} catch (Exception ex) { |
741 |
> |
unexpectedException(); |
742 |
> |
} |
743 |
> |
} |
744 |
> |
|
745 |
> |
/** |
746 |
> |
* getWaitingThreads throws IMSE if not synced |
747 |
> |
*/ |
748 |
> |
public void testGetWaitingThreadsIMSE() { |
749 |
> |
final Mutex sync = new Mutex(); |
750 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
751 |
> |
try { |
752 |
> |
sync.getWaitingThreads(c); |
753 |
> |
shouldThrow(); |
754 |
> |
} catch (IllegalMonitorStateException success) { |
755 |
> |
} catch (Exception ex) { |
756 |
> |
unexpectedException(); |
757 |
> |
} |
758 |
> |
} |
759 |
> |
|
760 |
> |
|
761 |
> |
|
762 |
> |
/** |
763 |
> |
* hasWaiters returns true when a thread is waiting, else false |
764 |
> |
*/ |
765 |
> |
public void testHasWaiters() { |
766 |
> |
final Mutex sync = new Mutex(); |
767 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
768 |
|
Thread t = new Thread(new Runnable() { |
769 |
|
public void run() { |
770 |
|
try { |
771 |
< |
lock.acquireExclusiveUninterruptibly(1); |
771 |
> |
sync.acquire(1); |
772 |
> |
threadAssertFalse(sync.hasWaiters(c)); |
773 |
> |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
774 |
|
c.await(); |
775 |
< |
lock.releaseExclusive(1); |
775 |
> |
sync.release(1); |
776 |
|
} |
777 |
|
catch(InterruptedException e) { |
778 |
|
threadUnexpectedException(); |
783 |
|
try { |
784 |
|
t.start(); |
785 |
|
Thread.sleep(SHORT_DELAY_MS); |
786 |
< |
lock.acquireExclusiveUninterruptibly(1); |
786 |
> |
sync.acquire(1); |
787 |
> |
assertTrue(sync.hasWaiters(c)); |
788 |
> |
assertEquals(1, sync.getWaitQueueLength(c)); |
789 |
|
c.signal(); |
790 |
< |
lock.releaseExclusive(1); |
790 |
> |
sync.release(1); |
791 |
> |
Thread.sleep(SHORT_DELAY_MS); |
792 |
> |
sync.acquire(1); |
793 |
> |
assertFalse(sync.hasWaiters(c)); |
794 |
> |
assertEquals(0, sync.getWaitQueueLength(c)); |
795 |
> |
sync.release(1); |
796 |
|
t.join(SHORT_DELAY_MS); |
797 |
|
assertFalse(t.isAlive()); |
798 |
|
} |
802 |
|
} |
803 |
|
|
804 |
|
/** |
805 |
+ |
* getWaitQueueLength returns number of waiting threads |
806 |
+ |
*/ |
807 |
+ |
public void testGetWaitQueueLength() { |
808 |
+ |
final Mutex sync = new Mutex(); |
809 |
+ |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
810 |
+ |
Thread t1 = new Thread(new Runnable() { |
811 |
+ |
public void run() { |
812 |
+ |
try { |
813 |
+ |
sync.acquire(1); |
814 |
+ |
threadAssertFalse(sync.hasWaiters(c)); |
815 |
+ |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
816 |
+ |
c.await(); |
817 |
+ |
sync.release(1); |
818 |
+ |
} |
819 |
+ |
catch(InterruptedException e) { |
820 |
+ |
threadUnexpectedException(); |
821 |
+ |
} |
822 |
+ |
} |
823 |
+ |
}); |
824 |
+ |
|
825 |
+ |
Thread t2 = new Thread(new Runnable() { |
826 |
+ |
public void run() { |
827 |
+ |
try { |
828 |
+ |
sync.acquire(1); |
829 |
+ |
threadAssertTrue(sync.hasWaiters(c)); |
830 |
+ |
threadAssertEquals(1, sync.getWaitQueueLength(c)); |
831 |
+ |
c.await(); |
832 |
+ |
sync.release(1); |
833 |
+ |
} |
834 |
+ |
catch(InterruptedException e) { |
835 |
+ |
threadUnexpectedException(); |
836 |
+ |
} |
837 |
+ |
} |
838 |
+ |
}); |
839 |
+ |
|
840 |
+ |
try { |
841 |
+ |
t1.start(); |
842 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
843 |
+ |
t2.start(); |
844 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
845 |
+ |
sync.acquire(1); |
846 |
+ |
assertTrue(sync.hasWaiters(c)); |
847 |
+ |
assertEquals(2, sync.getWaitQueueLength(c)); |
848 |
+ |
c.signalAll(); |
849 |
+ |
sync.release(1); |
850 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
851 |
+ |
sync.acquire(1); |
852 |
+ |
assertFalse(sync.hasWaiters(c)); |
853 |
+ |
assertEquals(0, sync.getWaitQueueLength(c)); |
854 |
+ |
sync.release(1); |
855 |
+ |
t1.join(SHORT_DELAY_MS); |
856 |
+ |
t2.join(SHORT_DELAY_MS); |
857 |
+ |
assertFalse(t1.isAlive()); |
858 |
+ |
assertFalse(t2.isAlive()); |
859 |
+ |
} |
860 |
+ |
catch (Exception ex) { |
861 |
+ |
unexpectedException(); |
862 |
+ |
} |
863 |
+ |
} |
864 |
+ |
|
865 |
+ |
/** |
866 |
+ |
* getWaitingThreads returns only and all waiting threads |
867 |
+ |
*/ |
868 |
+ |
public void testGetWaitingThreads() { |
869 |
+ |
final Mutex sync = new Mutex(); |
870 |
+ |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
871 |
+ |
Thread t1 = new Thread(new Runnable() { |
872 |
+ |
public void run() { |
873 |
+ |
try { |
874 |
+ |
sync.acquire(1); |
875 |
+ |
threadAssertTrue(sync.getWaitingThreads(c).isEmpty()); |
876 |
+ |
c.await(); |
877 |
+ |
sync.release(1); |
878 |
+ |
} |
879 |
+ |
catch(InterruptedException e) { |
880 |
+ |
threadUnexpectedException(); |
881 |
+ |
} |
882 |
+ |
} |
883 |
+ |
}); |
884 |
+ |
|
885 |
+ |
Thread t2 = new Thread(new Runnable() { |
886 |
+ |
public void run() { |
887 |
+ |
try { |
888 |
+ |
sync.acquire(1); |
889 |
+ |
threadAssertFalse(sync.getWaitingThreads(c).isEmpty()); |
890 |
+ |
c.await(); |
891 |
+ |
sync.release(1); |
892 |
+ |
} |
893 |
+ |
catch(InterruptedException e) { |
894 |
+ |
threadUnexpectedException(); |
895 |
+ |
} |
896 |
+ |
} |
897 |
+ |
}); |
898 |
+ |
|
899 |
+ |
try { |
900 |
+ |
sync.acquire(1); |
901 |
+ |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
902 |
+ |
sync.release(1); |
903 |
+ |
t1.start(); |
904 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
905 |
+ |
t2.start(); |
906 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
907 |
+ |
sync.acquire(1); |
908 |
+ |
assertTrue(sync.hasWaiters(c)); |
909 |
+ |
assertTrue(sync.getWaitingThreads(c).contains(t1)); |
910 |
+ |
assertTrue(sync.getWaitingThreads(c).contains(t2)); |
911 |
+ |
c.signalAll(); |
912 |
+ |
sync.release(1); |
913 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
914 |
+ |
sync.acquire(1); |
915 |
+ |
assertFalse(sync.hasWaiters(c)); |
916 |
+ |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
917 |
+ |
sync.release(1); |
918 |
+ |
t1.join(SHORT_DELAY_MS); |
919 |
+ |
t2.join(SHORT_DELAY_MS); |
920 |
+ |
assertFalse(t1.isAlive()); |
921 |
+ |
assertFalse(t2.isAlive()); |
922 |
+ |
} |
923 |
+ |
catch (Exception ex) { |
924 |
+ |
unexpectedException(); |
925 |
+ |
} |
926 |
+ |
} |
927 |
+ |
|
928 |
+ |
|
929 |
+ |
|
930 |
+ |
/** |
931 |
+ |
* awaitUninterruptibly doesn't abort on interrupt |
932 |
+ |
*/ |
933 |
+ |
public void testAwaitUninterruptibly() { |
934 |
+ |
final Mutex sync = new Mutex(); |
935 |
+ |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
936 |
+ |
Thread t = new Thread(new Runnable() { |
937 |
+ |
public void run() { |
938 |
+ |
sync.acquire(1); |
939 |
+ |
c.awaitUninterruptibly(); |
940 |
+ |
sync.release(1); |
941 |
+ |
} |
942 |
+ |
}); |
943 |
+ |
|
944 |
+ |
try { |
945 |
+ |
t.start(); |
946 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
947 |
+ |
t.interrupt(); |
948 |
+ |
sync.acquire(1); |
949 |
+ |
c.signal(); |
950 |
+ |
sync.release(1); |
951 |
+ |
assert(t.isInterrupted()); |
952 |
+ |
t.join(SHORT_DELAY_MS); |
953 |
+ |
assertFalse(t.isAlive()); |
954 |
+ |
} |
955 |
+ |
catch (Exception ex) { |
956 |
+ |
unexpectedException(); |
957 |
+ |
} |
958 |
+ |
} |
959 |
+ |
|
960 |
+ |
/** |
961 |
+ |
* await is interruptible |
962 |
+ |
*/ |
963 |
+ |
public void testAwait_Interrupt() { |
964 |
+ |
final Mutex sync = new Mutex(); |
965 |
+ |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
966 |
+ |
Thread t = new Thread(new Runnable() { |
967 |
+ |
public void run() { |
968 |
+ |
try { |
969 |
+ |
sync.acquire(1); |
970 |
+ |
c.await(); |
971 |
+ |
sync.release(1); |
972 |
+ |
threadShouldThrow(); |
973 |
+ |
} |
974 |
+ |
catch(InterruptedException success) { |
975 |
+ |
} |
976 |
+ |
} |
977 |
+ |
}); |
978 |
+ |
|
979 |
+ |
try { |
980 |
+ |
t.start(); |
981 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
982 |
+ |
t.interrupt(); |
983 |
+ |
t.join(SHORT_DELAY_MS); |
984 |
+ |
assertFalse(t.isAlive()); |
985 |
+ |
} |
986 |
+ |
catch (Exception ex) { |
987 |
+ |
unexpectedException(); |
988 |
+ |
} |
989 |
+ |
} |
990 |
+ |
|
991 |
+ |
/** |
992 |
+ |
* awaitNanos is interruptible |
993 |
+ |
*/ |
994 |
+ |
public void testAwaitNanos_Interrupt() { |
995 |
+ |
final Mutex sync = new Mutex(); |
996 |
+ |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
997 |
+ |
Thread t = new Thread(new Runnable() { |
998 |
+ |
public void run() { |
999 |
+ |
try { |
1000 |
+ |
sync.acquire(1); |
1001 |
+ |
c.awaitNanos(1000 * 1000 * 1000); // 1 sec |
1002 |
+ |
sync.release(1); |
1003 |
+ |
threadShouldThrow(); |
1004 |
+ |
} |
1005 |
+ |
catch(InterruptedException success) { |
1006 |
+ |
} |
1007 |
+ |
} |
1008 |
+ |
}); |
1009 |
+ |
|
1010 |
+ |
try { |
1011 |
+ |
t.start(); |
1012 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
1013 |
+ |
t.interrupt(); |
1014 |
+ |
t.join(SHORT_DELAY_MS); |
1015 |
+ |
assertFalse(t.isAlive()); |
1016 |
+ |
} |
1017 |
+ |
catch (Exception ex) { |
1018 |
+ |
unexpectedException(); |
1019 |
+ |
} |
1020 |
+ |
} |
1021 |
+ |
|
1022 |
+ |
/** |
1023 |
+ |
* awaitUntil is interruptible |
1024 |
+ |
*/ |
1025 |
+ |
public void testAwaitUntil_Interrupt() { |
1026 |
+ |
final Mutex sync = new Mutex(); |
1027 |
+ |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
1028 |
+ |
Thread t = new Thread(new Runnable() { |
1029 |
+ |
public void run() { |
1030 |
+ |
try { |
1031 |
+ |
sync.acquire(1); |
1032 |
+ |
java.util.Date d = new java.util.Date(); |
1033 |
+ |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
1034 |
+ |
sync.release(1); |
1035 |
+ |
threadShouldThrow(); |
1036 |
+ |
} |
1037 |
+ |
catch(InterruptedException success) { |
1038 |
+ |
} |
1039 |
+ |
} |
1040 |
+ |
}); |
1041 |
+ |
|
1042 |
+ |
try { |
1043 |
+ |
t.start(); |
1044 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
1045 |
+ |
t.interrupt(); |
1046 |
+ |
t.join(SHORT_DELAY_MS); |
1047 |
+ |
assertFalse(t.isAlive()); |
1048 |
+ |
} |
1049 |
+ |
catch (Exception ex) { |
1050 |
+ |
unexpectedException(); |
1051 |
+ |
} |
1052 |
+ |
} |
1053 |
+ |
|
1054 |
+ |
/** |
1055 |
+ |
* signalAll wakes up all threads |
1056 |
+ |
*/ |
1057 |
+ |
public void testSignalAll() { |
1058 |
+ |
final Mutex sync = new Mutex(); |
1059 |
+ |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
1060 |
+ |
Thread t1 = new Thread(new Runnable() { |
1061 |
+ |
public void run() { |
1062 |
+ |
try { |
1063 |
+ |
sync.acquire(1); |
1064 |
+ |
c.await(); |
1065 |
+ |
sync.release(1); |
1066 |
+ |
} |
1067 |
+ |
catch(InterruptedException e) { |
1068 |
+ |
threadUnexpectedException(); |
1069 |
+ |
} |
1070 |
+ |
} |
1071 |
+ |
}); |
1072 |
+ |
|
1073 |
+ |
Thread t2 = new Thread(new Runnable() { |
1074 |
+ |
public void run() { |
1075 |
+ |
try { |
1076 |
+ |
sync.acquire(1); |
1077 |
+ |
c.await(); |
1078 |
+ |
sync.release(1); |
1079 |
+ |
} |
1080 |
+ |
catch(InterruptedException e) { |
1081 |
+ |
threadUnexpectedException(); |
1082 |
+ |
} |
1083 |
+ |
} |
1084 |
+ |
}); |
1085 |
+ |
|
1086 |
+ |
try { |
1087 |
+ |
t1.start(); |
1088 |
+ |
t2.start(); |
1089 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
1090 |
+ |
sync.acquire(1); |
1091 |
+ |
c.signalAll(); |
1092 |
+ |
sync.release(1); |
1093 |
+ |
t1.join(SHORT_DELAY_MS); |
1094 |
+ |
t2.join(SHORT_DELAY_MS); |
1095 |
+ |
assertFalse(t1.isAlive()); |
1096 |
+ |
assertFalse(t2.isAlive()); |
1097 |
+ |
} |
1098 |
+ |
catch (Exception ex) { |
1099 |
+ |
unexpectedException(); |
1100 |
+ |
} |
1101 |
+ |
} |
1102 |
+ |
|
1103 |
+ |
|
1104 |
+ |
/** |
1105 |
|
* toString indicates current state |
1106 |
|
*/ |
1107 |
|
public void testToString() { |
1108 |
< |
Mutex lock = new Mutex(); |
1109 |
< |
String us = lock.toString(); |
1108 |
> |
Mutex sync = new Mutex(); |
1109 |
> |
String us = sync.toString(); |
1110 |
|
assertTrue(us.indexOf("State = 0") >= 0); |
1111 |
< |
lock.acquireExclusiveUninterruptibly(1); |
1112 |
< |
String ls = lock.toString(); |
1111 |
> |
sync.acquire(1); |
1112 |
> |
String ls = sync.toString(); |
1113 |
|
assertTrue(ls.indexOf("State = 1") >= 0); |
1114 |
|
} |
1115 |
|
|
1118 |
|
*/ |
1119 |
|
public void testSerialization() { |
1120 |
|
Mutex l = new Mutex(); |
1121 |
< |
l.acquireExclusiveUninterruptibly(1); |
1122 |
< |
assertTrue(l.isLocked()); |
1121 |
> |
l.acquire(1); |
1122 |
> |
assertTrue(l.isHeldExclusively()); |
1123 |
|
|
1124 |
|
try { |
1125 |
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
1130 |
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1131 |
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1132 |
|
Mutex r = (Mutex) in.readObject(); |
1133 |
< |
assertTrue(r.isLocked()); |
1133 |
> |
assertTrue(r.isHeldExclusively()); |
1134 |
|
} catch(Exception e){ |
1135 |
|
e.printStackTrace(); |
1136 |
|
unexpectedException(); |
1139 |
|
|
1140 |
|
|
1141 |
|
/** |
1142 |
< |
* Latch isSignalled initially returns false, then true after release |
1142 |
> |
* tryReleaseShared setting state changes getState |
1143 |
|
*/ |
1144 |
< |
public void testLatchIsSignalled() { |
1144 |
> |
public void testGetStateWithReleaseShared() { |
1145 |
|
final BooleanLatch l = new BooleanLatch(); |
1146 |
|
assertFalse(l.isSignalled()); |
1147 |
|
l.releaseShared(0); |
1149 |
|
} |
1150 |
|
|
1151 |
|
/** |
1152 |
< |
* release and has no effect when already signalled |
1152 |
> |
* releaseShared has no effect when already signalled |
1153 |
|
*/ |
1154 |
|
public void testReleaseShared() { |
1155 |
|
final BooleanLatch l = new BooleanLatch(); |
1200 |
|
public void run() { |
1201 |
|
try { |
1202 |
|
threadAssertFalse(l.isSignalled()); |
1203 |
< |
threadAssertTrue(l.acquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); |
1203 |
> |
threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); |
1204 |
|
threadAssertTrue(l.isSignalled()); |
1205 |
|
|
1206 |
|
} catch(InterruptedException e){ |
1253 |
|
public void run() { |
1254 |
|
try { |
1255 |
|
threadAssertFalse(l.isSignalled()); |
1256 |
< |
l.acquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); |
1256 |
> |
l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); |
1257 |
|
threadShouldThrow(); |
1258 |
|
} catch(InterruptedException success){} |
1259 |
|
} |
1278 |
|
public void run() { |
1279 |
|
try { |
1280 |
|
threadAssertFalse(l.isSignalled()); |
1281 |
< |
threadAssertFalse(l.acquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); |
1281 |
> |
threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); |
1282 |
|
} catch(InterruptedException ie){ |
1283 |
|
threadUnexpectedException(); |
1284 |
|
} |