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 |
|
|
17 |
|
public class AbstractQueuedLongSynchronizerTest extends JSR166TestCase { |
18 |
|
public static void main(String[] args) { |
19 |
< |
junit.textui.TestRunner.run (suite()); |
19 |
> |
junit.textui.TestRunner.run(suite()); |
20 |
|
} |
21 |
|
public static Test suite() { |
22 |
|
return new TestSuite(AbstractQueuedLongSynchronizerTest.class); |
47 |
|
return true; |
48 |
|
} |
49 |
|
|
50 |
< |
public AbstractQueuedLongSynchronizer.ConditionObject newCondition() { return new AbstractQueuedLongSynchronizer.ConditionObject(); } |
50 |
> |
public AbstractQueuedLongSynchronizer.ConditionObject newCondition() { |
51 |
> |
return new AbstractQueuedLongSynchronizer.ConditionObject(); |
52 |
> |
} |
53 |
|
|
54 |
|
} |
55 |
|
|
61 |
|
public boolean isSignalled() { return getState() != 0; } |
62 |
|
|
63 |
|
public long tryAcquireShared(long ignore) { |
64 |
< |
return isSignalled()? 1 : -1; |
64 |
> |
return isSignalled() ? 1 : -1; |
65 |
|
} |
66 |
|
|
67 |
|
public boolean tryReleaseShared(long ignore) { |
71 |
|
} |
72 |
|
|
73 |
|
/** |
74 |
< |
* A runnable calling acquireInterruptibly |
74 |
> |
* A runnable calling acquireInterruptibly that does not expect to |
75 |
> |
* be interrupted. |
76 |
|
*/ |
77 |
< |
class InterruptibleSyncRunnable implements Runnable { |
77 |
> |
class InterruptibleSyncRunnable extends CheckedRunnable { |
78 |
|
final Mutex sync; |
79 |
|
InterruptibleSyncRunnable(Mutex l) { sync = l; } |
80 |
< |
public void run() { |
81 |
< |
try { |
78 |
< |
sync.acquireInterruptibly(1); |
79 |
< |
} catch (InterruptedException success) {} |
80 |
> |
public void realRun() throws InterruptedException { |
81 |
> |
sync.acquireInterruptibly(1); |
82 |
|
} |
83 |
|
} |
84 |
|
|
85 |
|
|
86 |
|
/** |
87 |
|
* A runnable calling acquireInterruptibly that expects to be |
88 |
< |
* interrupted |
88 |
> |
* interrupted. |
89 |
|
*/ |
90 |
< |
class InterruptedSyncRunnable implements Runnable { |
90 |
> |
class InterruptedSyncRunnable extends CheckedInterruptedRunnable { |
91 |
|
final Mutex sync; |
92 |
|
InterruptedSyncRunnable(Mutex l) { sync = l; } |
93 |
< |
public void run() { |
94 |
< |
try { |
93 |
< |
sync.acquireInterruptibly(1); |
94 |
< |
threadShouldThrow(); |
95 |
< |
} catch (InterruptedException success) {} |
93 |
> |
public void realRun() throws InterruptedException { |
94 |
> |
sync.acquireInterruptibly(1); |
95 |
|
} |
96 |
|
} |
97 |
|
|
99 |
|
* isHeldExclusively is false upon construction |
100 |
|
*/ |
101 |
|
public void testIsHeldExclusively() { |
102 |
< |
Mutex rl = new Mutex(); |
102 |
> |
Mutex rl = new Mutex(); |
103 |
|
assertFalse(rl.isHeldExclusively()); |
104 |
|
} |
105 |
|
|
107 |
|
* acquiring released sync succeeds |
108 |
|
*/ |
109 |
|
public void testAcquire() { |
110 |
< |
Mutex rl = new Mutex(); |
110 |
> |
Mutex rl = new Mutex(); |
111 |
|
rl.acquire(1); |
112 |
|
assertTrue(rl.isHeldExclusively()); |
113 |
|
rl.release(1); |
118 |
|
* tryAcquire on an released sync succeeds |
119 |
|
*/ |
120 |
|
public void testTryAcquire() { |
121 |
< |
Mutex rl = new Mutex(); |
121 |
> |
Mutex rl = new Mutex(); |
122 |
|
assertTrue(rl.tryAcquire(1)); |
123 |
|
assertTrue(rl.isHeldExclusively()); |
124 |
|
rl.release(1); |
128 |
|
* hasQueuedThreads reports whether there are waiting threads |
129 |
|
*/ |
130 |
|
public void testhasQueuedThreads() throws InterruptedException { |
131 |
< |
final Mutex sync = new Mutex(); |
131 |
> |
final Mutex sync = new Mutex(); |
132 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
133 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
134 |
|
assertFalse(sync.hasQueuedThreads()); |
153 |
|
* isQueued(null) throws NPE |
154 |
|
*/ |
155 |
|
public void testIsQueuedNPE() { |
156 |
< |
final Mutex sync = new Mutex(); |
156 |
> |
final Mutex sync = new Mutex(); |
157 |
|
try { |
158 |
|
sync.isQueued(null); |
159 |
|
shouldThrow(); |
164 |
|
* isQueued reports whether a thread is queued. |
165 |
|
*/ |
166 |
|
public void testIsQueued() throws InterruptedException { |
167 |
< |
final Mutex sync = new Mutex(); |
167 |
> |
final Mutex sync = new Mutex(); |
168 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
169 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
170 |
|
assertFalse(sync.isQueued(t1)); |
194 |
|
* getFirstQueuedThread returns first waiting thread or null if none |
195 |
|
*/ |
196 |
|
public void testGetFirstQueuedThread() throws InterruptedException { |
197 |
< |
final Mutex sync = new Mutex(); |
197 |
> |
final Mutex sync = new Mutex(); |
198 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
199 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
200 |
|
assertNull(sync.getFirstQueuedThread()); |
221 |
|
* hasContended reports false if no thread has ever blocked, else true |
222 |
|
*/ |
223 |
|
public void testHasContended() throws InterruptedException { |
224 |
< |
final Mutex sync = new Mutex(); |
224 |
> |
final Mutex sync = new Mutex(); |
225 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
226 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
227 |
|
assertFalse(sync.hasContended()); |
246 |
|
* getQueuedThreads includes waiting threads |
247 |
|
*/ |
248 |
|
public void testGetQueuedThreads() throws InterruptedException { |
249 |
< |
final Mutex sync = new Mutex(); |
249 |
> |
final Mutex sync = new Mutex(); |
250 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
251 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
252 |
|
assertTrue(sync.getQueuedThreads().isEmpty()); |
274 |
|
* getExclusiveQueuedThreads includes waiting threads |
275 |
|
*/ |
276 |
|
public void testGetExclusiveQueuedThreads() throws InterruptedException { |
277 |
< |
final Mutex sync = new Mutex(); |
277 |
> |
final Mutex sync = new Mutex(); |
278 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
279 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
280 |
|
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
302 |
|
* getSharedQueuedThreads does not include exclusively waiting threads |
303 |
|
*/ |
304 |
|
public void testGetSharedQueuedThreads() throws InterruptedException { |
305 |
< |
final Mutex sync = new Mutex(); |
305 |
> |
final Mutex sync = new Mutex(); |
306 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
307 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
308 |
|
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
328 |
|
* tryAcquireNanos is interruptible. |
329 |
|
*/ |
330 |
|
public void testInterruptedException2() throws InterruptedException { |
331 |
< |
final Mutex sync = new Mutex(); |
332 |
< |
sync.acquire(1); |
333 |
< |
Thread t = new Thread(new Runnable() { |
334 |
< |
public void run() { |
335 |
< |
try { |
336 |
< |
sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); |
338 |
< |
threadShouldThrow(); |
339 |
< |
} catch (InterruptedException success) {} |
340 |
< |
} |
341 |
< |
}); |
331 |
> |
final Mutex sync = new Mutex(); |
332 |
> |
sync.acquire(1); |
333 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
334 |
> |
public void realRun() throws InterruptedException { |
335 |
> |
sync.tryAcquireNanos(1, MILLISECONDS.toNanos(MEDIUM_DELAY_MS)); |
336 |
> |
}}); |
337 |
|
|
338 |
|
t.start(); |
339 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
340 |
|
t.interrupt(); |
341 |
|
t.join(); |
342 |
|
} |
346 |
|
* TryAcquire on exclusively held sync fails |
347 |
|
*/ |
348 |
|
public void testTryAcquireWhenSynced() throws InterruptedException { |
349 |
< |
final Mutex sync = new Mutex(); |
350 |
< |
sync.acquire(1); |
351 |
< |
Thread t = new Thread(new Runnable() { |
352 |
< |
public void run() { |
353 |
< |
threadAssertFalse(sync.tryAcquire(1)); |
354 |
< |
} |
359 |
< |
}); |
349 |
> |
final Mutex sync = new Mutex(); |
350 |
> |
sync.acquire(1); |
351 |
> |
Thread t = new Thread(new CheckedRunnable() { |
352 |
> |
public void realRun() { |
353 |
> |
assertFalse(sync.tryAcquire(1)); |
354 |
> |
}}); |
355 |
|
|
356 |
|
t.start(); |
357 |
|
t.join(); |
362 |
|
* tryAcquireNanos on an exclusively held sync times out |
363 |
|
*/ |
364 |
|
public void testAcquireNanos_Timeout() throws InterruptedException { |
365 |
< |
final Mutex sync = new Mutex(); |
366 |
< |
sync.acquire(1); |
367 |
< |
Thread t = new Thread(new Runnable() { |
368 |
< |
public void run() { |
369 |
< |
try { |
370 |
< |
threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000)); |
371 |
< |
} catch (Exception ex) { |
377 |
< |
threadUnexpectedException(ex); |
378 |
< |
} |
379 |
< |
} |
380 |
< |
}); |
365 |
> |
final Mutex sync = new Mutex(); |
366 |
> |
sync.acquire(1); |
367 |
> |
Thread t = new Thread(new CheckedRunnable() { |
368 |
> |
public void realRun() throws InterruptedException { |
369 |
> |
long nanos = MILLISECONDS.toNanos(SHORT_DELAY_MS); |
370 |
> |
assertFalse(sync.tryAcquireNanos(1, nanos)); |
371 |
> |
}}); |
372 |
|
|
373 |
|
t.start(); |
374 |
|
t.join(); |
380 |
|
* getState is true when acquired and false when not |
381 |
|
*/ |
382 |
|
public void testGetState() throws InterruptedException { |
383 |
< |
final Mutex sync = new Mutex(); |
384 |
< |
sync.acquire(1); |
385 |
< |
assertTrue(sync.isHeldExclusively()); |
386 |
< |
sync.release(1); |
387 |
< |
assertFalse(sync.isHeldExclusively()); |
388 |
< |
Thread t = new Thread(new Runnable() { |
389 |
< |
public void run() { |
390 |
< |
sync.acquire(1); |
391 |
< |
try { |
392 |
< |
Thread.sleep(SMALL_DELAY_MS); |
393 |
< |
} |
403 |
< |
catch (Exception e) { |
404 |
< |
threadUnexpectedException(e); |
405 |
< |
} |
406 |
< |
sync.release(1); |
407 |
< |
} |
408 |
< |
}); |
383 |
> |
final Mutex sync = new Mutex(); |
384 |
> |
sync.acquire(1); |
385 |
> |
assertTrue(sync.isHeldExclusively()); |
386 |
> |
sync.release(1); |
387 |
> |
assertFalse(sync.isHeldExclusively()); |
388 |
> |
Thread t = new Thread(new CheckedRunnable() { |
389 |
> |
public void realRun() throws InterruptedException { |
390 |
> |
sync.acquire(1); |
391 |
> |
Thread.sleep(SMALL_DELAY_MS); |
392 |
> |
sync.release(1); |
393 |
> |
}}); |
394 |
|
|
395 |
|
t.start(); |
396 |
|
Thread.sleep(SHORT_DELAY_MS); |
404 |
|
* acquireInterruptibly is interruptible. |
405 |
|
*/ |
406 |
|
public void testAcquireInterruptibly1() throws InterruptedException { |
407 |
< |
final Mutex sync = new Mutex(); |
408 |
< |
sync.acquire(1); |
409 |
< |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
407 |
> |
final Mutex sync = new Mutex(); |
408 |
> |
sync.acquire(1); |
409 |
> |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
410 |
|
t.start(); |
411 |
|
Thread.sleep(SHORT_DELAY_MS); |
412 |
|
t.interrupt(); |
419 |
|
* acquireInterruptibly succeeds when released, else is interruptible |
420 |
|
*/ |
421 |
|
public void testAcquireInterruptibly2() throws InterruptedException { |
422 |
< |
final Mutex sync = new Mutex(); |
422 |
> |
final Mutex sync = new Mutex(); |
423 |
|
sync.acquireInterruptibly(1); |
424 |
< |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
424 |
> |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
425 |
|
t.start(); |
426 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
427 |
|
t.interrupt(); |
428 |
|
assertTrue(sync.isHeldExclusively()); |
429 |
|
t.join(); |
433 |
|
* owns is true for a condition created by sync else false |
434 |
|
*/ |
435 |
|
public void testOwns() { |
436 |
< |
final Mutex sync = new Mutex(); |
436 |
> |
final Mutex sync = new Mutex(); |
437 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
438 |
|
final Mutex sync2 = new Mutex(); |
439 |
|
assertTrue(sync.owns(c)); |
444 |
|
* Calling await without holding sync throws IllegalMonitorStateException |
445 |
|
*/ |
446 |
|
public void testAwait_IllegalMonitor() throws InterruptedException { |
447 |
< |
final Mutex sync = new Mutex(); |
447 |
> |
final Mutex sync = new Mutex(); |
448 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
449 |
|
try { |
450 |
|
c.await(); |
451 |
|
shouldThrow(); |
452 |
< |
} |
467 |
< |
catch (IllegalMonitorStateException success) { |
468 |
< |
} |
452 |
> |
} catch (IllegalMonitorStateException success) {} |
453 |
|
} |
454 |
|
|
455 |
|
/** |
456 |
|
* Calling signal without holding sync throws IllegalMonitorStateException |
457 |
|
*/ |
458 |
|
public void testSignal_IllegalMonitor() throws InterruptedException { |
459 |
< |
final Mutex sync = new Mutex(); |
459 |
> |
final Mutex sync = new Mutex(); |
460 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
461 |
|
try { |
462 |
|
c.signal(); |
463 |
|
shouldThrow(); |
464 |
< |
} |
481 |
< |
catch (IllegalMonitorStateException success) {} |
464 |
> |
} catch (IllegalMonitorStateException success) {} |
465 |
|
} |
466 |
|
|
467 |
|
/** |
468 |
|
* awaitNanos without a signal times out |
469 |
|
*/ |
470 |
|
public void testAwaitNanos_Timeout() throws InterruptedException { |
471 |
< |
final Mutex sync = new Mutex(); |
471 |
> |
final Mutex sync = new Mutex(); |
472 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
473 |
|
sync.acquire(1); |
474 |
|
long t = c.awaitNanos(100); |
477 |
|
} |
478 |
|
|
479 |
|
/** |
480 |
< |
* Timed await without a signal times out |
480 |
> |
* Timed await without a signal times out |
481 |
|
*/ |
482 |
|
public void testAwait_Timeout() throws InterruptedException { |
483 |
< |
final Mutex sync = new Mutex(); |
483 |
> |
final Mutex sync = new Mutex(); |
484 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
485 |
|
sync.acquire(1); |
486 |
< |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
486 |
> |
assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS)); |
487 |
|
sync.release(1); |
488 |
|
} |
489 |
|
|
491 |
|
* awaitUntil without a signal times out |
492 |
|
*/ |
493 |
|
public void testAwaitUntil_Timeout() throws InterruptedException { |
494 |
< |
final Mutex sync = new Mutex(); |
494 |
> |
final Mutex sync = new Mutex(); |
495 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
496 |
|
sync.acquire(1); |
497 |
|
java.util.Date d = new java.util.Date(); |
503 |
|
* await returns when signalled |
504 |
|
*/ |
505 |
|
public void testAwait() throws InterruptedException { |
506 |
< |
final Mutex sync = new Mutex(); |
506 |
> |
final Mutex sync = new Mutex(); |
507 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
508 |
< |
Thread t = new Thread(new Runnable() { |
509 |
< |
public void run() { |
510 |
< |
try { |
511 |
< |
sync.acquire(1); |
512 |
< |
c.await(); |
513 |
< |
sync.release(1); |
531 |
< |
} |
532 |
< |
catch (InterruptedException e) { |
533 |
< |
threadUnexpectedException(e); |
534 |
< |
} |
535 |
< |
} |
536 |
< |
}); |
508 |
> |
Thread t = new Thread(new CheckedRunnable() { |
509 |
> |
public void realRun() throws InterruptedException { |
510 |
> |
sync.acquire(1); |
511 |
> |
c.await(); |
512 |
> |
sync.release(1); |
513 |
> |
}}); |
514 |
|
|
515 |
|
t.start(); |
516 |
|
Thread.sleep(SHORT_DELAY_MS); |
527 |
|
* hasWaiters throws NPE if null |
528 |
|
*/ |
529 |
|
public void testHasWaitersNPE() { |
530 |
< |
final Mutex sync = new Mutex(); |
530 |
> |
final Mutex sync = new Mutex(); |
531 |
|
try { |
532 |
|
sync.hasWaiters(null); |
533 |
|
shouldThrow(); |
538 |
|
* getWaitQueueLength throws NPE if null |
539 |
|
*/ |
540 |
|
public void testGetWaitQueueLengthNPE() { |
541 |
< |
final Mutex sync = new Mutex(); |
541 |
> |
final Mutex sync = new Mutex(); |
542 |
|
try { |
543 |
|
sync.getWaitQueueLength(null); |
544 |
|
shouldThrow(); |
550 |
|
* getWaitingThreads throws NPE if null |
551 |
|
*/ |
552 |
|
public void testGetWaitingThreadsNPE() { |
553 |
< |
final Mutex sync = new Mutex(); |
553 |
> |
final Mutex sync = new Mutex(); |
554 |
|
try { |
555 |
|
sync.getWaitingThreads(null); |
556 |
|
shouldThrow(); |
562 |
|
* hasWaiters throws IAE if not owned |
563 |
|
*/ |
564 |
|
public void testHasWaitersIAE() { |
565 |
< |
final Mutex sync = new Mutex(); |
565 |
> |
final Mutex sync = new Mutex(); |
566 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
567 |
< |
final Mutex sync2 = new Mutex(); |
567 |
> |
final Mutex sync2 = new Mutex(); |
568 |
|
try { |
569 |
|
sync2.hasWaiters(c); |
570 |
|
shouldThrow(); |
575 |
|
* hasWaiters throws IMSE if not synced |
576 |
|
*/ |
577 |
|
public void testHasWaitersIMSE() { |
578 |
< |
final Mutex sync = new Mutex(); |
578 |
> |
final Mutex sync = new Mutex(); |
579 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
580 |
|
try { |
581 |
|
sync.hasWaiters(c); |
588 |
|
* getWaitQueueLength throws IAE if not owned |
589 |
|
*/ |
590 |
|
public void testGetWaitQueueLengthIAE() { |
591 |
< |
final Mutex sync = new Mutex(); |
591 |
> |
final Mutex sync = new Mutex(); |
592 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
593 |
< |
final Mutex sync2 = new Mutex(); |
593 |
> |
final Mutex sync2 = new Mutex(); |
594 |
|
try { |
595 |
|
sync2.getWaitQueueLength(c); |
596 |
|
shouldThrow(); |
601 |
|
* getWaitQueueLength throws IMSE if not synced |
602 |
|
*/ |
603 |
|
public void testGetWaitQueueLengthIMSE() { |
604 |
< |
final Mutex sync = new Mutex(); |
604 |
> |
final Mutex sync = new Mutex(); |
605 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
606 |
|
try { |
607 |
|
sync.getWaitQueueLength(c); |
614 |
|
* getWaitingThreads throws IAE if not owned |
615 |
|
*/ |
616 |
|
public void testGetWaitingThreadsIAE() { |
617 |
< |
final Mutex sync = new Mutex(); |
617 |
> |
final Mutex sync = new Mutex(); |
618 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
619 |
< |
final Mutex sync2 = new Mutex(); |
619 |
> |
final Mutex sync2 = new Mutex(); |
620 |
|
try { |
621 |
|
sync2.getWaitingThreads(c); |
622 |
|
shouldThrow(); |
627 |
|
* getWaitingThreads throws IMSE if not synced |
628 |
|
*/ |
629 |
|
public void testGetWaitingThreadsIMSE() { |
630 |
< |
final Mutex sync = new Mutex(); |
630 |
> |
final Mutex sync = new Mutex(); |
631 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
632 |
|
try { |
633 |
|
sync.getWaitingThreads(c); |
641 |
|
* hasWaiters returns true when a thread is waiting, else false |
642 |
|
*/ |
643 |
|
public void testHasWaiters() throws InterruptedException { |
644 |
< |
final Mutex sync = new Mutex(); |
644 |
> |
final Mutex sync = new Mutex(); |
645 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
646 |
< |
Thread t = new Thread(new Runnable() { |
647 |
< |
public void run() { |
648 |
< |
try { |
649 |
< |
sync.acquire(1); |
650 |
< |
threadAssertFalse(sync.hasWaiters(c)); |
651 |
< |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
652 |
< |
c.await(); |
653 |
< |
sync.release(1); |
677 |
< |
} |
678 |
< |
catch (InterruptedException e) { |
679 |
< |
threadUnexpectedException(e); |
680 |
< |
} |
681 |
< |
} |
682 |
< |
}); |
646 |
> |
Thread t = new Thread(new CheckedRunnable() { |
647 |
> |
public void realRun() throws InterruptedException { |
648 |
> |
sync.acquire(1); |
649 |
> |
assertFalse(sync.hasWaiters(c)); |
650 |
> |
assertEquals(0, sync.getWaitQueueLength(c)); |
651 |
> |
c.await(); |
652 |
> |
sync.release(1); |
653 |
> |
}}); |
654 |
|
|
655 |
|
t.start(); |
656 |
|
Thread.sleep(SHORT_DELAY_MS); |
672 |
|
* getWaitQueueLength returns number of waiting threads |
673 |
|
*/ |
674 |
|
public void testGetWaitQueueLength() throws InterruptedException { |
675 |
< |
final Mutex sync = new Mutex(); |
675 |
> |
final Mutex sync = new Mutex(); |
676 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
677 |
< |
Thread t1 = new Thread(new Runnable() { |
678 |
< |
public void run() { |
679 |
< |
try { |
680 |
< |
sync.acquire(1); |
681 |
< |
threadAssertFalse(sync.hasWaiters(c)); |
682 |
< |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
683 |
< |
c.await(); |
684 |
< |
sync.release(1); |
685 |
< |
} |
686 |
< |
catch (InterruptedException e) { |
687 |
< |
threadUnexpectedException(e); |
688 |
< |
} |
689 |
< |
} |
690 |
< |
}); |
691 |
< |
|
692 |
< |
Thread t2 = new Thread(new Runnable() { |
693 |
< |
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 |
< |
}); |
677 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
678 |
> |
public void realRun() throws InterruptedException { |
679 |
> |
sync.acquire(1); |
680 |
> |
assertFalse(sync.hasWaiters(c)); |
681 |
> |
assertEquals(0, sync.getWaitQueueLength(c)); |
682 |
> |
c.await(); |
683 |
> |
sync.release(1); |
684 |
> |
}}); |
685 |
> |
|
686 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
687 |
> |
public void realRun() throws InterruptedException { |
688 |
> |
sync.acquire(1); |
689 |
> |
assertTrue(sync.hasWaiters(c)); |
690 |
> |
assertEquals(1, sync.getWaitQueueLength(c)); |
691 |
> |
c.await(); |
692 |
> |
sync.release(1); |
693 |
> |
}}); |
694 |
|
|
695 |
|
t1.start(); |
696 |
|
Thread.sleep(SHORT_DELAY_MS); |
716 |
|
* getWaitingThreads returns only and all waiting threads |
717 |
|
*/ |
718 |
|
public void testGetWaitingThreads() throws InterruptedException { |
719 |
< |
final Mutex sync = new Mutex(); |
719 |
> |
final Mutex sync = new Mutex(); |
720 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
721 |
< |
Thread t1 = new Thread(new Runnable() { |
722 |
< |
public void run() { |
723 |
< |
try { |
724 |
< |
sync.acquire(1); |
725 |
< |
threadAssertTrue(sync.getWaitingThreads(c).isEmpty()); |
726 |
< |
c.await(); |
727 |
< |
sync.release(1); |
728 |
< |
} |
729 |
< |
catch (InterruptedException e) { |
730 |
< |
threadUnexpectedException(e); |
731 |
< |
} |
732 |
< |
} |
733 |
< |
}); |
734 |
< |
|
735 |
< |
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 |
< |
}); |
721 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
722 |
> |
public void realRun() throws InterruptedException { |
723 |
> |
sync.acquire(1); |
724 |
> |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
725 |
> |
c.await(); |
726 |
> |
sync.release(1); |
727 |
> |
}}); |
728 |
> |
|
729 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
730 |
> |
public void realRun() throws InterruptedException { |
731 |
> |
sync.acquire(1); |
732 |
> |
assertFalse(sync.getWaitingThreads(c).isEmpty()); |
733 |
> |
c.await(); |
734 |
> |
sync.release(1); |
735 |
> |
}}); |
736 |
|
|
737 |
|
sync.acquire(1); |
738 |
|
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
764 |
|
* awaitUninterruptibly doesn't abort on interrupt |
765 |
|
*/ |
766 |
|
public void testAwaitUninterruptibly() throws InterruptedException { |
767 |
< |
final Mutex sync = new Mutex(); |
767 |
> |
final Mutex sync = new Mutex(); |
768 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
769 |
< |
Thread t = new Thread(new Runnable() { |
770 |
< |
public void run() { |
771 |
< |
sync.acquire(1); |
772 |
< |
c.awaitUninterruptibly(); |
773 |
< |
sync.release(1); |
774 |
< |
} |
828 |
< |
}); |
769 |
> |
Thread t = new Thread(new CheckedRunnable() { |
770 |
> |
public void realRun() { |
771 |
> |
sync.acquire(1); |
772 |
> |
c.awaitUninterruptibly(); |
773 |
> |
sync.release(1); |
774 |
> |
}}); |
775 |
|
|
776 |
|
t.start(); |
777 |
|
Thread.sleep(SHORT_DELAY_MS); |
787 |
|
* await is interruptible |
788 |
|
*/ |
789 |
|
public void testAwait_Interrupt() throws InterruptedException { |
790 |
< |
final Mutex sync = new Mutex(); |
790 |
> |
final Mutex sync = new Mutex(); |
791 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
792 |
< |
Thread t = new Thread(new Runnable() { |
793 |
< |
public void run() { |
794 |
< |
try { |
795 |
< |
sync.acquire(1); |
796 |
< |
c.await(); |
851 |
< |
sync.release(1); |
852 |
< |
threadShouldThrow(); |
853 |
< |
} |
854 |
< |
catch (InterruptedException success) { |
855 |
< |
} |
856 |
< |
} |
857 |
< |
}); |
792 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
793 |
> |
public void realRun() throws InterruptedException { |
794 |
> |
sync.acquire(1); |
795 |
> |
c.await(); |
796 |
> |
}}); |
797 |
|
|
798 |
|
t.start(); |
799 |
|
Thread.sleep(SHORT_DELAY_MS); |
806 |
|
* awaitNanos is interruptible |
807 |
|
*/ |
808 |
|
public void testAwaitNanos_Interrupt() throws InterruptedException { |
809 |
< |
final Mutex sync = new Mutex(); |
809 |
> |
final Mutex sync = new Mutex(); |
810 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
811 |
< |
Thread t = new Thread(new Runnable() { |
812 |
< |
public void run() { |
813 |
< |
try { |
814 |
< |
sync.acquire(1); |
815 |
< |
c.awaitNanos(1000 * 1000 * 1000); // 1 sec |
877 |
< |
sync.release(1); |
878 |
< |
threadShouldThrow(); |
879 |
< |
} |
880 |
< |
catch (InterruptedException success) { |
881 |
< |
} |
882 |
< |
} |
883 |
< |
}); |
811 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
812 |
> |
public void realRun() throws InterruptedException { |
813 |
> |
sync.acquire(1); |
814 |
> |
c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); |
815 |
> |
}}); |
816 |
|
|
817 |
|
t.start(); |
818 |
|
Thread.sleep(SHORT_DELAY_MS); |
825 |
|
* awaitUntil is interruptible |
826 |
|
*/ |
827 |
|
public void testAwaitUntil_Interrupt() throws InterruptedException { |
828 |
< |
final Mutex sync = new Mutex(); |
828 |
> |
final Mutex sync = new Mutex(); |
829 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
830 |
< |
Thread t = new Thread(new Runnable() { |
831 |
< |
public void run() { |
832 |
< |
try { |
833 |
< |
sync.acquire(1); |
834 |
< |
java.util.Date d = new java.util.Date(); |
835 |
< |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
904 |
< |
sync.release(1); |
905 |
< |
threadShouldThrow(); |
906 |
< |
} |
907 |
< |
catch (InterruptedException success) { |
908 |
< |
} |
909 |
< |
} |
910 |
< |
}); |
830 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
831 |
> |
public void realRun() throws InterruptedException { |
832 |
> |
sync.acquire(1); |
833 |
> |
java.util.Date d = new java.util.Date(); |
834 |
> |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
835 |
> |
}}); |
836 |
|
|
837 |
|
t.start(); |
838 |
|
Thread.sleep(SHORT_DELAY_MS); |
845 |
|
* signalAll wakes up all threads |
846 |
|
*/ |
847 |
|
public void testSignalAll() throws InterruptedException { |
848 |
< |
final Mutex sync = new Mutex(); |
848 |
> |
final Mutex sync = new Mutex(); |
849 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
850 |
< |
Thread t1 = new Thread(new Runnable() { |
851 |
< |
public void run() { |
852 |
< |
try { |
853 |
< |
sync.acquire(1); |
854 |
< |
c.await(); |
855 |
< |
sync.release(1); |
856 |
< |
} |
857 |
< |
catch (InterruptedException e) { |
858 |
< |
threadUnexpectedException(); |
859 |
< |
} |
860 |
< |
} |
861 |
< |
}); |
862 |
< |
|
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 |
< |
}); |
850 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
851 |
> |
public void realRun() throws InterruptedException { |
852 |
> |
sync.acquire(1); |
853 |
> |
c.await(); |
854 |
> |
sync.release(1); |
855 |
> |
}}); |
856 |
> |
|
857 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
858 |
> |
public void realRun() throws InterruptedException { |
859 |
> |
sync.acquire(1); |
860 |
> |
c.await(); |
861 |
> |
sync.release(1); |
862 |
> |
}}); |
863 |
|
|
864 |
|
t1.start(); |
865 |
|
t2.start(); |
910 |
|
* tryReleaseShared setting state changes getState |
911 |
|
*/ |
912 |
|
public void testGetStateWithReleaseShared() { |
913 |
< |
final BooleanLatch l = new BooleanLatch(); |
914 |
< |
assertFalse(l.isSignalled()); |
915 |
< |
l.releaseShared(0); |
916 |
< |
assertTrue(l.isSignalled()); |
913 |
> |
final BooleanLatch l = new BooleanLatch(); |
914 |
> |
assertFalse(l.isSignalled()); |
915 |
> |
l.releaseShared(0); |
916 |
> |
assertTrue(l.isSignalled()); |
917 |
|
} |
918 |
|
|
919 |
|
/** |
920 |
|
* releaseShared has no effect when already signalled |
921 |
|
*/ |
922 |
|
public void testReleaseShared() { |
923 |
< |
final BooleanLatch l = new BooleanLatch(); |
924 |
< |
assertFalse(l.isSignalled()); |
925 |
< |
l.releaseShared(0); |
926 |
< |
assertTrue(l.isSignalled()); |
927 |
< |
l.releaseShared(0); |
928 |
< |
assertTrue(l.isSignalled()); |
923 |
> |
final BooleanLatch l = new BooleanLatch(); |
924 |
> |
assertFalse(l.isSignalled()); |
925 |
> |
l.releaseShared(0); |
926 |
> |
assertTrue(l.isSignalled()); |
927 |
> |
l.releaseShared(0); |
928 |
> |
assertTrue(l.isSignalled()); |
929 |
|
} |
930 |
|
|
931 |
|
/** |
932 |
|
* acquireSharedInterruptibly returns after release, but not before |
933 |
|
*/ |
934 |
|
public void testAcquireSharedInterruptibly() throws InterruptedException { |
935 |
< |
final BooleanLatch l = new BooleanLatch(); |
935 |
> |
final BooleanLatch l = new BooleanLatch(); |
936 |
|
|
937 |
< |
Thread t = new Thread(new Runnable() { |
938 |
< |
public void run() { |
939 |
< |
try { |
940 |
< |
threadAssertFalse(l.isSignalled()); |
941 |
< |
l.acquireSharedInterruptibly(0); |
942 |
< |
threadAssertTrue(l.isSignalled()); |
1030 |
< |
} catch (InterruptedException e) { |
1031 |
< |
threadUnexpectedException(); |
1032 |
< |
} |
1033 |
< |
} |
1034 |
< |
}); |
937 |
> |
Thread t = new Thread(new CheckedRunnable() { |
938 |
> |
public void realRun() throws InterruptedException { |
939 |
> |
assertFalse(l.isSignalled()); |
940 |
> |
l.acquireSharedInterruptibly(0); |
941 |
> |
assertTrue(l.isSignalled()); |
942 |
> |
}}); |
943 |
|
|
944 |
|
t.start(); |
945 |
|
assertFalse(l.isSignalled()); |
953 |
|
/** |
954 |
|
* acquireSharedTimed returns after release |
955 |
|
*/ |
956 |
< |
public void testAsquireSharedTimed() throws InterruptedException { |
957 |
< |
final BooleanLatch l = new BooleanLatch(); |
956 |
> |
public void testAcquireSharedTimed() throws InterruptedException { |
957 |
> |
final BooleanLatch l = new BooleanLatch(); |
958 |
|
|
959 |
< |
Thread t = new Thread(new Runnable() { |
960 |
< |
public void run() { |
961 |
< |
try { |
962 |
< |
threadAssertFalse(l.isSignalled()); |
963 |
< |
threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); |
964 |
< |
threadAssertTrue(l.isSignalled()); |
965 |
< |
|
1058 |
< |
} catch (InterruptedException e) { |
1059 |
< |
threadUnexpectedException(); |
1060 |
< |
} |
1061 |
< |
} |
1062 |
< |
}); |
959 |
> |
Thread t = new Thread(new CheckedRunnable() { |
960 |
> |
public void realRun() throws InterruptedException { |
961 |
> |
assertFalse(l.isSignalled()); |
962 |
> |
long nanos = MILLISECONDS.toNanos(MEDIUM_DELAY_MS); |
963 |
> |
assertTrue(l.tryAcquireSharedNanos(0, nanos)); |
964 |
> |
assertTrue(l.isSignalled()); |
965 |
> |
}}); |
966 |
|
|
967 |
|
t.start(); |
968 |
|
assertFalse(l.isSignalled()); |
975 |
|
/** |
976 |
|
* acquireSharedInterruptibly throws IE if interrupted before released |
977 |
|
*/ |
978 |
< |
public void testAcquireSharedInterruptibly_InterruptedException() throws InterruptedException { |
978 |
> |
public void testAcquireSharedInterruptibly_InterruptedException() |
979 |
> |
throws InterruptedException { |
980 |
|
final BooleanLatch l = new BooleanLatch(); |
981 |
< |
Thread t = new Thread(new Runnable() { |
982 |
< |
public void run() { |
983 |
< |
try { |
984 |
< |
threadAssertFalse(l.isSignalled()); |
985 |
< |
l.acquireSharedInterruptibly(0); |
1082 |
< |
threadShouldThrow(); |
1083 |
< |
} catch (InterruptedException success) {} |
1084 |
< |
} |
1085 |
< |
}); |
981 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
982 |
> |
public void realRun() throws InterruptedException { |
983 |
> |
assertFalse(l.isSignalled()); |
984 |
> |
l.acquireSharedInterruptibly(0); |
985 |
> |
}}); |
986 |
|
|
987 |
< |
t.start(); |
987 |
> |
t.start(); |
988 |
|
assertFalse(l.isSignalled()); |
989 |
|
t.interrupt(); |
990 |
|
t.join(); |
995 |
|
*/ |
996 |
|
public void testAcquireSharedNanos_InterruptedException() throws InterruptedException { |
997 |
|
final BooleanLatch l = new BooleanLatch(); |
998 |
< |
Thread t = new Thread(new Runnable() { |
999 |
< |
public void run() { |
1000 |
< |
try { |
1001 |
< |
threadAssertFalse(l.isSignalled()); |
1002 |
< |
l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); |
1003 |
< |
threadShouldThrow(); |
1104 |
< |
} catch (InterruptedException success) {} |
1105 |
< |
} |
1106 |
< |
}); |
998 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
999 |
> |
public void realRun() throws InterruptedException { |
1000 |
> |
assertFalse(l.isSignalled()); |
1001 |
> |
long nanos = MILLISECONDS.toNanos(SMALL_DELAY_MS); |
1002 |
> |
l.tryAcquireSharedNanos(0, nanos); |
1003 |
> |
}}); |
1004 |
|
|
1005 |
|
t.start(); |
1006 |
|
Thread.sleep(SHORT_DELAY_MS); |
1014 |
|
*/ |
1015 |
|
public void testAcquireSharedNanos_Timeout() throws InterruptedException { |
1016 |
|
final BooleanLatch l = new BooleanLatch(); |
1017 |
< |
Thread t = new Thread(new Runnable() { |
1018 |
< |
public void run() { |
1019 |
< |
try { |
1020 |
< |
threadAssertFalse(l.isSignalled()); |
1021 |
< |
threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); |
1022 |
< |
} catch (InterruptedException ie) { |
1126 |
< |
threadUnexpectedException(); |
1127 |
< |
} |
1128 |
< |
} |
1129 |
< |
}); |
1017 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1018 |
> |
public void realRun() throws InterruptedException { |
1019 |
> |
assertFalse(l.isSignalled()); |
1020 |
> |
long nanos = MILLISECONDS.toNanos(SMALL_DELAY_MS); |
1021 |
> |
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1022 |
> |
}}); |
1023 |
|
|
1024 |
|
t.start(); |
1025 |
|
Thread.sleep(SHORT_DELAY_MS); |
1027 |
|
t.join(); |
1028 |
|
} |
1029 |
|
|
1137 |
– |
|
1030 |
|
} |