44 |
|
} |
45 |
|
|
46 |
|
public AbstractQueuedSynchronizer.ConditionObject newCondition() { return new AbstractQueuedSynchronizer.ConditionObject(); } |
47 |
– |
|
48 |
– |
public void lock() { |
49 |
– |
acquire(1); |
50 |
– |
} |
47 |
|
|
48 |
|
} |
49 |
|
|
67 |
|
/** |
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.acquireInterruptibly(1); |
75 |
> |
sync.acquireInterruptibly(1); |
76 |
|
} catch(InterruptedException success){} |
77 |
|
} |
78 |
|
} |
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.acquireInterruptibly(1); |
90 |
> |
sync.acquireInterruptibly(1); |
91 |
|
threadShouldThrow(); |
92 |
|
} catch(InterruptedException success){} |
93 |
|
} |
102 |
|
} |
103 |
|
|
104 |
|
/** |
105 |
< |
* acquiring released lock succeeds |
105 |
> |
* acquiring released sync succeeds |
106 |
|
*/ |
107 |
|
public void testAcquire() { |
108 |
|
Mutex rl = new Mutex(); |
109 |
|
rl.acquire(1); |
110 |
|
assertTrue(rl.isHeldExclusively()); |
111 |
|
rl.release(1); |
112 |
+ |
assertFalse(rl.isHeldExclusively()); |
113 |
|
} |
114 |
|
|
115 |
|
/** |
116 |
< |
* tryAcquire on an released lock succeeds |
116 |
> |
* tryAcquire on an released sync succeeds |
117 |
|
*/ |
118 |
|
public void testTryAcquire() { |
119 |
|
Mutex rl = new Mutex(); |
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.acquire(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.release(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.acquire(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.release(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.acquire(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.release(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(lock.hasContended()); |
238 |
< |
lock.acquire(1); |
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 |
> |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
299 |
> |
sync.acquire(1); |
300 |
> |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
301 |
> |
t1.start(); |
302 |
> |
Thread.sleep(SHORT_DELAY_MS); |
303 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
304 |
> |
t2.start(); |
305 |
> |
Thread.sleep(SHORT_DELAY_MS); |
306 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
307 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
308 |
> |
t1.interrupt(); |
309 |
> |
Thread.sleep(SHORT_DELAY_MS); |
310 |
> |
assertFalse(sync.getExclusiveQueuedThreads().contains(t1)); |
311 |
> |
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
312 |
> |
sync.release(1); |
313 |
> |
Thread.sleep(SHORT_DELAY_MS); |
314 |
> |
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
315 |
> |
t1.join(); |
316 |
> |
t2.join(); |
317 |
> |
} catch(Exception e){ |
318 |
> |
unexpectedException(); |
319 |
> |
} |
320 |
> |
} |
321 |
> |
|
322 |
> |
/** |
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(lock.hasContended()); |
335 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
336 |
|
t2.start(); |
337 |
|
Thread.sleep(SHORT_DELAY_MS); |
338 |
< |
assertTrue(lock.hasContended()); |
338 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
339 |
|
t1.interrupt(); |
340 |
|
Thread.sleep(SHORT_DELAY_MS); |
341 |
< |
assertTrue(lock.hasContended()); |
342 |
< |
lock.release(1); |
341 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
342 |
> |
sync.release(1); |
343 |
|
Thread.sleep(SHORT_DELAY_MS); |
344 |
< |
assertTrue(lock.hasContended()); |
344 |
> |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
345 |
|
t1.join(); |
346 |
|
t2.join(); |
347 |
|
} catch(Exception e){ |
353 |
|
* tryAcquireNanos is interruptible. |
354 |
|
*/ |
355 |
|
public void testInterruptedException2() { |
356 |
< |
final Mutex lock = new Mutex(); |
357 |
< |
lock.acquire(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.tryAcquireNanos(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 |
< |
* TryAcquire on a locked lock fails |
376 |
> |
* TryAcquire on exclusively held sync fails |
377 |
|
*/ |
378 |
< |
public void testTryAcquireWhenLocked() { |
379 |
< |
final Mutex lock = new Mutex(); |
380 |
< |
lock.acquire(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.tryAcquire(1)); |
383 |
> |
threadAssertFalse(sync.tryAcquire(1)); |
384 |
|
} |
385 |
|
}); |
386 |
|
try { |
387 |
|
t.start(); |
388 |
|
t.join(); |
389 |
< |
lock.release(1); |
389 |
> |
sync.release(1); |
390 |
|
} catch(Exception e){ |
391 |
|
unexpectedException(); |
392 |
|
} |
393 |
|
} |
394 |
|
|
395 |
|
/** |
396 |
< |
* tryAcquireNanos on a locked lock times out |
396 |
> |
* tryAcquireNanos on an exclusively held sync times out |
397 |
|
*/ |
398 |
|
public void testAcquireNanos_Timeout() { |
399 |
< |
final Mutex lock = new Mutex(); |
400 |
< |
lock.acquire(1); |
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.tryAcquireNanos(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.release(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.acquire(1); |
426 |
< |
assertTrue(lock.isHeldExclusively()); |
427 |
< |
lock.release(1); |
428 |
< |
assertFalse(lock.isHeldExclusively()); |
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.acquire(1); |
431 |
> |
sync.acquire(1); |
432 |
|
try { |
433 |
|
Thread.sleep(SMALL_DELAY_MS); |
434 |
|
} |
435 |
|
catch(Exception e) { |
436 |
|
threadUnexpectedException(); |
437 |
|
} |
438 |
< |
lock.release(1); |
438 |
> |
sync.release(1); |
439 |
|
} |
440 |
|
}); |
441 |
|
try { |
442 |
|
t.start(); |
443 |
|
Thread.sleep(SHORT_DELAY_MS); |
444 |
< |
assertTrue(lock.isHeldExclusively()); |
444 |
> |
assertTrue(sync.isHeldExclusively()); |
445 |
|
t.join(); |
446 |
< |
assertFalse(lock.isHeldExclusively()); |
446 |
> |
assertFalse(sync.isHeldExclusively()); |
447 |
|
} catch(Exception e){ |
448 |
|
unexpectedException(); |
449 |
|
} |
454 |
|
* acquireInterruptibly is interruptible. |
455 |
|
*/ |
456 |
|
public void testAcquireInterruptibly1() { |
457 |
< |
final Mutex lock = new Mutex(); |
458 |
< |
lock.acquire(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.release(1); |
463 |
> |
sync.release(1); |
464 |
|
t.join(); |
465 |
|
} catch(Exception e){ |
466 |
|
unexpectedException(); |
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.acquireInterruptibly(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.isHeldExclusively()); |
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 AbstractQueuedSynchronizer.ConditionObject 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 AbstractQueuedSynchronizer.ConditionObject 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 AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition(); |
540 |
> |
final Mutex sync = new Mutex(); |
541 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
542 |
|
try { |
543 |
< |
lock.acquire(1); |
543 |
> |
sync.acquire(1); |
544 |
|
long t = c.awaitNanos(100); |
545 |
|
assertTrue(t <= 0); |
546 |
< |
lock.release(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 AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition(); |
557 |
> |
final Mutex sync = new Mutex(); |
558 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
559 |
|
try { |
560 |
< |
lock.acquire(1); |
560 |
> |
sync.acquire(1); |
561 |
|
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
562 |
< |
lock.release(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 AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition(); |
573 |
> |
final Mutex sync = new Mutex(); |
574 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
575 |
|
try { |
576 |
< |
lock.acquire(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.release(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 AbstractQueuedSynchronizer.ConditionObject 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 |
< |
lock.acquire(1); |
595 |
> |
sync.acquire(1); |
596 |
|
c.await(); |
597 |
< |
lock.release(1); |
597 |
> |
sync.release(1); |
598 |
|
} |
599 |
|
catch(InterruptedException e) { |
600 |
|
threadUnexpectedException(); |
605 |
|
try { |
606 |
|
t.start(); |
607 |
|
Thread.sleep(SHORT_DELAY_MS); |
608 |
< |
lock.acquire(1); |
608 |
> |
sync.acquire(1); |
609 |
|
c.signal(); |
610 |
< |
lock.release(1); |
610 |
> |
sync.release(1); |
611 |
|
t.join(SHORT_DELAY_MS); |
612 |
|
assertFalse(t.isAlive()); |
613 |
|
} |
622 |
|
* hasWaiters throws NPE if null |
623 |
|
*/ |
624 |
|
public void testHasWaitersNPE() { |
625 |
< |
final Mutex lock = new Mutex(); |
625 |
> |
final Mutex sync = new Mutex(); |
626 |
|
try { |
627 |
< |
lock.hasWaiters(null); |
627 |
> |
sync.hasWaiters(null); |
628 |
|
shouldThrow(); |
629 |
|
} catch (NullPointerException success) { |
630 |
|
} catch (Exception ex) { |
636 |
|
* getWaitQueueLength throws NPE if null |
637 |
|
*/ |
638 |
|
public void testGetWaitQueueLengthNPE() { |
639 |
< |
final Mutex lock = new Mutex(); |
639 |
> |
final Mutex sync = new Mutex(); |
640 |
|
try { |
641 |
< |
lock.getWaitQueueLength(null); |
641 |
> |
sync.getWaitQueueLength(null); |
642 |
|
shouldThrow(); |
643 |
|
} catch (NullPointerException success) { |
644 |
|
} catch (Exception ex) { |
651 |
|
* getWaitingThreads throws NPE if null |
652 |
|
*/ |
653 |
|
public void testGetWaitingThreadsNPE() { |
654 |
< |
final Mutex lock = new Mutex(); |
654 |
> |
final Mutex sync = new Mutex(); |
655 |
|
try { |
656 |
< |
lock.getWaitingThreads(null); |
656 |
> |
sync.getWaitingThreads(null); |
657 |
|
shouldThrow(); |
658 |
|
} catch (NullPointerException success) { |
659 |
|
} catch (Exception ex) { |
666 |
|
* hasWaiters throws IAE if not owned |
667 |
|
*/ |
668 |
|
public void testHasWaitersIAE() { |
669 |
< |
final Mutex lock = new Mutex(); |
670 |
< |
final AbstractQueuedSynchronizer.ConditionObject c = (lock.newCondition()); |
671 |
< |
final Mutex lock2 = new Mutex(); |
669 |
> |
final Mutex sync = new Mutex(); |
670 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
671 |
> |
final Mutex sync2 = new Mutex(); |
672 |
|
try { |
673 |
< |
lock2.hasWaiters(c); |
673 |
> |
sync2.hasWaiters(c); |
674 |
|
shouldThrow(); |
675 |
|
} catch (IllegalArgumentException success) { |
676 |
|
} catch (Exception ex) { |
679 |
|
} |
680 |
|
|
681 |
|
/** |
682 |
< |
* hasWaiters throws IMSE if not locked |
682 |
> |
* hasWaiters throws IMSE if not synced |
683 |
|
*/ |
684 |
|
public void testHasWaitersIMSE() { |
685 |
< |
final Mutex lock = new Mutex(); |
686 |
< |
final AbstractQueuedSynchronizer.ConditionObject c = (lock.newCondition()); |
685 |
> |
final Mutex sync = new Mutex(); |
686 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
687 |
|
try { |
688 |
< |
lock.hasWaiters(c); |
688 |
> |
sync.hasWaiters(c); |
689 |
|
shouldThrow(); |
690 |
|
} catch (IllegalMonitorStateException success) { |
691 |
|
} catch (Exception ex) { |
698 |
|
* getWaitQueueLength throws IAE if not owned |
699 |
|
*/ |
700 |
|
public void testGetWaitQueueLengthIAE() { |
701 |
< |
final Mutex lock = new Mutex(); |
702 |
< |
final AbstractQueuedSynchronizer.ConditionObject c = (lock.newCondition()); |
703 |
< |
final Mutex lock2 = new Mutex(); |
701 |
> |
final Mutex sync = new Mutex(); |
702 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
703 |
> |
final Mutex sync2 = new Mutex(); |
704 |
|
try { |
705 |
< |
lock2.getWaitQueueLength(c); |
705 |
> |
sync2.getWaitQueueLength(c); |
706 |
|
shouldThrow(); |
707 |
|
} catch (IllegalArgumentException success) { |
708 |
|
} catch (Exception ex) { |
711 |
|
} |
712 |
|
|
713 |
|
/** |
714 |
< |
* getWaitQueueLength throws IMSE if not locked |
714 |
> |
* getWaitQueueLength throws IMSE if not synced |
715 |
|
*/ |
716 |
|
public void testGetWaitQueueLengthIMSE() { |
717 |
< |
final Mutex lock = new Mutex(); |
718 |
< |
final AbstractQueuedSynchronizer.ConditionObject c = (lock.newCondition()); |
717 |
> |
final Mutex sync = new Mutex(); |
718 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
719 |
|
try { |
720 |
< |
lock.getWaitQueueLength(c); |
720 |
> |
sync.getWaitQueueLength(c); |
721 |
|
shouldThrow(); |
722 |
|
} catch (IllegalMonitorStateException success) { |
723 |
|
} catch (Exception ex) { |
730 |
|
* getWaitingThreads throws IAE if not owned |
731 |
|
*/ |
732 |
|
public void testGetWaitingThreadsIAE() { |
733 |
< |
final Mutex lock = new Mutex(); |
734 |
< |
final AbstractQueuedSynchronizer.ConditionObject c = (lock.newCondition()); |
735 |
< |
final Mutex lock2 = new Mutex(); |
733 |
> |
final Mutex sync = new Mutex(); |
734 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
735 |
> |
final Mutex sync2 = new Mutex(); |
736 |
|
try { |
737 |
< |
lock2.getWaitingThreads(c); |
737 |
> |
sync2.getWaitingThreads(c); |
738 |
|
shouldThrow(); |
739 |
|
} catch (IllegalArgumentException success) { |
740 |
|
} catch (Exception ex) { |
743 |
|
} |
744 |
|
|
745 |
|
/** |
746 |
< |
* getWaitingThreads throws IMSE if not locked |
746 |
> |
* getWaitingThreads throws IMSE if not synced |
747 |
|
*/ |
748 |
|
public void testGetWaitingThreadsIMSE() { |
749 |
< |
final Mutex lock = new Mutex(); |
750 |
< |
final AbstractQueuedSynchronizer.ConditionObject c = (lock.newCondition()); |
749 |
> |
final Mutex sync = new Mutex(); |
750 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
751 |
|
try { |
752 |
< |
lock.getWaitingThreads(c); |
752 |
> |
sync.getWaitingThreads(c); |
753 |
|
shouldThrow(); |
754 |
|
} catch (IllegalMonitorStateException success) { |
755 |
|
} catch (Exception ex) { |
763 |
|
* hasWaiters returns true when a thread is waiting, else false |
764 |
|
*/ |
765 |
|
public void testHasWaiters() { |
766 |
< |
final Mutex lock = new Mutex(); |
767 |
< |
final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition(); |
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.acquire(1); |
772 |
< |
threadAssertFalse(lock.hasWaiters(c)); |
773 |
< |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
771 |
> |
sync.acquire(1); |
772 |
> |
threadAssertFalse(sync.hasWaiters(c)); |
773 |
> |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
774 |
|
c.await(); |
775 |
< |
lock.release(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.acquire(1); |
787 |
< |
assertTrue(lock.hasWaiters(c)); |
788 |
< |
assertEquals(1, lock.getWaitQueueLength(c)); |
786 |
> |
sync.acquire(1); |
787 |
> |
assertTrue(sync.hasWaiters(c)); |
788 |
> |
assertEquals(1, sync.getWaitQueueLength(c)); |
789 |
|
c.signal(); |
790 |
< |
lock.release(1); |
790 |
> |
sync.release(1); |
791 |
|
Thread.sleep(SHORT_DELAY_MS); |
792 |
< |
lock.acquire(1); |
793 |
< |
assertFalse(lock.hasWaiters(c)); |
794 |
< |
assertEquals(0, lock.getWaitQueueLength(c)); |
795 |
< |
lock.release(1); |
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 |
|
} |
805 |
|
* getWaitQueueLength returns number of waiting threads |
806 |
|
*/ |
807 |
|
public void testGetWaitQueueLength() { |
808 |
< |
final Mutex lock = new Mutex(); |
809 |
< |
final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition(); |
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 |
< |
lock.acquire(1); |
814 |
< |
threadAssertFalse(lock.hasWaiters(c)); |
815 |
< |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
813 |
> |
sync.acquire(1); |
814 |
> |
threadAssertFalse(sync.hasWaiters(c)); |
815 |
> |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
816 |
|
c.await(); |
817 |
< |
lock.release(1); |
817 |
> |
sync.release(1); |
818 |
|
} |
819 |
|
catch(InterruptedException e) { |
820 |
|
threadUnexpectedException(); |
825 |
|
Thread t2 = new Thread(new Runnable() { |
826 |
|
public void run() { |
827 |
|
try { |
828 |
< |
lock.acquire(1); |
829 |
< |
threadAssertTrue(lock.hasWaiters(c)); |
830 |
< |
threadAssertEquals(1, lock.getWaitQueueLength(c)); |
828 |
> |
sync.acquire(1); |
829 |
> |
threadAssertTrue(sync.hasWaiters(c)); |
830 |
> |
threadAssertEquals(1, sync.getWaitQueueLength(c)); |
831 |
|
c.await(); |
832 |
< |
lock.release(1); |
832 |
> |
sync.release(1); |
833 |
|
} |
834 |
|
catch(InterruptedException e) { |
835 |
|
threadUnexpectedException(); |
842 |
|
Thread.sleep(SHORT_DELAY_MS); |
843 |
|
t2.start(); |
844 |
|
Thread.sleep(SHORT_DELAY_MS); |
845 |
< |
lock.acquire(1); |
846 |
< |
assertTrue(lock.hasWaiters(c)); |
847 |
< |
assertEquals(2, lock.getWaitQueueLength(c)); |
845 |
> |
sync.acquire(1); |
846 |
> |
assertTrue(sync.hasWaiters(c)); |
847 |
> |
assertEquals(2, sync.getWaitQueueLength(c)); |
848 |
|
c.signalAll(); |
849 |
< |
lock.release(1); |
849 |
> |
sync.release(1); |
850 |
|
Thread.sleep(SHORT_DELAY_MS); |
851 |
< |
lock.acquire(1); |
852 |
< |
assertFalse(lock.hasWaiters(c)); |
853 |
< |
assertEquals(0, lock.getWaitQueueLength(c)); |
854 |
< |
lock.release(1); |
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()); |
866 |
|
* getWaitingThreads returns only and all waiting threads |
867 |
|
*/ |
868 |
|
public void testGetWaitingThreads() { |
869 |
< |
final Mutex lock = new Mutex(); |
870 |
< |
final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition(); |
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 |
< |
lock.acquire(1); |
875 |
< |
threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); |
874 |
> |
sync.acquire(1); |
875 |
> |
threadAssertTrue(sync.getWaitingThreads(c).isEmpty()); |
876 |
|
c.await(); |
877 |
< |
lock.release(1); |
877 |
> |
sync.release(1); |
878 |
|
} |
879 |
|
catch(InterruptedException e) { |
880 |
|
threadUnexpectedException(); |
885 |
|
Thread t2 = new Thread(new Runnable() { |
886 |
|
public void run() { |
887 |
|
try { |
888 |
< |
lock.acquire(1); |
889 |
< |
threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); |
888 |
> |
sync.acquire(1); |
889 |
> |
threadAssertFalse(sync.getWaitingThreads(c).isEmpty()); |
890 |
|
c.await(); |
891 |
< |
lock.release(1); |
891 |
> |
sync.release(1); |
892 |
|
} |
893 |
|
catch(InterruptedException e) { |
894 |
|
threadUnexpectedException(); |
897 |
|
}); |
898 |
|
|
899 |
|
try { |
900 |
< |
lock.acquire(1); |
901 |
< |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
902 |
< |
lock.release(1); |
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 |
< |
lock.acquire(1); |
908 |
< |
assertTrue(lock.hasWaiters(c)); |
909 |
< |
assertTrue(lock.getWaitingThreads(c).contains(t1)); |
910 |
< |
assertTrue(lock.getWaitingThreads(c).contains(t2)); |
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 |
< |
lock.release(1); |
912 |
> |
sync.release(1); |
913 |
|
Thread.sleep(SHORT_DELAY_MS); |
914 |
< |
lock.acquire(1); |
915 |
< |
assertFalse(lock.hasWaiters(c)); |
916 |
< |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
917 |
< |
lock.release(1); |
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()); |
931 |
|
* awaitUninterruptibly doesn't abort on interrupt |
932 |
|
*/ |
933 |
|
public void testAwaitUninterruptibly() { |
934 |
< |
final Mutex lock = new Mutex(); |
935 |
< |
final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition(); |
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 |
< |
lock.acquire(1); |
938 |
> |
sync.acquire(1); |
939 |
|
c.awaitUninterruptibly(); |
940 |
< |
lock.release(1); |
940 |
> |
sync.release(1); |
941 |
|
} |
942 |
|
}); |
943 |
|
|
945 |
|
t.start(); |
946 |
|
Thread.sleep(SHORT_DELAY_MS); |
947 |
|
t.interrupt(); |
948 |
< |
lock.acquire(1); |
948 |
> |
sync.acquire(1); |
949 |
|
c.signal(); |
950 |
< |
lock.release(1); |
950 |
> |
sync.release(1); |
951 |
|
assert(t.isInterrupted()); |
952 |
|
t.join(SHORT_DELAY_MS); |
953 |
|
assertFalse(t.isAlive()); |
961 |
|
* await is interruptible |
962 |
|
*/ |
963 |
|
public void testAwait_Interrupt() { |
964 |
< |
final Mutex lock = new Mutex(); |
965 |
< |
final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition(); |
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 |
< |
lock.acquire(1); |
969 |
> |
sync.acquire(1); |
970 |
|
c.await(); |
971 |
< |
lock.release(1); |
971 |
> |
sync.release(1); |
972 |
|
threadShouldThrow(); |
973 |
|
} |
974 |
|
catch(InterruptedException success) { |
992 |
|
* awaitNanos is interruptible |
993 |
|
*/ |
994 |
|
public void testAwaitNanos_Interrupt() { |
995 |
< |
final Mutex lock = new Mutex(); |
996 |
< |
final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition(); |
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 |
< |
lock.acquire(1); |
1000 |
> |
sync.acquire(1); |
1001 |
|
c.awaitNanos(1000 * 1000 * 1000); // 1 sec |
1002 |
< |
lock.release(1); |
1002 |
> |
sync.release(1); |
1003 |
|
threadShouldThrow(); |
1004 |
|
} |
1005 |
|
catch(InterruptedException success) { |
1023 |
|
* awaitUntil is interruptible |
1024 |
|
*/ |
1025 |
|
public void testAwaitUntil_Interrupt() { |
1026 |
< |
final Mutex lock = new Mutex(); |
1027 |
< |
final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition(); |
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 |
< |
lock.acquire(1); |
1031 |
> |
sync.acquire(1); |
1032 |
|
java.util.Date d = new java.util.Date(); |
1033 |
|
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
1034 |
< |
lock.release(1); |
1034 |
> |
sync.release(1); |
1035 |
|
threadShouldThrow(); |
1036 |
|
} |
1037 |
|
catch(InterruptedException success) { |
1055 |
|
* signalAll wakes up all threads |
1056 |
|
*/ |
1057 |
|
public void testSignalAll() { |
1058 |
< |
final Mutex lock = new Mutex(); |
1059 |
< |
final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition(); |
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 |
< |
lock.acquire(1); |
1063 |
> |
sync.acquire(1); |
1064 |
|
c.await(); |
1065 |
< |
lock.release(1); |
1065 |
> |
sync.release(1); |
1066 |
|
} |
1067 |
|
catch(InterruptedException e) { |
1068 |
|
threadUnexpectedException(); |
1073 |
|
Thread t2 = new Thread(new Runnable() { |
1074 |
|
public void run() { |
1075 |
|
try { |
1076 |
< |
lock.acquire(1); |
1076 |
> |
sync.acquire(1); |
1077 |
|
c.await(); |
1078 |
< |
lock.release(1); |
1078 |
> |
sync.release(1); |
1079 |
|
} |
1080 |
|
catch(InterruptedException e) { |
1081 |
|
threadUnexpectedException(); |
1087 |
|
t1.start(); |
1088 |
|
t2.start(); |
1089 |
|
Thread.sleep(SHORT_DELAY_MS); |
1090 |
< |
lock.acquire(1); |
1090 |
> |
sync.acquire(1); |
1091 |
|
c.signalAll(); |
1092 |
< |
lock.release(1); |
1092 |
> |
sync.release(1); |
1093 |
|
t1.join(SHORT_DELAY_MS); |
1094 |
|
t2.join(SHORT_DELAY_MS); |
1095 |
|
assertFalse(t1.isAlive()); |
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.acquire(1); |
1112 |
< |
String ls = lock.toString(); |
1111 |
> |
sync.acquire(1); |
1112 |
> |
String ls = sync.toString(); |
1113 |
|
assertTrue(ls.indexOf("State = 1") >= 0); |
1114 |
|
} |
1115 |
|
|
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(); |