10 |
|
import junit.framework.*; |
11 |
|
import java.util.*; |
12 |
|
import java.util.concurrent.*; |
13 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
|
import java.util.concurrent.locks.*; |
15 |
|
import java.io.*; |
16 |
|
|
66 |
|
} |
67 |
|
|
68 |
|
/** |
69 |
< |
* A runnable calling acquireInterruptibly |
69 |
> |
* A runnable calling acquireInterruptibly that does not expect to |
70 |
> |
* be interrupted. |
71 |
|
*/ |
72 |
< |
class InterruptibleSyncRunnable implements Runnable { |
72 |
> |
class InterruptibleSyncRunnable extends CheckedRunnable { |
73 |
|
final Mutex sync; |
74 |
|
InterruptibleSyncRunnable(Mutex l) { sync = l; } |
75 |
< |
public void run() { |
76 |
< |
try { |
75 |
< |
sync.acquireInterruptibly(1); |
76 |
< |
} catch (InterruptedException success) {} |
75 |
> |
public void realRun() throws InterruptedException { |
76 |
> |
sync.acquireInterruptibly(1); |
77 |
|
} |
78 |
|
} |
79 |
|
|
80 |
|
|
81 |
|
/** |
82 |
|
* A runnable calling acquireInterruptibly that expects to be |
83 |
< |
* interrupted |
83 |
> |
* interrupted. |
84 |
|
*/ |
85 |
< |
class InterruptedSyncRunnable implements Runnable { |
85 |
> |
class InterruptedSyncRunnable extends CheckedInterruptedRunnable { |
86 |
|
final Mutex sync; |
87 |
|
InterruptedSyncRunnable(Mutex l) { sync = l; } |
88 |
< |
public void run() { |
89 |
< |
try { |
90 |
< |
sync.acquireInterruptibly(1); |
91 |
< |
threadShouldThrow(); |
92 |
< |
} catch (InterruptedException success) {} |
88 |
> |
public void realRun() throws InterruptedException { |
89 |
> |
sync.acquireInterruptibly(1); |
90 |
|
} |
91 |
|
} |
92 |
|
|
94 |
|
* isHeldExclusively is false upon construction |
95 |
|
*/ |
96 |
|
public void testIsHeldExclusively() { |
97 |
< |
Mutex rl = new Mutex(); |
97 |
> |
Mutex rl = new Mutex(); |
98 |
|
assertFalse(rl.isHeldExclusively()); |
99 |
|
} |
100 |
|
|
102 |
|
* acquiring released sync succeeds |
103 |
|
*/ |
104 |
|
public void testAcquire() { |
105 |
< |
Mutex rl = new Mutex(); |
105 |
> |
Mutex rl = new Mutex(); |
106 |
|
rl.acquire(1); |
107 |
|
assertTrue(rl.isHeldExclusively()); |
108 |
|
rl.release(1); |
113 |
|
* tryAcquire on an released sync succeeds |
114 |
|
*/ |
115 |
|
public void testTryAcquire() { |
116 |
< |
Mutex rl = new Mutex(); |
116 |
> |
Mutex rl = new Mutex(); |
117 |
|
assertTrue(rl.tryAcquire(1)); |
118 |
|
assertTrue(rl.isHeldExclusively()); |
119 |
|
rl.release(1); |
123 |
|
* hasQueuedThreads reports whether there are waiting threads |
124 |
|
*/ |
125 |
|
public void testhasQueuedThreads() throws InterruptedException { |
126 |
< |
final Mutex sync = new Mutex(); |
126 |
> |
final Mutex sync = new Mutex(); |
127 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
128 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
129 |
|
assertFalse(sync.hasQueuedThreads()); |
148 |
|
* isQueued(null) throws NPE |
149 |
|
*/ |
150 |
|
public void testIsQueuedNPE() { |
151 |
< |
final Mutex sync = new Mutex(); |
151 |
> |
final Mutex sync = new Mutex(); |
152 |
|
try { |
153 |
|
sync.isQueued(null); |
154 |
|
shouldThrow(); |
159 |
|
* isQueued reports whether a thread is queued. |
160 |
|
*/ |
161 |
|
public void testIsQueued() throws InterruptedException { |
162 |
< |
final Mutex sync = new Mutex(); |
162 |
> |
final Mutex sync = new Mutex(); |
163 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
164 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
165 |
|
assertFalse(sync.isQueued(t1)); |
189 |
|
* getFirstQueuedThread returns first waiting thread or null if none |
190 |
|
*/ |
191 |
|
public void testGetFirstQueuedThread() throws InterruptedException { |
192 |
< |
final Mutex sync = new Mutex(); |
192 |
> |
final Mutex sync = new Mutex(); |
193 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
194 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
195 |
|
assertNull(sync.getFirstQueuedThread()); |
216 |
|
* hasContended reports false if no thread has ever blocked, else true |
217 |
|
*/ |
218 |
|
public void testHasContended() throws InterruptedException { |
219 |
< |
final Mutex sync = new Mutex(); |
219 |
> |
final Mutex sync = new Mutex(); |
220 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
221 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
222 |
|
assertFalse(sync.hasContended()); |
241 |
|
* getQueuedThreads includes waiting threads |
242 |
|
*/ |
243 |
|
public void testGetQueuedThreads() throws InterruptedException { |
244 |
< |
final Mutex sync = new Mutex(); |
244 |
> |
final Mutex sync = new Mutex(); |
245 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
246 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
247 |
|
assertTrue(sync.getQueuedThreads().isEmpty()); |
269 |
|
* getExclusiveQueuedThreads includes waiting threads |
270 |
|
*/ |
271 |
|
public void testGetExclusiveQueuedThreads() throws InterruptedException { |
272 |
< |
final Mutex sync = new Mutex(); |
272 |
> |
final Mutex sync = new Mutex(); |
273 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
274 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
275 |
|
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
297 |
|
* getSharedQueuedThreads does not include exclusively waiting threads |
298 |
|
*/ |
299 |
|
public void testGetSharedQueuedThreads() throws InterruptedException { |
300 |
< |
final Mutex sync = new Mutex(); |
300 |
> |
final Mutex sync = new Mutex(); |
301 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
302 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
303 |
|
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
322 |
|
/** |
323 |
|
* tryAcquireNanos is interruptible. |
324 |
|
*/ |
325 |
< |
public void testInterruptedException2() { |
326 |
< |
final Mutex sync = new Mutex(); |
327 |
< |
sync.acquire(1); |
328 |
< |
Thread t = new Thread(new Runnable() { |
329 |
< |
public void run() { |
330 |
< |
try { |
331 |
< |
sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); |
335 |
< |
threadShouldThrow(); |
336 |
< |
} catch (InterruptedException success) {} |
337 |
< |
} |
338 |
< |
}); |
325 |
> |
public void testInterruptedException2() throws InterruptedException { |
326 |
> |
final Mutex sync = new Mutex(); |
327 |
> |
sync.acquire(1); |
328 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
329 |
> |
public void realRun() throws InterruptedException { |
330 |
> |
sync.tryAcquireNanos(1, MILLISECONDS.toNanos(MEDIUM_DELAY_MS)); |
331 |
> |
}}); |
332 |
|
|
333 |
|
t.start(); |
334 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
335 |
|
t.interrupt(); |
336 |
+ |
t.join(); |
337 |
|
} |
338 |
|
|
339 |
|
|
341 |
|
* TryAcquire on exclusively held sync fails |
342 |
|
*/ |
343 |
|
public void testTryAcquireWhenSynced() throws InterruptedException { |
344 |
< |
final Mutex sync = new Mutex(); |
345 |
< |
sync.acquire(1); |
346 |
< |
Thread t = new Thread(new Runnable() { |
347 |
< |
public void run() { |
348 |
< |
threadAssertFalse(sync.tryAcquire(1)); |
349 |
< |
} |
355 |
< |
}); |
344 |
> |
final Mutex sync = new Mutex(); |
345 |
> |
sync.acquire(1); |
346 |
> |
Thread t = new Thread(new CheckedRunnable() { |
347 |
> |
public void realRun() { |
348 |
> |
threadAssertFalse(sync.tryAcquire(1)); |
349 |
> |
}}); |
350 |
|
|
351 |
|
t.start(); |
352 |
|
t.join(); |
357 |
|
* tryAcquireNanos on an exclusively held sync times out |
358 |
|
*/ |
359 |
|
public void testAcquireNanos_Timeout() throws InterruptedException { |
360 |
< |
final Mutex sync = new Mutex(); |
361 |
< |
sync.acquire(1); |
362 |
< |
Thread t = new Thread(new Runnable() { |
363 |
< |
public void run() { |
364 |
< |
try { |
365 |
< |
threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000)); |
366 |
< |
} catch (Exception ex) { |
373 |
< |
threadUnexpectedException(); |
374 |
< |
} |
375 |
< |
} |
376 |
< |
}); |
360 |
> |
final Mutex sync = new Mutex(); |
361 |
> |
sync.acquire(1); |
362 |
> |
Thread t = new Thread(new CheckedRunnable() { |
363 |
> |
public void realRun() throws InterruptedException { |
364 |
> |
long nanos = MILLISECONDS.toNanos(SHORT_DELAY_MS); |
365 |
> |
assertFalse(sync.tryAcquireNanos(1, nanos)); |
366 |
> |
}}); |
367 |
|
|
368 |
|
t.start(); |
369 |
|
t.join(); |
375 |
|
* getState is true when acquired and false when not |
376 |
|
*/ |
377 |
|
public void testGetState() throws InterruptedException { |
378 |
< |
final Mutex sync = new Mutex(); |
379 |
< |
sync.acquire(1); |
380 |
< |
assertTrue(sync.isHeldExclusively()); |
381 |
< |
sync.release(1); |
382 |
< |
assertFalse(sync.isHeldExclusively()); |
383 |
< |
Thread t = new Thread(new Runnable() { |
384 |
< |
public void run() { |
385 |
< |
sync.acquire(1); |
386 |
< |
try { |
387 |
< |
Thread.sleep(SMALL_DELAY_MS); |
388 |
< |
} |
399 |
< |
catch (Exception e) { |
400 |
< |
threadUnexpectedException(); |
401 |
< |
} |
402 |
< |
sync.release(1); |
403 |
< |
} |
404 |
< |
}); |
378 |
> |
final Mutex sync = new Mutex(); |
379 |
> |
sync.acquire(1); |
380 |
> |
assertTrue(sync.isHeldExclusively()); |
381 |
> |
sync.release(1); |
382 |
> |
assertFalse(sync.isHeldExclusively()); |
383 |
> |
Thread t = new Thread(new CheckedRunnable() { |
384 |
> |
public void realRun() throws InterruptedException { |
385 |
> |
sync.acquire(1); |
386 |
> |
Thread.sleep(SMALL_DELAY_MS); |
387 |
> |
sync.release(1); |
388 |
> |
}}); |
389 |
|
|
390 |
|
t.start(); |
391 |
|
Thread.sleep(SHORT_DELAY_MS); |
399 |
|
* acquireInterruptibly is interruptible. |
400 |
|
*/ |
401 |
|
public void testAcquireInterruptibly1() throws InterruptedException { |
402 |
< |
final Mutex sync = new Mutex(); |
403 |
< |
sync.acquire(1); |
404 |
< |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
402 |
> |
final Mutex sync = new Mutex(); |
403 |
> |
sync.acquire(1); |
404 |
> |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
405 |
|
|
406 |
|
t.start(); |
407 |
|
Thread.sleep(SHORT_DELAY_MS); |
415 |
|
* acquireInterruptibly succeeds when released, else is interruptible |
416 |
|
*/ |
417 |
|
public void testAcquireInterruptibly2() throws InterruptedException { |
418 |
< |
final Mutex sync = new Mutex(); |
418 |
> |
final Mutex sync = new Mutex(); |
419 |
|
sync.acquireInterruptibly(1); |
420 |
< |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
420 |
> |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
421 |
|
t.start(); |
422 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
423 |
|
t.interrupt(); |
424 |
|
assertTrue(sync.isHeldExclusively()); |
425 |
|
t.join(); |
429 |
|
* owns is true for a condition created by sync else false |
430 |
|
*/ |
431 |
|
public void testOwns() { |
432 |
< |
final Mutex sync = new Mutex(); |
432 |
> |
final Mutex sync = new Mutex(); |
433 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
434 |
|
final Mutex sync2 = new Mutex(); |
435 |
|
assertTrue(sync.owns(c)); |
440 |
|
* Calling await without holding sync throws IllegalMonitorStateException |
441 |
|
*/ |
442 |
|
public void testAwait_IllegalMonitor() throws InterruptedException { |
443 |
< |
final Mutex sync = new Mutex(); |
443 |
> |
final Mutex sync = new Mutex(); |
444 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
445 |
|
try { |
446 |
|
c.await(); |
447 |
|
shouldThrow(); |
448 |
< |
} |
464 |
< |
catch (IllegalMonitorStateException success) {} |
448 |
> |
} catch (IllegalMonitorStateException success) {} |
449 |
|
} |
450 |
|
|
451 |
|
/** |
452 |
|
* Calling signal without holding sync throws IllegalMonitorStateException |
453 |
|
*/ |
454 |
|
public void testSignal_IllegalMonitor() { |
455 |
< |
final Mutex sync = new Mutex(); |
455 |
> |
final Mutex sync = new Mutex(); |
456 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
457 |
|
try { |
458 |
|
c.signal(); |
459 |
|
shouldThrow(); |
460 |
< |
} |
477 |
< |
catch (IllegalMonitorStateException success) {} |
460 |
> |
} catch (IllegalMonitorStateException success) {} |
461 |
|
} |
462 |
|
|
463 |
|
/** |
464 |
|
* awaitNanos without a signal times out |
465 |
|
*/ |
466 |
|
public void testAwaitNanos_Timeout() throws InterruptedException { |
467 |
< |
final Mutex sync = new Mutex(); |
467 |
> |
final Mutex sync = new Mutex(); |
468 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
469 |
|
sync.acquire(1); |
470 |
|
long t = c.awaitNanos(100); |
476 |
|
* Timed await without a signal times out |
477 |
|
*/ |
478 |
|
public void testAwait_Timeout() throws InterruptedException { |
479 |
< |
final Mutex sync = new Mutex(); |
479 |
> |
final Mutex sync = new Mutex(); |
480 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
481 |
|
sync.acquire(1); |
482 |
< |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
482 |
> |
assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS)); |
483 |
|
sync.release(1); |
484 |
|
} |
485 |
|
|
487 |
|
* awaitUntil without a signal times out |
488 |
|
*/ |
489 |
|
public void testAwaitUntil_Timeout() throws InterruptedException { |
490 |
< |
final Mutex sync = new Mutex(); |
490 |
> |
final Mutex sync = new Mutex(); |
491 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
492 |
|
sync.acquire(1); |
493 |
|
java.util.Date d = new java.util.Date(); |
499 |
|
* await returns when signalled |
500 |
|
*/ |
501 |
|
public void testAwait() throws InterruptedException { |
502 |
< |
final Mutex sync = new Mutex(); |
502 |
> |
final Mutex sync = new Mutex(); |
503 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
504 |
< |
Thread t = new Thread(new Runnable() { |
505 |
< |
public void run() { |
506 |
< |
try { |
507 |
< |
sync.acquire(1); |
508 |
< |
c.await(); |
509 |
< |
sync.release(1); |
527 |
< |
} |
528 |
< |
catch (InterruptedException e) { |
529 |
< |
threadUnexpectedException(); |
530 |
< |
} |
531 |
< |
} |
532 |
< |
}); |
504 |
> |
Thread t = new Thread(new CheckedRunnable() { |
505 |
> |
public void realRun() throws InterruptedException { |
506 |
> |
sync.acquire(1); |
507 |
> |
c.await(); |
508 |
> |
sync.release(1); |
509 |
> |
}}); |
510 |
|
|
511 |
|
t.start(); |
512 |
|
Thread.sleep(SHORT_DELAY_MS); |
523 |
|
* hasWaiters throws NPE if null |
524 |
|
*/ |
525 |
|
public void testHasWaitersNPE() { |
526 |
< |
final Mutex sync = new Mutex(); |
526 |
> |
final Mutex sync = new Mutex(); |
527 |
|
try { |
528 |
|
sync.hasWaiters(null); |
529 |
|
shouldThrow(); |
534 |
|
* getWaitQueueLength throws NPE if null |
535 |
|
*/ |
536 |
|
public void testGetWaitQueueLengthNPE() { |
537 |
< |
final Mutex sync = new Mutex(); |
537 |
> |
final Mutex sync = new Mutex(); |
538 |
|
try { |
539 |
|
sync.getWaitQueueLength(null); |
540 |
|
shouldThrow(); |
546 |
|
* getWaitingThreads throws NPE if null |
547 |
|
*/ |
548 |
|
public void testGetWaitingThreadsNPE() { |
549 |
< |
final Mutex sync = new Mutex(); |
549 |
> |
final Mutex sync = new Mutex(); |
550 |
|
try { |
551 |
|
sync.getWaitingThreads(null); |
552 |
|
shouldThrow(); |
558 |
|
* hasWaiters throws IAE if not owned |
559 |
|
*/ |
560 |
|
public void testHasWaitersIAE() { |
561 |
< |
final Mutex sync = new Mutex(); |
561 |
> |
final Mutex sync = new Mutex(); |
562 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
563 |
< |
final Mutex sync2 = new Mutex(); |
563 |
> |
final Mutex sync2 = new Mutex(); |
564 |
|
try { |
565 |
|
sync2.hasWaiters(c); |
566 |
|
shouldThrow(); |
571 |
|
* hasWaiters throws IMSE if not synced |
572 |
|
*/ |
573 |
|
public void testHasWaitersIMSE() { |
574 |
< |
final Mutex sync = new Mutex(); |
574 |
> |
final Mutex sync = new Mutex(); |
575 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
576 |
|
try { |
577 |
|
sync.hasWaiters(c); |
584 |
|
* getWaitQueueLength throws IAE if not owned |
585 |
|
*/ |
586 |
|
public void testGetWaitQueueLengthIAE() { |
587 |
< |
final Mutex sync = new Mutex(); |
587 |
> |
final Mutex sync = new Mutex(); |
588 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
589 |
< |
final Mutex sync2 = new Mutex(); |
589 |
> |
final Mutex sync2 = new Mutex(); |
590 |
|
try { |
591 |
|
sync2.getWaitQueueLength(c); |
592 |
|
shouldThrow(); |
597 |
|
* getWaitQueueLength throws IMSE if not synced |
598 |
|
*/ |
599 |
|
public void testGetWaitQueueLengthIMSE() { |
600 |
< |
final Mutex sync = new Mutex(); |
600 |
> |
final Mutex sync = new Mutex(); |
601 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
602 |
|
try { |
603 |
|
sync.getWaitQueueLength(c); |
610 |
|
* getWaitingThreads throws IAE if not owned |
611 |
|
*/ |
612 |
|
public void testGetWaitingThreadsIAE() { |
613 |
< |
final Mutex sync = new Mutex(); |
613 |
> |
final Mutex sync = new Mutex(); |
614 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
615 |
< |
final Mutex sync2 = new Mutex(); |
615 |
> |
final Mutex sync2 = new Mutex(); |
616 |
|
try { |
617 |
|
sync2.getWaitingThreads(c); |
618 |
|
shouldThrow(); |
623 |
|
* getWaitingThreads throws IMSE if not synced |
624 |
|
*/ |
625 |
|
public void testGetWaitingThreadsIMSE() { |
626 |
< |
final Mutex sync = new Mutex(); |
626 |
> |
final Mutex sync = new Mutex(); |
627 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
628 |
|
try { |
629 |
|
sync.getWaitingThreads(c); |
637 |
|
* hasWaiters returns true when a thread is waiting, else false |
638 |
|
*/ |
639 |
|
public void testHasWaiters() throws InterruptedException { |
640 |
< |
final Mutex sync = new Mutex(); |
640 |
> |
final Mutex sync = new Mutex(); |
641 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
642 |
< |
Thread t = new Thread(new Runnable() { |
643 |
< |
public void run() { |
644 |
< |
try { |
645 |
< |
sync.acquire(1); |
646 |
< |
threadAssertFalse(sync.hasWaiters(c)); |
647 |
< |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
648 |
< |
c.await(); |
649 |
< |
sync.release(1); |
673 |
< |
} |
674 |
< |
catch (InterruptedException e) { |
675 |
< |
threadUnexpectedException(); |
676 |
< |
} |
677 |
< |
} |
678 |
< |
}); |
642 |
> |
Thread t = new Thread(new CheckedRunnable() { |
643 |
> |
public void realRun() throws InterruptedException { |
644 |
> |
sync.acquire(1); |
645 |
> |
threadAssertFalse(sync.hasWaiters(c)); |
646 |
> |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
647 |
> |
c.await(); |
648 |
> |
sync.release(1); |
649 |
> |
}}); |
650 |
|
|
651 |
|
t.start(); |
652 |
|
Thread.sleep(SHORT_DELAY_MS); |
668 |
|
* getWaitQueueLength returns number of waiting threads |
669 |
|
*/ |
670 |
|
public void testGetWaitQueueLength() throws InterruptedException { |
671 |
< |
final Mutex sync = new Mutex(); |
671 |
> |
final Mutex sync = new Mutex(); |
672 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
673 |
< |
Thread t1 = new Thread(new Runnable() { |
674 |
< |
public void run() { |
675 |
< |
try { |
676 |
< |
sync.acquire(1); |
677 |
< |
threadAssertFalse(sync.hasWaiters(c)); |
678 |
< |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
679 |
< |
c.await(); |
680 |
< |
sync.release(1); |
681 |
< |
} |
682 |
< |
catch (InterruptedException e) { |
683 |
< |
threadUnexpectedException(); |
684 |
< |
} |
685 |
< |
} |
686 |
< |
}); |
687 |
< |
|
688 |
< |
Thread t2 = new Thread(new Runnable() { |
689 |
< |
public void run() { |
719 |
< |
try { |
720 |
< |
sync.acquire(1); |
721 |
< |
threadAssertTrue(sync.hasWaiters(c)); |
722 |
< |
threadAssertEquals(1, sync.getWaitQueueLength(c)); |
723 |
< |
c.await(); |
724 |
< |
sync.release(1); |
725 |
< |
} |
726 |
< |
catch (InterruptedException e) { |
727 |
< |
threadUnexpectedException(); |
728 |
< |
} |
729 |
< |
} |
730 |
< |
}); |
673 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
674 |
> |
public void realRun() throws InterruptedException { |
675 |
> |
sync.acquire(1); |
676 |
> |
threadAssertFalse(sync.hasWaiters(c)); |
677 |
> |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
678 |
> |
c.await(); |
679 |
> |
sync.release(1); |
680 |
> |
}}); |
681 |
> |
|
682 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
683 |
> |
public void realRun() throws InterruptedException { |
684 |
> |
sync.acquire(1); |
685 |
> |
threadAssertTrue(sync.hasWaiters(c)); |
686 |
> |
threadAssertEquals(1, sync.getWaitQueueLength(c)); |
687 |
> |
c.await(); |
688 |
> |
sync.release(1); |
689 |
> |
}}); |
690 |
|
|
691 |
|
t1.start(); |
692 |
|
Thread.sleep(SHORT_DELAY_MS); |
712 |
|
* getWaitingThreads returns only and all waiting threads |
713 |
|
*/ |
714 |
|
public void testGetWaitingThreads() throws InterruptedException { |
715 |
< |
final Mutex sync = new Mutex(); |
715 |
> |
final Mutex sync = new Mutex(); |
716 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
717 |
< |
Thread t1 = new Thread(new Runnable() { |
718 |
< |
public void run() { |
719 |
< |
try { |
720 |
< |
sync.acquire(1); |
721 |
< |
threadAssertTrue(sync.getWaitingThreads(c).isEmpty()); |
722 |
< |
c.await(); |
723 |
< |
sync.release(1); |
724 |
< |
} |
725 |
< |
catch (InterruptedException e) { |
726 |
< |
threadUnexpectedException(); |
727 |
< |
} |
728 |
< |
} |
729 |
< |
}); |
730 |
< |
|
731 |
< |
Thread t2 = new Thread(new Runnable() { |
773 |
< |
public void run() { |
774 |
< |
try { |
775 |
< |
sync.acquire(1); |
776 |
< |
threadAssertFalse(sync.getWaitingThreads(c).isEmpty()); |
777 |
< |
c.await(); |
778 |
< |
sync.release(1); |
779 |
< |
} |
780 |
< |
catch (InterruptedException e) { |
781 |
< |
threadUnexpectedException(); |
782 |
< |
} |
783 |
< |
} |
784 |
< |
}); |
717 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
718 |
> |
public void realRun() throws InterruptedException { |
719 |
> |
sync.acquire(1); |
720 |
> |
threadAssertTrue(sync.getWaitingThreads(c).isEmpty()); |
721 |
> |
c.await(); |
722 |
> |
sync.release(1); |
723 |
> |
}}); |
724 |
> |
|
725 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
726 |
> |
public void realRun() throws InterruptedException { |
727 |
> |
sync.acquire(1); |
728 |
> |
threadAssertFalse(sync.getWaitingThreads(c).isEmpty()); |
729 |
> |
c.await(); |
730 |
> |
sync.release(1); |
731 |
> |
}}); |
732 |
|
|
733 |
|
sync.acquire(1); |
734 |
|
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
760 |
|
* awaitUninterruptibly doesn't abort on interrupt |
761 |
|
*/ |
762 |
|
public void testAwaitUninterruptibly() throws InterruptedException { |
763 |
< |
final Mutex sync = new Mutex(); |
763 |
> |
final Mutex sync = new Mutex(); |
764 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
765 |
< |
Thread t = new Thread(new Runnable() { |
766 |
< |
public void run() { |
767 |
< |
sync.acquire(1); |
768 |
< |
c.awaitUninterruptibly(); |
769 |
< |
sync.release(1); |
770 |
< |
} |
824 |
< |
}); |
765 |
> |
Thread t = new Thread(new CheckedRunnable() { |
766 |
> |
public void realRun() { |
767 |
> |
sync.acquire(1); |
768 |
> |
c.awaitUninterruptibly(); |
769 |
> |
sync.release(1); |
770 |
> |
}}); |
771 |
|
|
772 |
|
t.start(); |
773 |
|
Thread.sleep(SHORT_DELAY_MS); |
783 |
|
* await is interruptible |
784 |
|
*/ |
785 |
|
public void testAwait_Interrupt() throws InterruptedException { |
786 |
< |
final Mutex sync = new Mutex(); |
786 |
> |
final Mutex sync = new Mutex(); |
787 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
788 |
< |
Thread t = new Thread(new Runnable() { |
789 |
< |
public void run() { |
790 |
< |
try { |
791 |
< |
sync.acquire(1); |
792 |
< |
c.await(); |
847 |
< |
sync.release(1); |
848 |
< |
threadShouldThrow(); |
849 |
< |
} |
850 |
< |
catch (InterruptedException success) { |
851 |
< |
} |
852 |
< |
} |
853 |
< |
}); |
788 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
789 |
> |
public void realRun() throws InterruptedException { |
790 |
> |
sync.acquire(1); |
791 |
> |
c.await(); |
792 |
> |
}}); |
793 |
|
|
794 |
|
t.start(); |
795 |
|
Thread.sleep(SHORT_DELAY_MS); |
802 |
|
* awaitNanos is interruptible |
803 |
|
*/ |
804 |
|
public void testAwaitNanos_Interrupt() throws InterruptedException { |
805 |
< |
final Mutex sync = new Mutex(); |
805 |
> |
final Mutex sync = new Mutex(); |
806 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
807 |
< |
Thread t = new Thread(new Runnable() { |
808 |
< |
public void run() { |
809 |
< |
try { |
810 |
< |
sync.acquire(1); |
811 |
< |
c.awaitNanos(1000 * 1000 * 1000); // 1 sec |
873 |
< |
sync.release(1); |
874 |
< |
threadShouldThrow(); |
875 |
< |
} |
876 |
< |
catch (InterruptedException success) { |
877 |
< |
} |
878 |
< |
} |
879 |
< |
}); |
807 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
808 |
> |
public void realRun() throws InterruptedException { |
809 |
> |
sync.acquire(1); |
810 |
> |
c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); |
811 |
> |
}}); |
812 |
|
|
813 |
|
t.start(); |
814 |
|
Thread.sleep(SHORT_DELAY_MS); |
821 |
|
* awaitUntil is interruptible |
822 |
|
*/ |
823 |
|
public void testAwaitUntil_Interrupt() throws InterruptedException { |
824 |
< |
final Mutex sync = new Mutex(); |
824 |
> |
final Mutex sync = new Mutex(); |
825 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
826 |
< |
Thread t = new Thread(new Runnable() { |
827 |
< |
public void run() { |
828 |
< |
try { |
829 |
< |
sync.acquire(1); |
830 |
< |
java.util.Date d = new java.util.Date(); |
831 |
< |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
900 |
< |
sync.release(1); |
901 |
< |
threadShouldThrow(); |
902 |
< |
} |
903 |
< |
catch (InterruptedException success) { |
904 |
< |
} |
905 |
< |
} |
906 |
< |
}); |
826 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
827 |
> |
public void realRun() throws InterruptedException { |
828 |
> |
sync.acquire(1); |
829 |
> |
java.util.Date d = new java.util.Date(); |
830 |
> |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
831 |
> |
}}); |
832 |
|
|
833 |
|
t.start(); |
834 |
|
Thread.sleep(SHORT_DELAY_MS); |
841 |
|
* signalAll wakes up all threads |
842 |
|
*/ |
843 |
|
public void testSignalAll() throws InterruptedException { |
844 |
< |
final Mutex sync = new Mutex(); |
844 |
> |
final Mutex sync = new Mutex(); |
845 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
846 |
< |
Thread t1 = new Thread(new Runnable() { |
847 |
< |
public void run() { |
848 |
< |
try { |
849 |
< |
sync.acquire(1); |
850 |
< |
c.await(); |
851 |
< |
sync.release(1); |
852 |
< |
} |
853 |
< |
catch (InterruptedException e) { |
854 |
< |
threadUnexpectedException(); |
855 |
< |
} |
856 |
< |
} |
857 |
< |
}); |
858 |
< |
|
934 |
< |
Thread t2 = new Thread(new Runnable() { |
935 |
< |
public void run() { |
936 |
< |
try { |
937 |
< |
sync.acquire(1); |
938 |
< |
c.await(); |
939 |
< |
sync.release(1); |
940 |
< |
} |
941 |
< |
catch (InterruptedException e) { |
942 |
< |
threadUnexpectedException(); |
943 |
< |
} |
944 |
< |
} |
945 |
< |
}); |
846 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
847 |
> |
public void realRun() throws InterruptedException { |
848 |
> |
sync.acquire(1); |
849 |
> |
c.await(); |
850 |
> |
sync.release(1); |
851 |
> |
}}); |
852 |
> |
|
853 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
854 |
> |
public void realRun() throws InterruptedException { |
855 |
> |
sync.acquire(1); |
856 |
> |
c.await(); |
857 |
> |
sync.release(1); |
858 |
> |
}}); |
859 |
|
|
860 |
|
t1.start(); |
861 |
|
t2.start(); |
906 |
|
* tryReleaseShared setting state changes getState |
907 |
|
*/ |
908 |
|
public void testGetStateWithReleaseShared() { |
909 |
< |
final BooleanLatch l = new BooleanLatch(); |
910 |
< |
assertFalse(l.isSignalled()); |
911 |
< |
l.releaseShared(0); |
912 |
< |
assertTrue(l.isSignalled()); |
909 |
> |
final BooleanLatch l = new BooleanLatch(); |
910 |
> |
assertFalse(l.isSignalled()); |
911 |
> |
l.releaseShared(0); |
912 |
> |
assertTrue(l.isSignalled()); |
913 |
|
} |
914 |
|
|
915 |
|
/** |
916 |
|
* releaseShared has no effect when already signalled |
917 |
|
*/ |
918 |
|
public void testReleaseShared() { |
919 |
< |
final BooleanLatch l = new BooleanLatch(); |
920 |
< |
assertFalse(l.isSignalled()); |
921 |
< |
l.releaseShared(0); |
922 |
< |
assertTrue(l.isSignalled()); |
923 |
< |
l.releaseShared(0); |
924 |
< |
assertTrue(l.isSignalled()); |
919 |
> |
final BooleanLatch l = new BooleanLatch(); |
920 |
> |
assertFalse(l.isSignalled()); |
921 |
> |
l.releaseShared(0); |
922 |
> |
assertTrue(l.isSignalled()); |
923 |
> |
l.releaseShared(0); |
924 |
> |
assertTrue(l.isSignalled()); |
925 |
|
} |
926 |
|
|
927 |
|
/** |
928 |
|
* acquireSharedInterruptibly returns after release, but not before |
929 |
|
*/ |
930 |
|
public void testAcquireSharedInterruptibly() throws InterruptedException { |
931 |
< |
final BooleanLatch l = new BooleanLatch(); |
931 |
> |
final BooleanLatch l = new BooleanLatch(); |
932 |
|
|
933 |
< |
Thread t = new Thread(new Runnable() { |
934 |
< |
public void run() { |
935 |
< |
try { |
936 |
< |
threadAssertFalse(l.isSignalled()); |
937 |
< |
l.acquireSharedInterruptibly(0); |
938 |
< |
threadAssertTrue(l.isSignalled()); |
1026 |
< |
} catch (InterruptedException e) { |
1027 |
< |
threadUnexpectedException(); |
1028 |
< |
} |
1029 |
< |
} |
1030 |
< |
}); |
933 |
> |
Thread t = new Thread(new CheckedRunnable() { |
934 |
> |
public void realRun() throws InterruptedException { |
935 |
> |
threadAssertFalse(l.isSignalled()); |
936 |
> |
l.acquireSharedInterruptibly(0); |
937 |
> |
threadAssertTrue(l.isSignalled()); |
938 |
> |
}}); |
939 |
|
|
940 |
|
t.start(); |
941 |
|
assertFalse(l.isSignalled()); |
950 |
|
* acquireSharedTimed returns after release |
951 |
|
*/ |
952 |
|
public void testAsquireSharedTimed() throws InterruptedException { |
953 |
< |
final BooleanLatch l = new BooleanLatch(); |
953 |
> |
final BooleanLatch l = new BooleanLatch(); |
954 |
|
|
955 |
< |
Thread t = new Thread(new Runnable() { |
956 |
< |
public void run() { |
957 |
< |
try { |
958 |
< |
threadAssertFalse(l.isSignalled()); |
959 |
< |
threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); |
960 |
< |
threadAssertTrue(l.isSignalled()); |
961 |
< |
|
1054 |
< |
} catch (InterruptedException e) { |
1055 |
< |
threadUnexpectedException(); |
1056 |
< |
} |
1057 |
< |
} |
1058 |
< |
}); |
955 |
> |
Thread t = new Thread(new CheckedRunnable() { |
956 |
> |
public void realRun() throws InterruptedException { |
957 |
> |
assertFalse(l.isSignalled()); |
958 |
> |
long nanos = MILLISECONDS.toNanos(MEDIUM_DELAY_MS); |
959 |
> |
assertTrue(l.tryAcquireSharedNanos(0, nanos)); |
960 |
> |
assertTrue(l.isSignalled()); |
961 |
> |
}}); |
962 |
|
|
963 |
|
t.start(); |
964 |
|
assertFalse(l.isSignalled()); |
973 |
|
*/ |
974 |
|
public void testAcquireSharedInterruptibly_InterruptedException() throws InterruptedException { |
975 |
|
final BooleanLatch l = new BooleanLatch(); |
976 |
< |
Thread t = new Thread(new Runnable() { |
977 |
< |
public void run() { |
978 |
< |
try { |
979 |
< |
threadAssertFalse(l.isSignalled()); |
980 |
< |
l.acquireSharedInterruptibly(0); |
981 |
< |
threadShouldThrow(); |
982 |
< |
} catch (InterruptedException success) {} |
1080 |
< |
} |
1081 |
< |
}); |
1082 |
< |
t.start(); |
976 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
977 |
> |
public void realRun() throws InterruptedException { |
978 |
> |
threadAssertFalse(l.isSignalled()); |
979 |
> |
l.acquireSharedInterruptibly(0); |
980 |
> |
}}); |
981 |
> |
|
982 |
> |
t.start(); |
983 |
|
assertFalse(l.isSignalled()); |
984 |
|
t.interrupt(); |
985 |
|
t.join(); |
990 |
|
*/ |
991 |
|
public void testAcquireSharedNanos_InterruptedException() throws InterruptedException { |
992 |
|
final BooleanLatch l = new BooleanLatch(); |
993 |
< |
Thread t = new Thread(new Runnable() { |
994 |
< |
public void run() { |
995 |
< |
try { |
996 |
< |
threadAssertFalse(l.isSignalled()); |
997 |
< |
l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); |
998 |
< |
threadShouldThrow(); |
999 |
< |
} catch (InterruptedException success) {} |
1100 |
< |
} |
1101 |
< |
}); |
993 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
994 |
> |
public void realRun() throws InterruptedException { |
995 |
> |
assertFalse(l.isSignalled()); |
996 |
> |
long nanos = MILLISECONDS.toNanos(SMALL_DELAY_MS); |
997 |
> |
l.tryAcquireSharedNanos(0, nanos); |
998 |
> |
}}); |
999 |
> |
|
1000 |
|
t.start(); |
1001 |
|
Thread.sleep(SHORT_DELAY_MS); |
1002 |
|
assertFalse(l.isSignalled()); |
1009 |
|
*/ |
1010 |
|
public void testAcquireSharedNanos_Timeout() throws InterruptedException { |
1011 |
|
final BooleanLatch l = new BooleanLatch(); |
1012 |
< |
Thread t = new Thread(new Runnable() { |
1013 |
< |
public void run() { |
1014 |
< |
try { |
1015 |
< |
threadAssertFalse(l.isSignalled()); |
1016 |
< |
threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); |
1017 |
< |
} catch (InterruptedException ie) { |
1018 |
< |
threadUnexpectedException(); |
1121 |
< |
} |
1122 |
< |
} |
1123 |
< |
}); |
1012 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1013 |
> |
public void realRun() throws InterruptedException { |
1014 |
> |
assertFalse(l.isSignalled()); |
1015 |
> |
long nanos = MILLISECONDS.toNanos(SMALL_DELAY_MS); |
1016 |
> |
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1017 |
> |
}}); |
1018 |
> |
|
1019 |
|
t.start(); |
1020 |
|
Thread.sleep(SHORT_DELAY_MS); |
1021 |
|
assertFalse(l.isSignalled()); |
1022 |
|
t.join(); |
1023 |
|
} |
1024 |
|
|
1130 |
– |
|
1025 |
|
} |