56 |
|
public Collection<Thread> getQueuedThreads() { |
57 |
|
return super.getQueuedThreads(); |
58 |
|
} |
59 |
– |
public ConditionObject newCondition() { |
60 |
– |
return new PublicCondition(this); |
61 |
– |
} |
59 |
|
|
60 |
< |
static class PublicCondition extends ReentrantLock.ConditionObject { |
64 |
< |
PublicCondition(PublicReentrantLock l) { super(l); } |
65 |
< |
public Collection<Thread> getWaitingThreads() { |
66 |
< |
return super.getWaitingThreads(); |
67 |
< |
} |
68 |
< |
} |
60 |
> |
} |
61 |
|
|
62 |
+ |
/** |
63 |
+ |
* Constructor sets given fairness |
64 |
+ |
*/ |
65 |
+ |
public void testConstructor() { |
66 |
+ |
ReentrantLock rl = new ReentrantLock(); |
67 |
+ |
assertFalse(rl.isFair()); |
68 |
+ |
ReentrantLock r2 = new ReentrantLock(true); |
69 |
+ |
assertTrue(r2.isFair()); |
70 |
|
} |
71 |
|
|
72 |
|
/** |
418 |
|
*/ |
419 |
|
public void testAwait() { |
420 |
|
final ReentrantLock lock = new ReentrantLock(); |
421 |
< |
final ReentrantLock.ConditionObject c = lock.newCondition(); |
421 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition(); |
422 |
|
Thread t = new Thread(new Runnable() { |
423 |
|
public void run() { |
424 |
|
try { |
451 |
|
*/ |
452 |
|
public void testHasWaiters() { |
453 |
|
final ReentrantLock lock = new ReentrantLock(); |
454 |
< |
final ReentrantLock.ConditionObject c = lock.newCondition(); |
454 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition(); |
455 |
|
Thread t = new Thread(new Runnable() { |
456 |
|
public void run() { |
457 |
|
try { |
493 |
|
*/ |
494 |
|
public void testGetWaitQueueLength() { |
495 |
|
final ReentrantLock lock = new ReentrantLock(); |
496 |
< |
final ReentrantLock.ConditionObject c = lock.newCondition(); |
496 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition(); |
497 |
|
Thread t1 = new Thread(new Runnable() { |
498 |
|
public void run() { |
499 |
|
try { |
541 |
|
lock.unlock(); |
542 |
|
t1.join(SHORT_DELAY_MS); |
543 |
|
t2.join(SHORT_DELAY_MS); |
544 |
– |
assertFalse(t1.isAlive()); |
545 |
– |
assertFalse(t2.isAlive()); |
546 |
– |
} |
547 |
– |
catch (Exception ex) { |
548 |
– |
unexpectedException(); |
549 |
– |
} |
550 |
– |
} |
551 |
– |
|
552 |
– |
/** |
553 |
– |
* getWaitingThreads returns only and all waiting threads |
554 |
– |
*/ |
555 |
– |
public void testGetWaitingThreads() { |
556 |
– |
final PublicReentrantLock lock = new PublicReentrantLock(); |
557 |
– |
final PublicReentrantLock.PublicCondition c = (PublicReentrantLock.PublicCondition)lock.newCondition(); |
558 |
– |
Thread t1 = new Thread(new Runnable() { |
559 |
– |
public void run() { |
560 |
– |
try { |
561 |
– |
lock.lock(); |
562 |
– |
threadAssertTrue(c.getWaitingThreads().isEmpty()); |
563 |
– |
c.await(); |
564 |
– |
lock.unlock(); |
565 |
– |
} |
566 |
– |
catch(InterruptedException e) { |
567 |
– |
threadUnexpectedException(); |
568 |
– |
} |
569 |
– |
} |
570 |
– |
}); |
571 |
– |
|
572 |
– |
Thread t2 = new Thread(new Runnable() { |
573 |
– |
public void run() { |
574 |
– |
try { |
575 |
– |
lock.lock(); |
576 |
– |
threadAssertFalse(c.getWaitingThreads().isEmpty()); |
577 |
– |
c.await(); |
578 |
– |
lock.unlock(); |
579 |
– |
} |
580 |
– |
catch(InterruptedException e) { |
581 |
– |
threadUnexpectedException(); |
582 |
– |
} |
583 |
– |
} |
584 |
– |
}); |
585 |
– |
|
586 |
– |
try { |
587 |
– |
lock.lock(); |
588 |
– |
assertTrue(c.getWaitingThreads().isEmpty()); |
589 |
– |
lock.unlock(); |
590 |
– |
t1.start(); |
591 |
– |
Thread.sleep(SHORT_DELAY_MS); |
592 |
– |
t2.start(); |
593 |
– |
Thread.sleep(SHORT_DELAY_MS); |
594 |
– |
lock.lock(); |
595 |
– |
assertTrue(c.hasWaiters()); |
596 |
– |
assertTrue(c.getWaitingThreads().contains(t1)); |
597 |
– |
assertTrue(c.getWaitingThreads().contains(t2)); |
598 |
– |
c.signalAll(); |
599 |
– |
lock.unlock(); |
600 |
– |
Thread.sleep(SHORT_DELAY_MS); |
601 |
– |
lock.lock(); |
602 |
– |
assertFalse(c.hasWaiters()); |
603 |
– |
assertTrue(c.getWaitingThreads().isEmpty()); |
604 |
– |
lock.unlock(); |
605 |
– |
t1.join(SHORT_DELAY_MS); |
606 |
– |
t2.join(SHORT_DELAY_MS); |
544 |
|
assertFalse(t1.isAlive()); |
545 |
|
assertFalse(t2.isAlive()); |
546 |
|
} |