ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantLockTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ReentrantLockTest.java (file contents):
Revision 1.8 by dl, Fri Sep 26 15:33:13 2003 UTC vs.
Revision 1.12 by dl, Sat Dec 27 19:26:43 2003 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
2 > * Written by Doug Lea with assistance from members of JCP JSR-166
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/licenses/publicdomain
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
# Line 56 | Line 57 | public class ReentrantLockTest extends J
57          public Collection<Thread> getQueuedThreads() {
58              return super.getQueuedThreads();
59          }
59        public ConditionObject newCondition() {
60            return new PublicCondition(this);
61        }
60  
61 <        static class PublicCondition extends ReentrantLock.ConditionObject {
64 <            PublicCondition(PublicReentrantLock l) { super(l); }
65 <            public Collection<Thread> getWaitingThreads() {
66 <                return super.getWaitingThreads();
67 <            }
68 <        }
61 >    }
62  
63 +    /**
64 +     * Constructor sets given fairness
65 +     */
66 +    public void testConstructor() {
67 +        ReentrantLock rl = new ReentrantLock();
68 +        assertFalse(rl.isFair());
69 +        ReentrantLock r2 = new ReentrantLock(true);
70 +        assertTrue(r2.isFair());
71      }
72  
73      /**
# Line 418 | Line 419 | public class ReentrantLockTest extends J
419       */
420      public void testAwait() {
421          final ReentrantLock lock = new ReentrantLock();
422 <        final ReentrantLock.ConditionObject c = lock.newCondition();
422 >        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
423          Thread t = new Thread(new Runnable() {
424                  public void run() {
425                      try {
# Line 451 | Line 452 | public class ReentrantLockTest extends J
452       */
453      public void testHasWaiters() {
454          final ReentrantLock lock = new ReentrantLock();
455 <        final ReentrantLock.ConditionObject c = lock.newCondition();
455 >        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
456          Thread t = new Thread(new Runnable() {
457                  public void run() {
458                      try {
# Line 493 | Line 494 | public class ReentrantLockTest extends J
494       */
495      public void testGetWaitQueueLength() {
496          final ReentrantLock lock = new ReentrantLock();
497 <        final ReentrantLock.ConditionObject c = lock.newCondition();
497 >        final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition();
498          Thread t1 = new Thread(new Runnable() {
499                  public void run() {
500                      try {
# Line 541 | Line 542 | public class ReentrantLockTest extends J
542              lock.unlock();
543              t1.join(SHORT_DELAY_MS);
544              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);
545              assertFalse(t1.isAlive());
546              assertFalse(t2.isAlive());
547          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines