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

Comparing jsr166/src/test/tck/ReentrantReadWriteLockTest.java (file contents):
Revision 1.6 by dl, Mon Nov 10 13:32:08 2003 UTC vs.
Revision 1.20 by dl, Fri Jul 2 10:24:00 2004 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 ReentrantReadWriteLockTest
57          public Collection<Thread> getQueuedThreads() {
58              return super.getQueuedThreads();
59          }
60 <        public WriterConditionObject newCondition() {
61 <            return new PublicCondition(this);
60 >        public Collection<Thread> getWaitingThreads(Condition c) {
61 >            return super.getWaitingThreads(c);
62          }
62
63        static class PublicCondition extends ReentrantReadWriteLock.WriterConditionObject {
64            PublicCondition(PublicReentrantReadWriteLock l) { super(l); }
65            public Collection<Thread> getWaitingThreads() {
66                return super.getWaitingThreads();
67            }
68        }
69
63      }
64  
65      /**
# Line 76 | Line 69 | public class ReentrantReadWriteLockTest
69          ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
70          assertFalse(rl.isFair());
71          assertFalse(rl.isWriteLocked());
72 <        assertEquals(0, rl.getReadLocks());
72 >        assertEquals(0, rl.getReadLockCount());
73          ReentrantReadWriteLock r2 = new ReentrantReadWriteLock(true);
74          assertTrue(r2.isFair());
75          assertFalse(r2.isWriteLocked());
76 <        assertEquals(0, r2.getReadLocks());
76 >        assertEquals(0, r2.getReadLockCount());
77      }
78  
79      /**
# Line 91 | Line 84 | public class ReentrantReadWriteLockTest
84          rl.writeLock().lock();
85          assertTrue(rl.isWriteLocked());
86          assertTrue(rl.isWriteLockedByCurrentThread());
87 <        assertEquals(0, rl.getReadLocks());
87 >        assertEquals(0, rl.getReadLockCount());
88          rl.writeLock().unlock();
89          assertFalse(rl.isWriteLocked());
90          assertFalse(rl.isWriteLockedByCurrentThread());
91 <        assertEquals(0, rl.getReadLocks());
91 >        assertEquals(0, rl.getReadLockCount());
92          rl.readLock().lock();
93          assertFalse(rl.isWriteLocked());
94          assertFalse(rl.isWriteLockedByCurrentThread());
95 <        assertEquals(1, rl.getReadLocks());
95 >        assertEquals(1, rl.getReadLockCount());
96          rl.readLock().unlock();
97          assertFalse(rl.isWriteLocked());
98          assertFalse(rl.isWriteLockedByCurrentThread());
99 <        assertEquals(0, rl.getReadLocks());
99 >        assertEquals(0, rl.getReadLockCount());
100      }
101  
102  
# Line 115 | Line 108 | public class ReentrantReadWriteLockTest
108          rl.writeLock().lock();
109          assertTrue(rl.isWriteLocked());
110          assertTrue(rl.isWriteLockedByCurrentThread());
111 <        assertEquals(0, rl.getReadLocks());
111 >        assertEquals(0, rl.getReadLockCount());
112          rl.writeLock().unlock();
113          assertFalse(rl.isWriteLocked());
114          assertFalse(rl.isWriteLockedByCurrentThread());
115 <        assertEquals(0, rl.getReadLocks());
115 >        assertEquals(0, rl.getReadLockCount());
116          rl.readLock().lock();
117          assertFalse(rl.isWriteLocked());
118          assertFalse(rl.isWriteLockedByCurrentThread());
119 <        assertEquals(1, rl.getReadLocks());
119 >        assertEquals(1, rl.getReadLockCount());
120          rl.readLock().unlock();
121          assertFalse(rl.isWriteLocked());
122          assertFalse(rl.isWriteLockedByCurrentThread());
123 <        assertEquals(0, rl.getReadLocks());
123 >        assertEquals(0, rl.getReadLockCount());
124      }
125  
126      /**
# Line 163 | Line 156 | public class ReentrantReadWriteLockTest
156       */
157      public void testWriteLockInterruptibly_Interrupted() {
158          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
166        lock.writeLock().lock();
159          Thread t = new Thread(new Runnable() {
160                  public void run() {
161                      try {
162                          lock.writeLock().lockInterruptibly();
163 <                        threadShouldThrow();
163 >                        lock.writeLock().unlock();
164 >                        lock.writeLock().lockInterruptibly();
165 >                        lock.writeLock().unlock();
166                      } catch(InterruptedException success){}
167                  }
168              });
169          try {
170 +            lock.writeLock().lock();
171              t.start();
172              t.interrupt();
173              lock.writeLock().unlock();
# Line 183 | Line 178 | public class ReentrantReadWriteLockTest
178      }
179  
180      /**
181 <     * timed write-trylock is interruptible
181 >     * timed write-tryLock is interruptible
182       */
183      public void testWriteTryLock_Interrupted() {
184          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 192 | Line 187 | public class ReentrantReadWriteLockTest
187                  public void run() {
188                      try {
189                          lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS);
195                        threadShouldThrow();
190                      } catch(InterruptedException success){}
191                  }
192              });
# Line 216 | Line 210 | public class ReentrantReadWriteLockTest
210                  public void run() {
211                      try {
212                          lock.readLock().lockInterruptibly();
219                        threadShouldThrow();
213                      } catch(InterruptedException success){}
214                  }
215              });
# Line 231 | Line 224 | public class ReentrantReadWriteLockTest
224      }
225  
226      /**
227 <     * timed read-trylock is interruptible
227 >     * timed read-tryLock is interruptible
228       */
229      public void testReadTryLock_Interrupted() {
230          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 255 | Line 248 | public class ReentrantReadWriteLockTest
248  
249      
250      /**
251 <     * write-trylock fails if locked
251 >     * write-tryLock fails if locked
252       */
253      public void testWriteTryLockWhenLocked() {
254          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 275 | Line 268 | public class ReentrantReadWriteLockTest
268      }
269  
270      /**
271 <     * read-trylock fails if locked
271 >     * read-tryLock fails if locked
272       */
273      public void testReadTryLockWhenLocked() {
274          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 383 | Line 376 | public class ReentrantReadWriteLockTest
376          }
377      }
378  
379 +    /**
380 +     * Read trylock succeeds if write locked by current thread
381 +     */
382 +    public void testReadHoldingWriteLock() {
383 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
384 +        lock.writeLock().lock();
385 +        assertTrue(lock.readLock().tryLock());
386 +        lock.readLock().unlock();
387 +        lock.writeLock().unlock();
388 +    }
389  
390      /**
391 <     * Read trylock succeeds if readlocked but not writelocked
391 >     * Read lock succeeds if write locked by current thread even if
392 >     * other threads are waiting
393 >     */
394 >    public void testReadHoldingWriteLock2() {
395 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
396 >        lock.writeLock().lock();
397 >        Thread t1 = new Thread(new Runnable() {
398 >                public void run() {
399 >                    lock.readLock().lock();
400 >                    lock.readLock().unlock();
401 >                }
402 >            });
403 >        Thread t2 = new Thread(new Runnable() {
404 >                public void run() {
405 >                    lock.readLock().lock();
406 >                    lock.readLock().unlock();
407 >                }
408 >            });
409 >
410 >        try {
411 >            t1.start();
412 >            t2.start();
413 >            lock.readLock().lock();
414 >            lock.readLock().unlock();
415 >            Thread.sleep(SHORT_DELAY_MS);
416 >            lock.readLock().lock();
417 >            lock.readLock().unlock();
418 >            lock.writeLock().unlock();
419 >            t1.join(MEDIUM_DELAY_MS);
420 >            t2.join(MEDIUM_DELAY_MS);
421 >            assertTrue(!t1.isAlive());
422 >            assertTrue(!t2.isAlive());
423 >          
424 >        } catch(Exception e){
425 >            unexpectedException();
426 >        }
427 >    }
428 >
429 >    /**
430 >     * Fair Read trylock succeeds if write locked by current thread
431 >     */
432 >    public void testReadHoldingWriteLockFair() {
433 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
434 >        lock.writeLock().lock();
435 >        assertTrue(lock.readLock().tryLock());
436 >        lock.readLock().unlock();
437 >        lock.writeLock().unlock();
438 >    }
439 >
440 >    /**
441 >     * Fair Read lock succeeds if write locked by current thread even if
442 >     * other threads are waiting
443 >     */
444 >    public void testReadHoldingWriteLockFair2() {
445 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
446 >        lock.writeLock().lock();
447 >        Thread t1 = new Thread(new Runnable() {
448 >                public void run() {
449 >                    lock.readLock().lock();
450 >                    lock.readLock().unlock();
451 >                }
452 >            });
453 >        Thread t2 = new Thread(new Runnable() {
454 >                public void run() {
455 >                    lock.readLock().lock();
456 >                    lock.readLock().unlock();
457 >                }
458 >            });
459 >
460 >        try {
461 >            t1.start();
462 >            t2.start();
463 >            lock.readLock().lock();
464 >            lock.readLock().unlock();
465 >            Thread.sleep(SHORT_DELAY_MS);
466 >            lock.readLock().lock();
467 >            lock.readLock().unlock();
468 >            lock.writeLock().unlock();
469 >            t1.join(MEDIUM_DELAY_MS);
470 >            t2.join(MEDIUM_DELAY_MS);
471 >            assertTrue(!t1.isAlive());
472 >            assertTrue(!t2.isAlive());
473 >          
474 >        } catch(Exception e){
475 >            unexpectedException();
476 >        }
477 >    }
478 >
479 >
480 >    /**
481 >     * Read tryLock succeeds if readlocked but not writelocked
482       */
483      public void testTryLockWhenReadLocked() {
484          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 408 | Line 501 | public class ReentrantReadWriteLockTest
501      
502  
503      /**
504 <     * write trylock fails when readlocked
504 >     * write tryLock fails when readlocked
505       */
506      public void testWriteTryLockWhenReadLocked() {
507          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 430 | Line 523 | public class ReentrantReadWriteLockTest
523      
524  
525      /**
526 <     * write timed trylock times out if locked
526 >     * write timed tryLock times out if locked
527       */
528      public void testWriteTryLock_Timeout() {
529          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 454 | Line 547 | public class ReentrantReadWriteLockTest
547      }
548  
549      /**
550 <     * read timed trylock times out if write-locked
550 >     * read timed tryLock times out if write-locked
551       */
552      public void testReadTryLock_Timeout() {
553          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 855 | Line 948 | public class ReentrantReadWriteLockTest
948      }
949  
950      /**
951 +     * hasQueuedThreads reports whether there are waiting threads
952 +     */
953 +    public void testhasQueuedThreads() {
954 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
955 +        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
956 +        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
957 +        try {
958 +            assertFalse(lock.hasQueuedThreads());
959 +            lock.writeLock().lock();
960 +            t1.start();
961 +            Thread.sleep(SHORT_DELAY_MS);
962 +            assertTrue(lock.hasQueuedThreads());
963 +            t2.start();
964 +            Thread.sleep(SHORT_DELAY_MS);
965 +            assertTrue(lock.hasQueuedThreads());
966 +            t1.interrupt();
967 +            Thread.sleep(SHORT_DELAY_MS);
968 +            assertTrue(lock.hasQueuedThreads());
969 +            lock.writeLock().unlock();
970 +            Thread.sleep(SHORT_DELAY_MS);
971 +            assertFalse(lock.hasQueuedThreads());
972 +            t1.join();
973 +            t2.join();
974 +        } catch(Exception e){
975 +            unexpectedException();
976 +        }
977 +    }
978 +
979 +    /**
980 +     * hasQueuedThread(null) throws NPE
981 +     */
982 +    public void testHasQueuedThreadNPE() {
983 +        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
984 +        try {
985 +            sync.hasQueuedThread(null);
986 +            shouldThrow();
987 +        } catch (NullPointerException success) {
988 +        }
989 +    }
990 +
991 +    /**
992 +     * hasQueuedThread reports whether a thread is queued.
993 +     */
994 +    public void testHasQueuedThread() {
995 +        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
996 +        Thread t1 = new Thread(new InterruptedLockRunnable(sync));
997 +        Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
998 +        try {
999 +            assertFalse(sync.hasQueuedThread(t1));
1000 +            assertFalse(sync.hasQueuedThread(t2));
1001 +            sync.writeLock().lock();
1002 +            t1.start();
1003 +            Thread.sleep(SHORT_DELAY_MS);
1004 +            assertTrue(sync.hasQueuedThread(t1));
1005 +            t2.start();
1006 +            Thread.sleep(SHORT_DELAY_MS);
1007 +            assertTrue(sync.hasQueuedThread(t1));
1008 +            assertTrue(sync.hasQueuedThread(t2));
1009 +            t1.interrupt();
1010 +            Thread.sleep(SHORT_DELAY_MS);
1011 +            assertFalse(sync.hasQueuedThread(t1));
1012 +            assertTrue(sync.hasQueuedThread(t2));
1013 +            sync.writeLock().unlock();
1014 +            Thread.sleep(SHORT_DELAY_MS);
1015 +            assertFalse(sync.hasQueuedThread(t1));
1016 +            Thread.sleep(SHORT_DELAY_MS);
1017 +            assertFalse(sync.hasQueuedThread(t2));
1018 +            t1.join();
1019 +            t2.join();
1020 +        } catch(Exception e){
1021 +            unexpectedException();
1022 +        }
1023 +    }
1024 +
1025 +
1026 +    /**
1027       * getQueueLength reports number of waiting threads
1028       */
1029      public void testGetQueueLength() {
# Line 916 | Line 1085 | public class ReentrantReadWriteLockTest
1085      }
1086  
1087      /**
1088 +     * hasWaiters throws NPE if null
1089 +     */
1090 +    public void testHasWaitersNPE() {
1091 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1092 +        try {
1093 +            lock.hasWaiters(null);
1094 +            shouldThrow();
1095 +        } catch (NullPointerException success) {
1096 +        } catch (Exception ex) {
1097 +            unexpectedException();
1098 +        }
1099 +    }
1100 +
1101 +    /**
1102 +     * getWaitQueueLength throws NPE if null
1103 +     */
1104 +    public void testGetWaitQueueLengthNPE() {
1105 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1106 +        try {
1107 +            lock.getWaitQueueLength(null);
1108 +            shouldThrow();
1109 +        } catch (NullPointerException success) {
1110 +        } catch (Exception ex) {
1111 +            unexpectedException();
1112 +        }
1113 +    }
1114 +
1115 +
1116 +    /**
1117 +     * getWaitingThreads throws NPE if null
1118 +     */
1119 +    public void testGetWaitingThreadsNPE() {
1120 +        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1121 +        try {
1122 +            lock.getWaitingThreads(null);
1123 +            shouldThrow();
1124 +        } catch (NullPointerException success) {
1125 +        } catch (Exception ex) {
1126 +            unexpectedException();
1127 +        }
1128 +    }
1129 +
1130 +    /**
1131 +     * hasWaiters throws IAE if not owned
1132 +     */
1133 +    public void testHasWaitersIAE() {
1134 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1135 +        final Condition c = (lock.writeLock().newCondition());
1136 +        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1137 +        try {
1138 +            lock2.hasWaiters(c);
1139 +            shouldThrow();
1140 +        } catch (IllegalArgumentException success) {
1141 +        } catch (Exception ex) {
1142 +            unexpectedException();
1143 +        }
1144 +    }
1145 +
1146 +    /**
1147 +     * hasWaiters throws IMSE if not locked
1148 +     */
1149 +    public void testHasWaitersIMSE() {
1150 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1151 +        final Condition c = (lock.writeLock().newCondition());
1152 +        try {
1153 +            lock.hasWaiters(c);
1154 +            shouldThrow();
1155 +        } catch (IllegalMonitorStateException success) {
1156 +        } catch (Exception ex) {
1157 +            unexpectedException();
1158 +        }
1159 +    }
1160 +
1161 +
1162 +    /**
1163 +     * getWaitQueueLength throws IAE if not owned
1164 +     */
1165 +    public void testGetWaitQueueLengthIAE() {
1166 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1167 +        final Condition c = (lock.writeLock().newCondition());
1168 +        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1169 +        try {
1170 +            lock2.getWaitQueueLength(c);
1171 +            shouldThrow();
1172 +        } catch (IllegalArgumentException success) {
1173 +        } catch (Exception ex) {
1174 +            unexpectedException();
1175 +        }
1176 +    }
1177 +
1178 +    /**
1179 +     * getWaitQueueLength throws IMSE if not locked
1180 +     */
1181 +    public void testGetWaitQueueLengthIMSE() {
1182 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1183 +        final Condition c = (lock.writeLock().newCondition());
1184 +        try {
1185 +            lock.getWaitQueueLength(c);
1186 +            shouldThrow();
1187 +        } catch (IllegalMonitorStateException success) {
1188 +        } catch (Exception ex) {
1189 +            unexpectedException();
1190 +        }
1191 +    }
1192 +
1193 +
1194 +    /**
1195 +     * getWaitingThreads throws IAE if not owned
1196 +     */
1197 +    public void testGetWaitingThreadsIAE() {
1198 +        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();  
1199 +        final Condition c = (lock.writeLock().newCondition());
1200 +        final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock();  
1201 +        try {
1202 +            lock2.getWaitingThreads(c);
1203 +            shouldThrow();
1204 +        } catch (IllegalArgumentException success) {
1205 +        } catch (Exception ex) {
1206 +            unexpectedException();
1207 +        }
1208 +    }
1209 +
1210 +    /**
1211 +     * getWaitingThreads throws IMSE if not locked
1212 +     */
1213 +    public void testGetWaitingThreadsIMSE() {
1214 +        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();  
1215 +        final Condition c = (lock.writeLock().newCondition());
1216 +        try {
1217 +            lock.getWaitingThreads(c);
1218 +            shouldThrow();
1219 +        } catch (IllegalMonitorStateException success) {
1220 +        } catch (Exception ex) {
1221 +            unexpectedException();
1222 +        }
1223 +    }
1224 +
1225 +
1226 +    /**
1227       * hasWaiters returns true when a thread is waiting, else false
1228       */
1229      public void testHasWaiters() {
1230 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
1231 <        final ReentrantReadWriteLock.WriterConditionObject c = (ReentrantReadWriteLock.WriterConditionObject)(lock.writeLock().newCondition());
1230 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1231 >        final Condition c = (lock.writeLock().newCondition());
1232          Thread t = new Thread(new Runnable() {
1233                  public void run() {
1234                      try {
1235                          lock.writeLock().lock();
1236 <                        threadAssertFalse(c.hasWaiters());
1237 <                        threadAssertEquals(0, c.getWaitQueueLength());
1236 >                        threadAssertFalse(lock.hasWaiters(c));
1237 >                        threadAssertEquals(0, lock.getWaitQueueLength(c));
1238                          c.await();
1239                          lock.writeLock().unlock();
1240                      }
# Line 940 | Line 1248 | public class ReentrantReadWriteLockTest
1248              t.start();
1249              Thread.sleep(SHORT_DELAY_MS);
1250              lock.writeLock().lock();
1251 <            assertTrue(c.hasWaiters());
1252 <            assertEquals(1, c.getWaitQueueLength());
1251 >            assertTrue(lock.hasWaiters(c));
1252 >            assertEquals(1, lock.getWaitQueueLength(c));
1253              c.signal();
1254              lock.writeLock().unlock();
1255              Thread.sleep(SHORT_DELAY_MS);
1256              lock.writeLock().lock();
1257 <            assertFalse(c.hasWaiters());
1258 <            assertEquals(0, c.getWaitQueueLength());
1257 >            assertFalse(lock.hasWaiters(c));
1258 >            assertEquals(0, lock.getWaitQueueLength(c));
1259              lock.writeLock().unlock();
1260              t.join(SHORT_DELAY_MS);
1261              assertFalse(t.isAlive());
# Line 961 | Line 1269 | public class ReentrantReadWriteLockTest
1269       * getWaitQueueLength returns number of waiting threads
1270       */
1271      public void testGetWaitQueueLength() {
1272 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
1273 <        final ReentrantReadWriteLock.WriterConditionObject c = (ReentrantReadWriteLock.WriterConditionObject)(lock.writeLock().newCondition());
1274 <        Thread t1 = new Thread(new Runnable() {
967 <                public void run() {
968 <                    try {
969 <                        lock.writeLock().lock();
970 <                        threadAssertFalse(c.hasWaiters());
971 <                        threadAssertEquals(0, c.getWaitQueueLength());
972 <                        c.await();
973 <                        lock.writeLock().unlock();
974 <                    }
975 <                    catch(InterruptedException e) {
976 <                        threadUnexpectedException();
977 <                    }
978 <                }
979 <            });
980 <
981 <        Thread t2 = new Thread(new Runnable() {
1272 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1273 >        final Condition c = (lock.writeLock().newCondition());
1274 >        Thread t = new Thread(new Runnable() {
1275                  public void run() {
1276                      try {
1277                          lock.writeLock().lock();
1278 <                        threadAssertTrue(c.hasWaiters());
1279 <                        threadAssertEquals(1, c.getWaitQueueLength());
1278 >                        threadAssertFalse(lock.hasWaiters(c));
1279 >                        threadAssertEquals(0, lock.getWaitQueueLength(c));
1280                          c.await();
1281                          lock.writeLock().unlock();
1282                      }
# Line 994 | Line 1287 | public class ReentrantReadWriteLockTest
1287              });
1288  
1289          try {
1290 <            t1.start();
998 <            Thread.sleep(SHORT_DELAY_MS);
999 <            t2.start();
1290 >            t.start();
1291              Thread.sleep(SHORT_DELAY_MS);
1292              lock.writeLock().lock();
1293 <            assertTrue(c.hasWaiters());
1294 <            assertEquals(2, c.getWaitQueueLength());
1295 <            c.signalAll();
1293 >            assertTrue(lock.hasWaiters(c));
1294 >            assertEquals(1, lock.getWaitQueueLength(c));
1295 >            c.signal();
1296              lock.writeLock().unlock();
1297              Thread.sleep(SHORT_DELAY_MS);
1298              lock.writeLock().lock();
1299 <            assertFalse(c.hasWaiters());
1300 <            assertEquals(0, c.getWaitQueueLength());
1299 >            assertFalse(lock.hasWaiters(c));
1300 >            assertEquals(0, lock.getWaitQueueLength(c));
1301              lock.writeLock().unlock();
1302 <            t1.join(SHORT_DELAY_MS);
1303 <            t2.join(SHORT_DELAY_MS);
1013 <            assertFalse(t1.isAlive());
1014 <            assertFalse(t2.isAlive());
1302 >            t.join(SHORT_DELAY_MS);
1303 >            assertFalse(t.isAlive());
1304          }
1305          catch (Exception ex) {
1306              unexpectedException();
1307          }
1308      }
1309  
1310 +
1311      /**
1312       * getWaitingThreads returns only and all waiting threads
1313       */
1314      public void testGetWaitingThreads() {
1315          final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();  
1316 <        final PublicReentrantReadWriteLock.PublicCondition c = (PublicReentrantReadWriteLock.PublicCondition)lock.newCondition();
1316 >        final Condition c = lock.writeLock().newCondition();
1317          Thread t1 = new Thread(new Runnable() {
1318                  public void run() {
1319                      try {
1320                          lock.writeLock().lock();
1321 <                        threadAssertTrue(c.getWaitingThreads().isEmpty());
1321 >                        threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
1322                          c.await();
1323                          lock.writeLock().unlock();
1324                      }
# Line 1042 | Line 1332 | public class ReentrantReadWriteLockTest
1332                  public void run() {
1333                      try {
1334                          lock.writeLock().lock();
1335 <                        threadAssertFalse(c.getWaitingThreads().isEmpty());
1335 >                        threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
1336                          c.await();
1337                          lock.writeLock().unlock();
1338                      }
# Line 1054 | Line 1344 | public class ReentrantReadWriteLockTest
1344  
1345          try {
1346              lock.writeLock().lock();
1347 <            assertTrue(c.getWaitingThreads().isEmpty());
1347 >            assertTrue(lock.getWaitingThreads(c).isEmpty());
1348              lock.writeLock().unlock();
1349              t1.start();
1350              Thread.sleep(SHORT_DELAY_MS);
1351              t2.start();
1352              Thread.sleep(SHORT_DELAY_MS);
1353              lock.writeLock().lock();
1354 <            assertTrue(c.hasWaiters());
1355 <            assertTrue(c.getWaitingThreads().contains(t1));
1356 <            assertTrue(c.getWaitingThreads().contains(t2));
1354 >            assertTrue(lock.hasWaiters(c));
1355 >            assertTrue(lock.getWaitingThreads(c).contains(t1));
1356 >            assertTrue(lock.getWaitingThreads(c).contains(t2));
1357              c.signalAll();
1358              lock.writeLock().unlock();
1359              Thread.sleep(SHORT_DELAY_MS);
1360              lock.writeLock().lock();
1361 <            assertFalse(c.hasWaiters());
1362 <            assertTrue(c.getWaitingThreads().isEmpty());
1361 >            assertFalse(lock.hasWaiters(c));
1362 >            assertTrue(lock.getWaitingThreads(c).isEmpty());
1363              lock.writeLock().unlock();
1364              t1.join(SHORT_DELAY_MS);
1365              t2.join(SHORT_DELAY_MS);
# Line 1081 | Line 1371 | public class ReentrantReadWriteLockTest
1371          }
1372      }
1373  
1374 +    /**
1375 +     * toString indicates current lock state
1376 +     */
1377 +    public void testToString() {
1378 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1379 +        String us = lock.toString();
1380 +        assertTrue(us.indexOf("Write locks = 0") >= 0);
1381 +        assertTrue(us.indexOf("Read locks = 0") >= 0);
1382 +        lock.writeLock().lock();
1383 +        String ws = lock.toString();
1384 +        assertTrue(ws.indexOf("Write locks = 1") >= 0);
1385 +        assertTrue(ws.indexOf("Read locks = 0") >= 0);
1386 +        lock.writeLock().unlock();
1387 +        lock.readLock().lock();
1388 +        lock.readLock().lock();
1389 +        String rs = lock.toString();
1390 +        assertTrue(rs.indexOf("Write locks = 0") >= 0);
1391 +        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1392 +    }
1393 +
1394 +    /**
1395 +     * readLock.toString indicates current lock state
1396 +     */
1397 +    public void testReadLockToString() {
1398 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1399 +        String us = lock.readLock().toString();
1400 +        assertTrue(us.indexOf("Read locks = 0") >= 0);
1401 +        lock.readLock().lock();
1402 +        lock.readLock().lock();
1403 +        String rs = lock.readLock().toString();
1404 +        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1405 +    }
1406 +
1407 +    /**
1408 +     * writeLock.toString indicates current lock state
1409 +     */
1410 +    public void testWriteLockToString() {
1411 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1412 +        String us = lock.writeLock().toString();
1413 +        assertTrue(us.indexOf("Unlocked") >= 0);
1414 +        lock.writeLock().lock();
1415 +        String ls = lock.writeLock().toString();
1416 +        assertTrue(ls.indexOf("Locked") >= 0);
1417 +    }
1418 +
1419   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines