68 |
|
} |
69 |
|
|
70 |
|
/** |
71 |
< |
* A runnable calling acquireInterruptibly |
71 |
> |
* A runnable calling acquireInterruptibly that does not expect to |
72 |
> |
* be interrupted. |
73 |
|
*/ |
74 |
< |
class InterruptibleSyncRunnable implements Runnable { |
74 |
> |
class InterruptibleSyncRunnable extends CheckedRunnable { |
75 |
|
final Mutex sync; |
76 |
|
InterruptibleSyncRunnable(Mutex l) { sync = l; } |
77 |
< |
public void run() { |
78 |
< |
try { |
78 |
< |
sync.acquireInterruptibly(1); |
79 |
< |
} catch (InterruptedException success) {} |
77 |
> |
public void realRun() throws InterruptedException { |
78 |
> |
sync.acquireInterruptibly(1); |
79 |
|
} |
80 |
|
} |
81 |
|
|
82 |
|
|
83 |
|
/** |
84 |
|
* A runnable calling acquireInterruptibly that expects to be |
85 |
< |
* interrupted |
85 |
> |
* interrupted. |
86 |
|
*/ |
87 |
< |
class InterruptedSyncRunnable implements Runnable { |
87 |
> |
class InterruptedSyncRunnable extends CheckedInterruptedRunnable { |
88 |
|
final Mutex sync; |
89 |
|
InterruptedSyncRunnable(Mutex l) { sync = l; } |
90 |
< |
public void run() { |
91 |
< |
try { |
93 |
< |
sync.acquireInterruptibly(1); |
94 |
< |
threadShouldThrow(); |
95 |
< |
} catch (InterruptedException success) {} |
90 |
> |
public void realRun() throws InterruptedException { |
91 |
> |
sync.acquireInterruptibly(1); |
92 |
|
} |
93 |
|
} |
94 |
|
|
96 |
|
* isHeldExclusively is false upon construction |
97 |
|
*/ |
98 |
|
public void testIsHeldExclusively() { |
99 |
< |
Mutex rl = new Mutex(); |
99 |
> |
Mutex rl = new Mutex(); |
100 |
|
assertFalse(rl.isHeldExclusively()); |
101 |
|
} |
102 |
|
|
104 |
|
* acquiring released sync succeeds |
105 |
|
*/ |
106 |
|
public void testAcquire() { |
107 |
< |
Mutex rl = new Mutex(); |
107 |
> |
Mutex rl = new Mutex(); |
108 |
|
rl.acquire(1); |
109 |
|
assertTrue(rl.isHeldExclusively()); |
110 |
|
rl.release(1); |
115 |
|
* tryAcquire on an released sync succeeds |
116 |
|
*/ |
117 |
|
public void testTryAcquire() { |
118 |
< |
Mutex rl = new Mutex(); |
118 |
> |
Mutex rl = new Mutex(); |
119 |
|
assertTrue(rl.tryAcquire(1)); |
120 |
|
assertTrue(rl.isHeldExclusively()); |
121 |
|
rl.release(1); |
125 |
|
* hasQueuedThreads reports whether there are waiting threads |
126 |
|
*/ |
127 |
|
public void testhasQueuedThreads() throws InterruptedException { |
128 |
< |
final Mutex sync = new Mutex(); |
128 |
> |
final Mutex sync = new Mutex(); |
129 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
130 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
131 |
|
assertFalse(sync.hasQueuedThreads()); |
150 |
|
* isQueued(null) throws NPE |
151 |
|
*/ |
152 |
|
public void testIsQueuedNPE() { |
153 |
< |
final Mutex sync = new Mutex(); |
153 |
> |
final Mutex sync = new Mutex(); |
154 |
|
try { |
155 |
|
sync.isQueued(null); |
156 |
|
shouldThrow(); |
161 |
|
* isQueued reports whether a thread is queued. |
162 |
|
*/ |
163 |
|
public void testIsQueued() throws InterruptedException { |
164 |
< |
final Mutex sync = new Mutex(); |
164 |
> |
final Mutex sync = new Mutex(); |
165 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
166 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
167 |
|
assertFalse(sync.isQueued(t1)); |
191 |
|
* getFirstQueuedThread returns first waiting thread or null if none |
192 |
|
*/ |
193 |
|
public void testGetFirstQueuedThread() throws InterruptedException { |
194 |
< |
final Mutex sync = new Mutex(); |
194 |
> |
final Mutex sync = new Mutex(); |
195 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
196 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
197 |
|
assertNull(sync.getFirstQueuedThread()); |
218 |
|
* hasContended reports false if no thread has ever blocked, else true |
219 |
|
*/ |
220 |
|
public void testHasContended() throws InterruptedException { |
221 |
< |
final Mutex sync = new Mutex(); |
221 |
> |
final Mutex sync = new Mutex(); |
222 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
223 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
224 |
|
assertFalse(sync.hasContended()); |
243 |
|
* getQueuedThreads includes waiting threads |
244 |
|
*/ |
245 |
|
public void testGetQueuedThreads() throws InterruptedException { |
246 |
< |
final Mutex sync = new Mutex(); |
246 |
> |
final Mutex sync = new Mutex(); |
247 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
248 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
249 |
|
assertTrue(sync.getQueuedThreads().isEmpty()); |
271 |
|
* getExclusiveQueuedThreads includes waiting threads |
272 |
|
*/ |
273 |
|
public void testGetExclusiveQueuedThreads() throws InterruptedException { |
274 |
< |
final Mutex sync = new Mutex(); |
274 |
> |
final Mutex sync = new Mutex(); |
275 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
276 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
277 |
|
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
299 |
|
* getSharedQueuedThreads does not include exclusively waiting threads |
300 |
|
*/ |
301 |
|
public void testGetSharedQueuedThreads() throws InterruptedException { |
302 |
< |
final Mutex sync = new Mutex(); |
302 |
> |
final Mutex sync = new Mutex(); |
303 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
304 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
305 |
|
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
325 |
|
* tryAcquireNanos is interruptible. |
326 |
|
*/ |
327 |
|
public void testInterruptedException2() throws InterruptedException { |
328 |
< |
final Mutex sync = new Mutex(); |
329 |
< |
sync.acquire(1); |
330 |
< |
Thread t = new Thread(new Runnable() { |
331 |
< |
public void run() { |
332 |
< |
try { |
333 |
< |
sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); |
338 |
< |
threadShouldThrow(); |
339 |
< |
} catch (InterruptedException success) {} |
340 |
< |
} |
341 |
< |
}); |
328 |
> |
final Mutex sync = new Mutex(); |
329 |
> |
sync.acquire(1); |
330 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
331 |
> |
public void realRun() throws InterruptedException { |
332 |
> |
sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); |
333 |
> |
}}); |
334 |
|
|
335 |
|
t.start(); |
336 |
|
t.interrupt(); |
342 |
|
* TryAcquire on exclusively held sync fails |
343 |
|
*/ |
344 |
|
public void testTryAcquireWhenSynced() throws InterruptedException { |
345 |
< |
final Mutex sync = new Mutex(); |
346 |
< |
sync.acquire(1); |
347 |
< |
Thread t = new Thread(new Runnable() { |
348 |
< |
public void run() { |
349 |
< |
threadAssertFalse(sync.tryAcquire(1)); |
350 |
< |
} |
359 |
< |
}); |
345 |
> |
final Mutex sync = new Mutex(); |
346 |
> |
sync.acquire(1); |
347 |
> |
Thread t = new Thread(new CheckedRunnable() { |
348 |
> |
public void realRun() { |
349 |
> |
threadAssertFalse(sync.tryAcquire(1)); |
350 |
> |
}}); |
351 |
|
|
352 |
|
t.start(); |
353 |
|
t.join(); |
358 |
|
* tryAcquireNanos on an exclusively held sync times out |
359 |
|
*/ |
360 |
|
public void testAcquireNanos_Timeout() throws InterruptedException { |
361 |
< |
final Mutex sync = new Mutex(); |
362 |
< |
sync.acquire(1); |
363 |
< |
Thread t = new Thread(new Runnable() { |
364 |
< |
public void run() { |
365 |
< |
try { |
366 |
< |
threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000)); |
376 |
< |
} catch (Exception ex) { |
377 |
< |
threadUnexpectedException(ex); |
378 |
< |
} |
379 |
< |
} |
380 |
< |
}); |
361 |
> |
final Mutex sync = new Mutex(); |
362 |
> |
sync.acquire(1); |
363 |
> |
Thread t = new Thread(new CheckedRunnable() { |
364 |
> |
public void realRun() throws InterruptedException { |
365 |
> |
threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000)); |
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 |
< |
} |
403 |
< |
catch (Exception e) { |
404 |
< |
threadUnexpectedException(e); |
405 |
< |
} |
406 |
< |
sync.release(1); |
407 |
< |
} |
408 |
< |
}); |
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 |
|
t.start(); |
406 |
|
Thread.sleep(SHORT_DELAY_MS); |
407 |
|
t.interrupt(); |
414 |
|
* acquireInterruptibly succeeds when released, else is interruptible |
415 |
|
*/ |
416 |
|
public void testAcquireInterruptibly2() throws InterruptedException { |
417 |
< |
final Mutex sync = new Mutex(); |
417 |
> |
final Mutex sync = new Mutex(); |
418 |
|
sync.acquireInterruptibly(1); |
419 |
< |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
419 |
> |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
420 |
|
t.start(); |
421 |
|
t.interrupt(); |
422 |
|
assertTrue(sync.isHeldExclusively()); |
427 |
|
* owns is true for a condition created by sync else false |
428 |
|
*/ |
429 |
|
public void testOwns() { |
430 |
< |
final Mutex sync = new Mutex(); |
430 |
> |
final Mutex sync = new Mutex(); |
431 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
432 |
|
final Mutex sync2 = new Mutex(); |
433 |
|
assertTrue(sync.owns(c)); |
438 |
|
* Calling await without holding sync throws IllegalMonitorStateException |
439 |
|
*/ |
440 |
|
public void testAwait_IllegalMonitor() throws InterruptedException { |
441 |
< |
final Mutex sync = new Mutex(); |
441 |
> |
final Mutex sync = new Mutex(); |
442 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
443 |
|
try { |
444 |
|
c.await(); |
445 |
|
shouldThrow(); |
446 |
< |
} |
467 |
< |
catch (IllegalMonitorStateException success) { |
468 |
< |
} |
446 |
> |
} catch (IllegalMonitorStateException success) {} |
447 |
|
} |
448 |
|
|
449 |
|
/** |
450 |
|
* Calling signal without holding sync throws IllegalMonitorStateException |
451 |
|
*/ |
452 |
|
public void testSignal_IllegalMonitor() throws InterruptedException { |
453 |
< |
final Mutex sync = new Mutex(); |
453 |
> |
final Mutex sync = new Mutex(); |
454 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
455 |
|
try { |
456 |
|
c.signal(); |
457 |
|
shouldThrow(); |
458 |
< |
} |
481 |
< |
catch (IllegalMonitorStateException success) {} |
458 |
> |
} catch (IllegalMonitorStateException success) {} |
459 |
|
} |
460 |
|
|
461 |
|
/** |
462 |
|
* awaitNanos without a signal times out |
463 |
|
*/ |
464 |
|
public void testAwaitNanos_Timeout() throws InterruptedException { |
465 |
< |
final Mutex sync = new Mutex(); |
465 |
> |
final Mutex sync = new Mutex(); |
466 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
467 |
|
sync.acquire(1); |
468 |
|
long t = c.awaitNanos(100); |
474 |
|
* Timed await without a signal times out |
475 |
|
*/ |
476 |
|
public void testAwait_Timeout() throws InterruptedException { |
477 |
< |
final Mutex sync = new Mutex(); |
477 |
> |
final Mutex sync = new Mutex(); |
478 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
479 |
|
sync.acquire(1); |
480 |
|
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
485 |
|
* awaitUntil without a signal times out |
486 |
|
*/ |
487 |
|
public void testAwaitUntil_Timeout() throws InterruptedException { |
488 |
< |
final Mutex sync = new Mutex(); |
488 |
> |
final Mutex sync = new Mutex(); |
489 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
490 |
|
sync.acquire(1); |
491 |
|
java.util.Date d = new java.util.Date(); |
497 |
|
* await returns when signalled |
498 |
|
*/ |
499 |
|
public void testAwait() throws InterruptedException { |
500 |
< |
final Mutex sync = new Mutex(); |
500 |
> |
final Mutex sync = new Mutex(); |
501 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
502 |
< |
Thread t = new Thread(new Runnable() { |
503 |
< |
public void run() { |
504 |
< |
try { |
505 |
< |
sync.acquire(1); |
506 |
< |
c.await(); |
507 |
< |
sync.release(1); |
531 |
< |
} |
532 |
< |
catch (InterruptedException e) { |
533 |
< |
threadUnexpectedException(e); |
534 |
< |
} |
535 |
< |
} |
536 |
< |
}); |
502 |
> |
Thread t = new Thread(new CheckedRunnable() { |
503 |
> |
public void realRun() throws InterruptedException { |
504 |
> |
sync.acquire(1); |
505 |
> |
c.await(); |
506 |
> |
sync.release(1); |
507 |
> |
}}); |
508 |
|
|
509 |
|
t.start(); |
510 |
|
Thread.sleep(SHORT_DELAY_MS); |
521 |
|
* hasWaiters throws NPE if null |
522 |
|
*/ |
523 |
|
public void testHasWaitersNPE() { |
524 |
< |
final Mutex sync = new Mutex(); |
524 |
> |
final Mutex sync = new Mutex(); |
525 |
|
try { |
526 |
|
sync.hasWaiters(null); |
527 |
|
shouldThrow(); |
532 |
|
* getWaitQueueLength throws NPE if null |
533 |
|
*/ |
534 |
|
public void testGetWaitQueueLengthNPE() { |
535 |
< |
final Mutex sync = new Mutex(); |
535 |
> |
final Mutex sync = new Mutex(); |
536 |
|
try { |
537 |
|
sync.getWaitQueueLength(null); |
538 |
|
shouldThrow(); |
544 |
|
* getWaitingThreads throws NPE if null |
545 |
|
*/ |
546 |
|
public void testGetWaitingThreadsNPE() { |
547 |
< |
final Mutex sync = new Mutex(); |
547 |
> |
final Mutex sync = new Mutex(); |
548 |
|
try { |
549 |
|
sync.getWaitingThreads(null); |
550 |
|
shouldThrow(); |
556 |
|
* hasWaiters throws IAE if not owned |
557 |
|
*/ |
558 |
|
public void testHasWaitersIAE() { |
559 |
< |
final Mutex sync = new Mutex(); |
559 |
> |
final Mutex sync = new Mutex(); |
560 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
561 |
< |
final Mutex sync2 = new Mutex(); |
561 |
> |
final Mutex sync2 = new Mutex(); |
562 |
|
try { |
563 |
|
sync2.hasWaiters(c); |
564 |
|
shouldThrow(); |
569 |
|
* hasWaiters throws IMSE if not synced |
570 |
|
*/ |
571 |
|
public void testHasWaitersIMSE() { |
572 |
< |
final Mutex sync = new Mutex(); |
572 |
> |
final Mutex sync = new Mutex(); |
573 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
574 |
|
try { |
575 |
|
sync.hasWaiters(c); |
582 |
|
* getWaitQueueLength throws IAE if not owned |
583 |
|
*/ |
584 |
|
public void testGetWaitQueueLengthIAE() { |
585 |
< |
final Mutex sync = new Mutex(); |
585 |
> |
final Mutex sync = new Mutex(); |
586 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
587 |
< |
final Mutex sync2 = new Mutex(); |
587 |
> |
final Mutex sync2 = new Mutex(); |
588 |
|
try { |
589 |
|
sync2.getWaitQueueLength(c); |
590 |
|
shouldThrow(); |
595 |
|
* getWaitQueueLength throws IMSE if not synced |
596 |
|
*/ |
597 |
|
public void testGetWaitQueueLengthIMSE() { |
598 |
< |
final Mutex sync = new Mutex(); |
598 |
> |
final Mutex sync = new Mutex(); |
599 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
600 |
|
try { |
601 |
|
sync.getWaitQueueLength(c); |
608 |
|
* getWaitingThreads throws IAE if not owned |
609 |
|
*/ |
610 |
|
public void testGetWaitingThreadsIAE() { |
611 |
< |
final Mutex sync = new Mutex(); |
611 |
> |
final Mutex sync = new Mutex(); |
612 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
613 |
< |
final Mutex sync2 = new Mutex(); |
613 |
> |
final Mutex sync2 = new Mutex(); |
614 |
|
try { |
615 |
|
sync2.getWaitingThreads(c); |
616 |
|
shouldThrow(); |
621 |
|
* getWaitingThreads throws IMSE if not synced |
622 |
|
*/ |
623 |
|
public void testGetWaitingThreadsIMSE() { |
624 |
< |
final Mutex sync = new Mutex(); |
624 |
> |
final Mutex sync = new Mutex(); |
625 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
626 |
|
try { |
627 |
|
sync.getWaitingThreads(c); |
635 |
|
* hasWaiters returns true when a thread is waiting, else false |
636 |
|
*/ |
637 |
|
public void testHasWaiters() throws InterruptedException { |
638 |
< |
final Mutex sync = new Mutex(); |
638 |
> |
final Mutex sync = new Mutex(); |
639 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
640 |
< |
Thread t = new Thread(new Runnable() { |
641 |
< |
public void run() { |
642 |
< |
try { |
643 |
< |
sync.acquire(1); |
644 |
< |
threadAssertFalse(sync.hasWaiters(c)); |
645 |
< |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
646 |
< |
c.await(); |
647 |
< |
sync.release(1); |
677 |
< |
} |
678 |
< |
catch (InterruptedException e) { |
679 |
< |
threadUnexpectedException(e); |
680 |
< |
} |
681 |
< |
} |
682 |
< |
}); |
640 |
> |
Thread t = new Thread(new CheckedRunnable() { |
641 |
> |
public void realRun() throws InterruptedException { |
642 |
> |
sync.acquire(1); |
643 |
> |
threadAssertFalse(sync.hasWaiters(c)); |
644 |
> |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
645 |
> |
c.await(); |
646 |
> |
sync.release(1); |
647 |
> |
}}); |
648 |
|
|
649 |
|
t.start(); |
650 |
|
Thread.sleep(SHORT_DELAY_MS); |
666 |
|
* getWaitQueueLength returns number of waiting threads |
667 |
|
*/ |
668 |
|
public void testGetWaitQueueLength() throws InterruptedException { |
669 |
< |
final Mutex sync = new Mutex(); |
669 |
> |
final Mutex sync = new Mutex(); |
670 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
671 |
< |
Thread t1 = new Thread(new Runnable() { |
672 |
< |
public void run() { |
673 |
< |
try { |
674 |
< |
sync.acquire(1); |
675 |
< |
threadAssertFalse(sync.hasWaiters(c)); |
676 |
< |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
677 |
< |
c.await(); |
678 |
< |
sync.release(1); |
679 |
< |
} |
680 |
< |
catch (InterruptedException e) { |
681 |
< |
threadUnexpectedException(e); |
682 |
< |
} |
683 |
< |
} |
684 |
< |
}); |
685 |
< |
|
686 |
< |
Thread t2 = new Thread(new Runnable() { |
687 |
< |
public void run() { |
723 |
< |
try { |
724 |
< |
sync.acquire(1); |
725 |
< |
threadAssertTrue(sync.hasWaiters(c)); |
726 |
< |
threadAssertEquals(1, sync.getWaitQueueLength(c)); |
727 |
< |
c.await(); |
728 |
< |
sync.release(1); |
729 |
< |
} |
730 |
< |
catch (InterruptedException e) { |
731 |
< |
threadUnexpectedException(e); |
732 |
< |
} |
733 |
< |
} |
734 |
< |
}); |
671 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
672 |
> |
public void realRun() throws InterruptedException { |
673 |
> |
sync.acquire(1); |
674 |
> |
threadAssertFalse(sync.hasWaiters(c)); |
675 |
> |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
676 |
> |
c.await(); |
677 |
> |
sync.release(1); |
678 |
> |
}}); |
679 |
> |
|
680 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
681 |
> |
public void realRun() throws InterruptedException { |
682 |
> |
sync.acquire(1); |
683 |
> |
threadAssertTrue(sync.hasWaiters(c)); |
684 |
> |
threadAssertEquals(1, sync.getWaitQueueLength(c)); |
685 |
> |
c.await(); |
686 |
> |
sync.release(1); |
687 |
> |
}}); |
688 |
|
|
689 |
|
t1.start(); |
690 |
|
Thread.sleep(SHORT_DELAY_MS); |
710 |
|
* getWaitingThreads returns only and all waiting threads |
711 |
|
*/ |
712 |
|
public void testGetWaitingThreads() throws InterruptedException { |
713 |
< |
final Mutex sync = new Mutex(); |
713 |
> |
final Mutex sync = new Mutex(); |
714 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
715 |
< |
Thread t1 = new Thread(new Runnable() { |
716 |
< |
public void run() { |
717 |
< |
try { |
718 |
< |
sync.acquire(1); |
719 |
< |
threadAssertTrue(sync.getWaitingThreads(c).isEmpty()); |
720 |
< |
c.await(); |
721 |
< |
sync.release(1); |
722 |
< |
} |
723 |
< |
catch (InterruptedException e) { |
724 |
< |
threadUnexpectedException(e); |
725 |
< |
} |
726 |
< |
} |
727 |
< |
}); |
728 |
< |
|
729 |
< |
Thread t2 = new Thread(new Runnable() { |
777 |
< |
public void run() { |
778 |
< |
try { |
779 |
< |
sync.acquire(1); |
780 |
< |
threadAssertFalse(sync.getWaitingThreads(c).isEmpty()); |
781 |
< |
c.await(); |
782 |
< |
sync.release(1); |
783 |
< |
} |
784 |
< |
catch (InterruptedException e) { |
785 |
< |
threadUnexpectedException(e); |
786 |
< |
} |
787 |
< |
} |
788 |
< |
}); |
715 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
716 |
> |
public void realRun() throws InterruptedException { |
717 |
> |
sync.acquire(1); |
718 |
> |
threadAssertTrue(sync.getWaitingThreads(c).isEmpty()); |
719 |
> |
c.await(); |
720 |
> |
sync.release(1); |
721 |
> |
}}); |
722 |
> |
|
723 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
724 |
> |
public void realRun() throws InterruptedException { |
725 |
> |
sync.acquire(1); |
726 |
> |
threadAssertFalse(sync.getWaitingThreads(c).isEmpty()); |
727 |
> |
c.await(); |
728 |
> |
sync.release(1); |
729 |
> |
}}); |
730 |
|
|
731 |
|
sync.acquire(1); |
732 |
|
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
758 |
|
* awaitUninterruptibly doesn't abort on interrupt |
759 |
|
*/ |
760 |
|
public void testAwaitUninterruptibly() throws InterruptedException { |
761 |
< |
final Mutex sync = new Mutex(); |
761 |
> |
final Mutex sync = new Mutex(); |
762 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
763 |
< |
Thread t = new Thread(new Runnable() { |
764 |
< |
public void run() { |
765 |
< |
sync.acquire(1); |
766 |
< |
c.awaitUninterruptibly(); |
767 |
< |
sync.release(1); |
768 |
< |
} |
828 |
< |
}); |
763 |
> |
Thread t = new Thread(new CheckedRunnable() { |
764 |
> |
public void realRun() { |
765 |
> |
sync.acquire(1); |
766 |
> |
c.awaitUninterruptibly(); |
767 |
> |
sync.release(1); |
768 |
> |
}}); |
769 |
|
|
770 |
|
t.start(); |
771 |
|
Thread.sleep(SHORT_DELAY_MS); |
781 |
|
* await is interruptible |
782 |
|
*/ |
783 |
|
public void testAwait_Interrupt() throws InterruptedException { |
784 |
< |
final Mutex sync = new Mutex(); |
784 |
> |
final Mutex sync = new Mutex(); |
785 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
786 |
< |
Thread t = new Thread(new Runnable() { |
787 |
< |
public void run() { |
788 |
< |
try { |
789 |
< |
sync.acquire(1); |
790 |
< |
c.await(); |
791 |
< |
sync.release(1); |
852 |
< |
threadShouldThrow(); |
853 |
< |
} |
854 |
< |
catch (InterruptedException success) { |
855 |
< |
} |
856 |
< |
} |
857 |
< |
}); |
786 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
787 |
> |
public void realRun() throws InterruptedException { |
788 |
> |
sync.acquire(1); |
789 |
> |
c.await(); |
790 |
> |
sync.release(1); |
791 |
> |
}}); |
792 |
|
|
793 |
|
t.start(); |
794 |
|
Thread.sleep(SHORT_DELAY_MS); |
801 |
|
* awaitNanos is interruptible |
802 |
|
*/ |
803 |
|
public void testAwaitNanos_Interrupt() throws InterruptedException { |
804 |
< |
final Mutex sync = new Mutex(); |
804 |
> |
final Mutex sync = new Mutex(); |
805 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
806 |
< |
Thread t = new Thread(new Runnable() { |
807 |
< |
public void run() { |
808 |
< |
try { |
809 |
< |
sync.acquire(1); |
810 |
< |
c.awaitNanos(1000 * 1000 * 1000); // 1 sec |
811 |
< |
sync.release(1); |
878 |
< |
threadShouldThrow(); |
879 |
< |
} |
880 |
< |
catch (InterruptedException success) { |
881 |
< |
} |
882 |
< |
} |
883 |
< |
}); |
806 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
807 |
> |
public void realRun() throws InterruptedException { |
808 |
> |
sync.acquire(1); |
809 |
> |
c.awaitNanos(1000 * 1000 * 1000); // 1 sec |
810 |
> |
sync.release(1); |
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 AbstractQueuedLongSynchronizer.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)); |
832 |
< |
sync.release(1); |
905 |
< |
threadShouldThrow(); |
906 |
< |
} |
907 |
< |
catch (InterruptedException success) { |
908 |
< |
} |
909 |
< |
} |
910 |
< |
}); |
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 |
> |
sync.release(1); |
832 |
> |
}}); |
833 |
|
|
834 |
|
t.start(); |
835 |
|
Thread.sleep(SHORT_DELAY_MS); |
842 |
|
* signalAll wakes up all threads |
843 |
|
*/ |
844 |
|
public void testSignalAll() throws InterruptedException { |
845 |
< |
final Mutex sync = new Mutex(); |
845 |
> |
final Mutex sync = new Mutex(); |
846 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
847 |
< |
Thread t1 = new Thread(new Runnable() { |
848 |
< |
public void run() { |
849 |
< |
try { |
850 |
< |
sync.acquire(1); |
851 |
< |
c.await(); |
852 |
< |
sync.release(1); |
853 |
< |
} |
854 |
< |
catch (InterruptedException e) { |
855 |
< |
threadUnexpectedException(); |
856 |
< |
} |
857 |
< |
} |
858 |
< |
}); |
859 |
< |
|
938 |
< |
Thread t2 = new Thread(new Runnable() { |
939 |
< |
public void run() { |
940 |
< |
try { |
941 |
< |
sync.acquire(1); |
942 |
< |
c.await(); |
943 |
< |
sync.release(1); |
944 |
< |
} |
945 |
< |
catch (InterruptedException e) { |
946 |
< |
threadUnexpectedException(); |
947 |
< |
} |
948 |
< |
} |
949 |
< |
}); |
847 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
848 |
> |
public void realRun() throws InterruptedException { |
849 |
> |
sync.acquire(1); |
850 |
> |
c.await(); |
851 |
> |
sync.release(1); |
852 |
> |
}}); |
853 |
> |
|
854 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
855 |
> |
public void realRun() throws InterruptedException { |
856 |
> |
sync.acquire(1); |
857 |
> |
c.await(); |
858 |
> |
sync.release(1); |
859 |
> |
}}); |
860 |
|
|
861 |
|
t1.start(); |
862 |
|
t2.start(); |
907 |
|
* tryReleaseShared setting state changes getState |
908 |
|
*/ |
909 |
|
public void testGetStateWithReleaseShared() { |
910 |
< |
final BooleanLatch l = new BooleanLatch(); |
911 |
< |
assertFalse(l.isSignalled()); |
912 |
< |
l.releaseShared(0); |
913 |
< |
assertTrue(l.isSignalled()); |
910 |
> |
final BooleanLatch l = new BooleanLatch(); |
911 |
> |
assertFalse(l.isSignalled()); |
912 |
> |
l.releaseShared(0); |
913 |
> |
assertTrue(l.isSignalled()); |
914 |
|
} |
915 |
|
|
916 |
|
/** |
917 |
|
* releaseShared has no effect when already signalled |
918 |
|
*/ |
919 |
|
public void testReleaseShared() { |
920 |
< |
final BooleanLatch l = new BooleanLatch(); |
921 |
< |
assertFalse(l.isSignalled()); |
922 |
< |
l.releaseShared(0); |
923 |
< |
assertTrue(l.isSignalled()); |
924 |
< |
l.releaseShared(0); |
925 |
< |
assertTrue(l.isSignalled()); |
920 |
> |
final BooleanLatch l = new BooleanLatch(); |
921 |
> |
assertFalse(l.isSignalled()); |
922 |
> |
l.releaseShared(0); |
923 |
> |
assertTrue(l.isSignalled()); |
924 |
> |
l.releaseShared(0); |
925 |
> |
assertTrue(l.isSignalled()); |
926 |
|
} |
927 |
|
|
928 |
|
/** |
929 |
|
* acquireSharedInterruptibly returns after release, but not before |
930 |
|
*/ |
931 |
|
public void testAcquireSharedInterruptibly() throws InterruptedException { |
932 |
< |
final BooleanLatch l = new BooleanLatch(); |
932 |
> |
final BooleanLatch l = new BooleanLatch(); |
933 |
|
|
934 |
< |
Thread t = new Thread(new Runnable() { |
935 |
< |
public void run() { |
936 |
< |
try { |
937 |
< |
threadAssertFalse(l.isSignalled()); |
938 |
< |
l.acquireSharedInterruptibly(0); |
939 |
< |
threadAssertTrue(l.isSignalled()); |
1030 |
< |
} catch (InterruptedException e) { |
1031 |
< |
threadUnexpectedException(); |
1032 |
< |
} |
1033 |
< |
} |
1034 |
< |
}); |
934 |
> |
Thread t = new Thread(new CheckedRunnable() { |
935 |
> |
public void realRun() throws InterruptedException { |
936 |
> |
threadAssertFalse(l.isSignalled()); |
937 |
> |
l.acquireSharedInterruptibly(0); |
938 |
> |
threadAssertTrue(l.isSignalled()); |
939 |
> |
}}); |
940 |
|
|
941 |
|
t.start(); |
942 |
|
assertFalse(l.isSignalled()); |
951 |
|
* acquireSharedTimed returns after release |
952 |
|
*/ |
953 |
|
public void testAsquireSharedTimed() throws InterruptedException { |
954 |
< |
final BooleanLatch l = new BooleanLatch(); |
954 |
> |
final BooleanLatch l = new BooleanLatch(); |
955 |
|
|
956 |
< |
Thread t = new Thread(new Runnable() { |
957 |
< |
public void run() { |
958 |
< |
try { |
959 |
< |
threadAssertFalse(l.isSignalled()); |
960 |
< |
threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); |
961 |
< |
threadAssertTrue(l.isSignalled()); |
1057 |
< |
|
1058 |
< |
} catch (InterruptedException e) { |
1059 |
< |
threadUnexpectedException(); |
1060 |
< |
} |
1061 |
< |
} |
1062 |
< |
}); |
956 |
> |
Thread t = new Thread(new CheckedRunnable() { |
957 |
> |
public void realRun() throws InterruptedException { |
958 |
> |
threadAssertFalse(l.isSignalled()); |
959 |
> |
threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); |
960 |
> |
threadAssertTrue(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); |
1082 |
< |
threadShouldThrow(); |
1083 |
< |
} catch (InterruptedException success) {} |
1084 |
< |
} |
1085 |
< |
}); |
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(); |
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); |
1103 |
< |
threadShouldThrow(); |
1104 |
< |
} catch (InterruptedException success) {} |
1105 |
< |
} |
1106 |
< |
}); |
993 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
994 |
> |
public void realRun() throws InterruptedException { |
995 |
> |
threadAssertFalse(l.isSignalled()); |
996 |
> |
l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); |
997 |
> |
}}); |
998 |
|
|
999 |
|
t.start(); |
1000 |
|
Thread.sleep(SHORT_DELAY_MS); |
1008 |
|
*/ |
1009 |
|
public void testAcquireSharedNanos_Timeout() throws InterruptedException { |
1010 |
|
final BooleanLatch l = new BooleanLatch(); |
1011 |
< |
Thread t = new Thread(new Runnable() { |
1012 |
< |
public void run() { |
1013 |
< |
try { |
1014 |
< |
threadAssertFalse(l.isSignalled()); |
1015 |
< |
threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); |
1125 |
< |
} catch (InterruptedException ie) { |
1126 |
< |
threadUnexpectedException(); |
1127 |
< |
} |
1128 |
< |
} |
1129 |
< |
}); |
1011 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1012 |
> |
public void realRun() throws InterruptedException { |
1013 |
> |
threadAssertFalse(l.isSignalled()); |
1014 |
> |
threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); |
1015 |
> |
}}); |
1016 |
|
|
1017 |
|
t.start(); |
1018 |
|
Thread.sleep(SHORT_DELAY_MS); |
1020 |
|
t.join(); |
1021 |
|
} |
1022 |
|
|
1137 |
– |
|
1023 |
|
} |