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.33 by jsr166, Tue Nov 17 14:33:55 2009 UTC vs.
Revision 1.49 by jsr166, Mon May 2 00:07:54 2011 UTC

# Line 1 | Line 1
1   /*
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
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
# Line 9 | Line 9
9   import junit.framework.*;
10   import java.util.concurrent.locks.*;
11   import java.util.concurrent.*;
12 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
13   import java.io.*;
14   import java.util.*;
15  
16   public class ReentrantReadWriteLockTest extends JSR166TestCase {
17      public static void main(String[] args) {
18 <        junit.textui.TestRunner.run (suite());
18 >        junit.textui.TestRunner.run(suite());
19      }
20      public static Test suite() {
21 <        return new TestSuite(ReentrantReadWriteLockTest.class);
21 >        return new TestSuite(ReentrantReadWriteLockTest.class);
22      }
23  
24      /**
# Line 58 | Line 59 | public class ReentrantReadWriteLockTest
59      }
60  
61      /**
62 +     * Releases lock, checking that it had a hold count of 1.
63 +     */
64 +    void releaseLock(ReentrantReadWriteLock.WriteLock lock) {
65 +        assertTrue(lock.isHeldByCurrentThread());
66 +        lock.unlock();
67 +        assertFalse(lock.isHeldByCurrentThread());
68 +    }
69 +
70 +    /**
71       * Constructor sets given fairness, and is in unlocked state
72       */
73      public void testConstructor() {
74 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
74 >        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
75          assertFalse(rl.isFair());
76          assertFalse(rl.isWriteLocked());
77          assertEquals(0, rl.getReadLockCount());
78 <        ReentrantReadWriteLock r2 = new ReentrantReadWriteLock(true);
78 >        ReentrantReadWriteLock r2 = new ReentrantReadWriteLock(true);
79          assertTrue(r2.isFair());
80          assertFalse(r2.isWriteLocked());
81          assertEquals(0, r2.getReadLockCount());
82 <        ReentrantReadWriteLock r3 = new ReentrantReadWriteLock(false);
82 >        ReentrantReadWriteLock r3 = new ReentrantReadWriteLock(false);
83          assertFalse(r3.isFair());
84          assertFalse(r3.isWriteLocked());
85          assertEquals(0, r3.getReadLockCount());
# Line 79 | Line 89 | public class ReentrantReadWriteLockTest
89       * write-locking and read-locking an unlocked lock succeed
90       */
91      public void testLock() {
92 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
92 >        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
93          rl.writeLock().lock();
94          assertTrue(rl.isWriteLocked());
95          assertTrue(rl.isWriteLockedByCurrentThread());
# Line 105 | Line 115 | public class ReentrantReadWriteLockTest
115       * locking an unlocked fair lock succeeds
116       */
117      public void testFairLock() {
118 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true);
118 >        ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true);
119          rl.writeLock().lock();
120          assertTrue(rl.isWriteLocked());
121          assertTrue(rl.isWriteLockedByCurrentThread());
# Line 130 | Line 140 | public class ReentrantReadWriteLockTest
140       * getWriteHoldCount returns number of recursive holds
141       */
142      public void testGetWriteHoldCount() {
143 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
144 <        for (int i = 1; i <= SIZE; i++) {
145 <            lock.writeLock().lock();
146 <            assertEquals(i,lock.getWriteHoldCount());
147 <        }
148 <        for (int i = SIZE; i > 0; i--) {
149 <            lock.writeLock().unlock();
150 <            assertEquals(i-1,lock.getWriteHoldCount());
151 <        }
143 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
144 >        for (int i = 1; i <= SIZE; i++) {
145 >            lock.writeLock().lock();
146 >            assertEquals(i,lock.getWriteHoldCount());
147 >        }
148 >        for (int i = SIZE; i > 0; i--) {
149 >            lock.writeLock().unlock();
150 >            assertEquals(i-1,lock.getWriteHoldCount());
151 >        }
152      }
153  
154      /**
155       * WriteLock.getHoldCount returns number of recursive holds
156       */
157      public void testGetHoldCount() {
158 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
159 <        for (int i = 1; i <= SIZE; i++) {
160 <            lock.writeLock().lock();
161 <            assertEquals(i,lock.writeLock().getHoldCount());
162 <        }
163 <        for (int i = SIZE; i > 0; i--) {
164 <            lock.writeLock().unlock();
165 <            assertEquals(i-1,lock.writeLock().getHoldCount());
166 <        }
158 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
159 >        for (int i = 1; i <= SIZE; i++) {
160 >            lock.writeLock().lock();
161 >            assertEquals(i,lock.writeLock().getHoldCount());
162 >        }
163 >        for (int i = SIZE; i > 0; i--) {
164 >            lock.writeLock().unlock();
165 >            assertEquals(i-1,lock.writeLock().getHoldCount());
166 >        }
167      }
168  
169      /**
170       * getReadHoldCount returns number of recursive holds
171       */
172      public void testGetReadHoldCount() {
173 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
174 <        for (int i = 1; i <= SIZE; i++) {
175 <            lock.readLock().lock();
176 <            assertEquals(i,lock.getReadHoldCount());
177 <        }
178 <        for (int i = SIZE; i > 0; i--) {
179 <            lock.readLock().unlock();
180 <            assertEquals(i-1,lock.getReadHoldCount());
181 <        }
173 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
174 >        for (int i = 1; i <= SIZE; i++) {
175 >            lock.readLock().lock();
176 >            assertEquals(i,lock.getReadHoldCount());
177 >        }
178 >        for (int i = SIZE; i > 0; i--) {
179 >            lock.readLock().unlock();
180 >            assertEquals(i-1,lock.getReadHoldCount());
181 >        }
182      }
183  
184  
# Line 176 | Line 186 | public class ReentrantReadWriteLockTest
186       * write-unlocking an unlocked lock throws IllegalMonitorStateException
187       */
188      public void testUnlock_IllegalMonitorStateException() {
189 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
190 <        try {
191 <            rl.writeLock().unlock();
192 <            shouldThrow();
193 <        } catch (IllegalMonitorStateException success) {}
189 >        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
190 >        try {
191 >            rl.writeLock().unlock();
192 >            shouldThrow();
193 >        } catch (IllegalMonitorStateException success) {}
194      }
195  
196  
# Line 188 | Line 198 | public class ReentrantReadWriteLockTest
198       * write-lockInterruptibly is interruptible
199       */
200      public void testWriteLockInterruptibly_Interrupted() throws Exception {
201 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
202 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
201 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
202 >        lock.writeLock().lock();
203 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
204              public void realRun() throws InterruptedException {
205                  lock.writeLock().lockInterruptibly();
195                lock.writeLock().unlock();
196                lock.writeLock().lockInterruptibly();
197                lock.writeLock().unlock();
206              }});
207  
200        lock.writeLock().lock();
201        t.start();
208          Thread.sleep(SHORT_DELAY_MS);
209          t.interrupt();
204        Thread.sleep(SHORT_DELAY_MS);
205        lock.writeLock().unlock();
210          t.join();
211 +        releaseLock(lock.writeLock());
212      }
213  
214      /**
215       * timed write-tryLock is interruptible
216       */
217      public void testWriteTryLock_Interrupted() throws InterruptedException {
218 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
219 <        lock.writeLock().lock();
220 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
218 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
219 >        lock.writeLock().lock();
220 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
221              public void realRun() throws InterruptedException {
222 <                lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS);
222 >                lock.writeLock().tryLock(SMALL_DELAY_MS, MILLISECONDS);
223              }});
224  
225 <        t.start();
225 >        Thread.sleep(SHORT_DELAY_MS);
226          t.interrupt();
222        lock.writeLock().unlock();
227          t.join();
228 +        releaseLock(lock.writeLock());
229      }
230  
231      /**
232       * read-lockInterruptibly is interruptible
233       */
234      public void testReadLockInterruptibly_Interrupted() throws InterruptedException {
235 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
236 <        lock.writeLock().lock();
237 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
235 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
236 >        lock.writeLock().lock();
237 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
238              public void realRun() throws InterruptedException {
239                  lock.readLock().lockInterruptibly();
240              }});
241  
237        t.start();
242          Thread.sleep(SHORT_DELAY_MS);
243          t.interrupt();
240        Thread.sleep(SHORT_DELAY_MS);
241        lock.writeLock().unlock();
244          t.join();
245 +        releaseLock(lock.writeLock());
246      }
247  
248      /**
249       * timed read-tryLock is interruptible
250       */
251      public void testReadTryLock_Interrupted() throws InterruptedException {
252 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
253 <        lock.writeLock().lock();
254 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
252 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
253 >        lock.writeLock().lock();
254 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
255              public void realRun() throws InterruptedException {
256 <                lock.readLock().tryLock(1000,TimeUnit.MILLISECONDS);
256 >                lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS);
257              }});
258  
259 <        t.start();
259 >        Thread.sleep(SHORT_DELAY_MS);
260          t.interrupt();
261          t.join();
262 +        releaseLock(lock.writeLock());
263      }
264  
265  
# Line 263 | Line 267 | public class ReentrantReadWriteLockTest
267       * write-tryLock fails if locked
268       */
269      public void testWriteTryLockWhenLocked() throws InterruptedException {
270 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
271 <        lock.writeLock().lock();
272 <        Thread t = new Thread(new Runnable() {
273 <                public void run() {
274 <                    threadAssertFalse(lock.writeLock().tryLock());
275 <                }
272 <            });
270 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
271 >        lock.writeLock().lock();
272 >        Thread t = newStartedThread(new CheckedRunnable() {
273 >            public void realRun() {
274 >                assertFalse(lock.writeLock().tryLock());
275 >            }});
276  
274        t.start();
277          t.join();
278          lock.writeLock().unlock();
279      }
# Line 280 | Line 282 | public class ReentrantReadWriteLockTest
282       * read-tryLock fails if locked
283       */
284      public void testReadTryLockWhenLocked() throws InterruptedException {
285 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
286 <        lock.writeLock().lock();
287 <        Thread t = new Thread(new Runnable() {
288 <                public void run() {
289 <                    threadAssertFalse(lock.readLock().tryLock());
290 <                }
289 <            });
285 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
286 >        lock.writeLock().lock();
287 >        Thread t = newStartedThread(new CheckedRunnable() {
288 >            public void realRun() {
289 >                assertFalse(lock.readLock().tryLock());
290 >            }});
291  
291        t.start();
292          t.join();
293          lock.writeLock().unlock();
294      }
# Line 297 | Line 297 | public class ReentrantReadWriteLockTest
297       * Multiple threads can hold a read lock when not write-locked
298       */
299      public void testMultipleReadLocks() throws InterruptedException {
300 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
301 <        lock.readLock().lock();
302 <        Thread t = new Thread(new Runnable() {
303 <                public void run() {
304 <                    threadAssertTrue(lock.readLock().tryLock());
305 <                    lock.readLock().unlock();
306 <                }
307 <            });
300 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
301 >        lock.readLock().lock();
302 >        Thread t = newStartedThread(new CheckedRunnable() {
303 >            public void realRun() {
304 >                assertTrue(lock.readLock().tryLock());
305 >                lock.readLock().unlock();
306 >            }});
307  
309        t.start();
308          t.join();
309          lock.readLock().unlock();
310      }
# Line 315 | Line 313 | public class ReentrantReadWriteLockTest
313       * A writelock succeeds after reading threads unlock
314       */
315      public void testWriteAfterMultipleReadLocks() throws InterruptedException {
316 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
317 <        lock.readLock().lock();
318 <        Thread t1 = new Thread(new Runnable() {
319 <                public void run() {
320 <                    lock.readLock().lock();
321 <                    lock.readLock().unlock();
322 <                }
323 <            });
324 <        Thread t2 = new Thread(new Runnable() {
325 <                public void run() {
326 <                    lock.writeLock().lock();
327 <                    lock.writeLock().unlock();
330 <                }
331 <            });
316 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
317 >        lock.readLock().lock();
318 >        Thread t1 = newStartedThread(new CheckedRunnable() {
319 >            public void realRun() {
320 >                lock.readLock().lock();
321 >                lock.readLock().unlock();
322 >            }});
323 >        Thread t2 = newStartedThread(new CheckedRunnable() {
324 >            public void realRun() {
325 >                lock.writeLock().lock();
326 >                lock.writeLock().unlock();
327 >            }});
328  
333        t1.start();
334        t2.start();
329          Thread.sleep(SHORT_DELAY_MS);
330          lock.readLock().unlock();
331          t1.join(MEDIUM_DELAY_MS);
# Line 344 | Line 338 | public class ReentrantReadWriteLockTest
338       * Readlocks succeed after a writing thread unlocks
339       */
340      public void testReadAfterWriteLock() throws InterruptedException {
341 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
342 <        lock.writeLock().lock();
343 <        Thread t1 = new Thread(new Runnable() {
344 <                public void run() {
345 <                    lock.readLock().lock();
346 <                    lock.readLock().unlock();
347 <                }
348 <            });
349 <        Thread t2 = new Thread(new Runnable() {
350 <                public void run() {
351 <                    lock.readLock().lock();
352 <                    lock.readLock().unlock();
359 <                }
360 <            });
341 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
342 >        lock.writeLock().lock();
343 >        Thread t1 = newStartedThread(new CheckedRunnable() {
344 >            public void realRun() {
345 >                lock.readLock().lock();
346 >                lock.readLock().unlock();
347 >            }});
348 >        Thread t2 = newStartedThread(new CheckedRunnable() {
349 >            public void realRun() {
350 >                lock.readLock().lock();
351 >                lock.readLock().unlock();
352 >            }});
353  
362        t1.start();
363        t2.start();
354          Thread.sleep(SHORT_DELAY_MS);
355          lock.writeLock().unlock();
356          t1.join(MEDIUM_DELAY_MS);
# Line 373 | Line 363 | public class ReentrantReadWriteLockTest
363       * Read trylock succeeds if write locked by current thread
364       */
365      public void testReadHoldingWriteLock() {
366 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
367 <        lock.writeLock().lock();
366 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
367 >        lock.writeLock().lock();
368          assertTrue(lock.readLock().tryLock());
369          lock.readLock().unlock();
370          lock.writeLock().unlock();
# Line 385 | Line 375 | public class ReentrantReadWriteLockTest
375       * other threads are waiting for readlock
376       */
377      public void testReadHoldingWriteLock2() throws InterruptedException {
378 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
379 <        lock.writeLock().lock();
380 <        Thread t1 = new Thread(new Runnable() {
381 <                public void run() {
382 <                    lock.readLock().lock();
383 <                    lock.readLock().unlock();
384 <                }
385 <            });
386 <        Thread t2 = new Thread(new Runnable() {
387 <                public void run() {
388 <                    lock.readLock().lock();
389 <                    lock.readLock().unlock();
400 <                }
401 <            });
378 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
379 >        lock.writeLock().lock();
380 >        Thread t1 = newStartedThread(new CheckedRunnable() {
381 >            public void realRun() {
382 >                lock.readLock().lock();
383 >                lock.readLock().unlock();
384 >            }});
385 >        Thread t2 = newStartedThread(new CheckedRunnable() {
386 >            public void realRun() {
387 >                lock.readLock().lock();
388 >                lock.readLock().unlock();
389 >            }});
390  
403        t1.start();
404        t2.start();
391          lock.readLock().lock();
392          lock.readLock().unlock();
393          Thread.sleep(SHORT_DELAY_MS);
# Line 415 | Line 401 | public class ReentrantReadWriteLockTest
401      }
402  
403      /**
404 <     *  Read lock succeeds if write locked by current thread even if
404 >     * Read lock succeeds if write locked by current thread even if
405       * other threads are waiting for writelock
406       */
407      public void testReadHoldingWriteLock3() throws InterruptedException {
408 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
409 <        lock.writeLock().lock();
410 <        Thread t1 = new Thread(new Runnable() {
411 <                public void run() {
412 <                    lock.writeLock().lock();
413 <                    lock.writeLock().unlock();
414 <                }
415 <            });
416 <        Thread t2 = new Thread(new Runnable() {
417 <                public void run() {
418 <                    lock.writeLock().lock();
419 <                    lock.writeLock().unlock();
434 <                }
435 <            });
408 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
409 >        lock.writeLock().lock();
410 >        Thread t1 = newStartedThread(new CheckedRunnable() {
411 >            public void realRun() {
412 >                lock.writeLock().lock();
413 >                lock.writeLock().unlock();
414 >            }});
415 >        Thread t2 = newStartedThread(new CheckedRunnable() {
416 >            public void realRun() {
417 >                lock.writeLock().lock();
418 >                lock.writeLock().unlock();
419 >            }});
420  
437        t1.start();
438        t2.start();
421          lock.readLock().lock();
422          lock.readLock().unlock();
423          Thread.sleep(SHORT_DELAY_MS);
# Line 450 | Line 432 | public class ReentrantReadWriteLockTest
432  
433  
434      /**
435 <     *  Write lock succeeds if write locked by current thread even if
435 >     * Write lock succeeds if write locked by current thread even if
436       * other threads are waiting for writelock
437       */
438      public void testWriteHoldingWriteLock4() throws InterruptedException {
439 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
440 <        lock.writeLock().lock();
441 <        Thread t1 = new Thread(new Runnable() {
442 <                public void run() {
443 <                    lock.writeLock().lock();
444 <                    lock.writeLock().unlock();
445 <                }
446 <            });
447 <        Thread t2 = new Thread(new Runnable() {
448 <                public void run() {
449 <                    lock.writeLock().lock();
450 <                    lock.writeLock().unlock();
469 <                }
470 <            });
439 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
440 >        lock.writeLock().lock();
441 >        Thread t1 = newStartedThread(new CheckedRunnable() {
442 >            public void realRun() {
443 >                lock.writeLock().lock();
444 >                lock.writeLock().unlock();
445 >            }});
446 >        Thread t2 = newStartedThread(new CheckedRunnable() {
447 >            public void realRun() {
448 >                lock.writeLock().lock();
449 >                lock.writeLock().unlock();
450 >            }});
451  
472        t1.start();
473        t2.start();
452          lock.writeLock().lock();
453          lock.writeLock().unlock();
454          Thread.sleep(SHORT_DELAY_MS);
# Line 488 | Line 466 | public class ReentrantReadWriteLockTest
466       * Fair Read trylock succeeds if write locked by current thread
467       */
468      public void testReadHoldingWriteLockFair() {
469 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
470 <        lock.writeLock().lock();
469 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
470 >        lock.writeLock().lock();
471          assertTrue(lock.readLock().tryLock());
472          lock.readLock().unlock();
473          lock.writeLock().unlock();
# Line 500 | Line 478 | public class ReentrantReadWriteLockTest
478       * other threads are waiting for readlock
479       */
480      public void testReadHoldingWriteLockFair2() throws InterruptedException {
481 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
482 <        lock.writeLock().lock();
483 <        Thread t1 = new Thread(new Runnable() {
484 <                public void run() {
485 <                    lock.readLock().lock();
486 <                    lock.readLock().unlock();
487 <                }
488 <            });
489 <        Thread t2 = new Thread(new Runnable() {
490 <                public void run() {
491 <                    lock.readLock().lock();
492 <                    lock.readLock().unlock();
515 <                }
516 <            });
481 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
482 >        lock.writeLock().lock();
483 >        Thread t1 = newStartedThread(new CheckedRunnable() {
484 >            public void realRun() {
485 >                lock.readLock().lock();
486 >                lock.readLock().unlock();
487 >            }});
488 >        Thread t2 = newStartedThread(new CheckedRunnable() {
489 >            public void realRun() {
490 >                lock.readLock().lock();
491 >                lock.readLock().unlock();
492 >            }});
493  
518        t1.start();
519        t2.start();
494          lock.readLock().lock();
495          lock.readLock().unlock();
496          Thread.sleep(SHORT_DELAY_MS);
# Line 535 | Line 509 | public class ReentrantReadWriteLockTest
509       * other threads are waiting for writelock
510       */
511      public void testReadHoldingWriteLockFair3() throws InterruptedException {
512 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
513 <        lock.writeLock().lock();
514 <        Thread t1 = new Thread(new Runnable() {
515 <                public void run() {
516 <                    lock.writeLock().lock();
517 <                    lock.writeLock().unlock();
518 <                }
519 <            });
520 <        Thread t2 = new Thread(new Runnable() {
521 <                public void run() {
522 <                    lock.writeLock().lock();
523 <                    lock.writeLock().unlock();
550 <                }
551 <            });
512 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
513 >        lock.writeLock().lock();
514 >        Thread t1 = newStartedThread(new CheckedRunnable() {
515 >            public void realRun() {
516 >                lock.writeLock().lock();
517 >                lock.writeLock().unlock();
518 >            }});
519 >        Thread t2 = newStartedThread(new CheckedRunnable() {
520 >            public void realRun() {
521 >                lock.writeLock().lock();
522 >                lock.writeLock().unlock();
523 >            }});
524  
553        t1.start();
554        t2.start();
525          lock.readLock().lock();
526          lock.readLock().unlock();
527          Thread.sleep(SHORT_DELAY_MS);
# Line 570 | Line 540 | public class ReentrantReadWriteLockTest
540       * other threads are waiting for writelock
541       */
542      public void testWriteHoldingWriteLockFair4() throws InterruptedException {
543 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
544 <        lock.writeLock().lock();
545 <        Thread t1 = new Thread(new Runnable() {
546 <                public void run() {
547 <                    lock.writeLock().lock();
548 <                    lock.writeLock().unlock();
549 <                }
550 <            });
551 <        Thread t2 = new Thread(new Runnable() {
552 <                public void run() {
553 <                    lock.writeLock().lock();
554 <                    lock.writeLock().unlock();
585 <                }
586 <            });
543 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
544 >        lock.writeLock().lock();
545 >        Thread t1 = newStartedThread(new CheckedRunnable() {
546 >            public void realRun() {
547 >                lock.writeLock().lock();
548 >                lock.writeLock().unlock();
549 >            }});
550 >        Thread t2 = newStartedThread(new CheckedRunnable() {
551 >            public void realRun() {
552 >                lock.writeLock().lock();
553 >                lock.writeLock().unlock();
554 >            }});
555  
588        t1.start();
589        t2.start();
556          Thread.sleep(SHORT_DELAY_MS);
557          assertTrue(lock.isWriteLockedByCurrentThread());
558 <        assertTrue(lock.getWriteHoldCount() == 1);
558 >        assertEquals(1, lock.getWriteHoldCount());
559          lock.writeLock().lock();
560 <        assertTrue(lock.getWriteHoldCount() == 2);
560 >        assertEquals(2, lock.getWriteHoldCount());
561          lock.writeLock().unlock();
562          lock.writeLock().lock();
563          lock.writeLock().unlock();
# Line 607 | Line 573 | public class ReentrantReadWriteLockTest
573       * Read tryLock succeeds if readlocked but not writelocked
574       */
575      public void testTryLockWhenReadLocked() throws InterruptedException {
576 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
577 <        lock.readLock().lock();
578 <        Thread t = new Thread(new Runnable() {
579 <                public void run() {
580 <                    threadAssertTrue(lock.readLock().tryLock());
581 <                    lock.readLock().unlock();
582 <                }
617 <            });
576 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
577 >        lock.readLock().lock();
578 >        Thread t = newStartedThread(new CheckedRunnable() {
579 >            public void realRun() {
580 >                assertTrue(lock.readLock().tryLock());
581 >                lock.readLock().unlock();
582 >            }});
583  
619        t.start();
584          t.join();
585          lock.readLock().unlock();
586      }
587  
624
625
588      /**
589       * write tryLock fails when readlocked
590       */
591      public void testWriteTryLockWhenReadLocked() throws InterruptedException {
592 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
593 <        lock.readLock().lock();
594 <        Thread t = new Thread(new Runnable() {
595 <                public void run() {
596 <                    threadAssertFalse(lock.writeLock().tryLock());
597 <                }
636 <            });
592 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
593 >        lock.readLock().lock();
594 >        Thread t = newStartedThread(new CheckedRunnable() {
595 >            public void realRun() {
596 >                assertFalse(lock.writeLock().tryLock());
597 >            }});
598  
638        t.start();
599          t.join();
600          lock.readLock().unlock();
601      }
# Line 645 | Line 605 | public class ReentrantReadWriteLockTest
605       * Fair Read tryLock succeeds if readlocked but not writelocked
606       */
607      public void testTryLockWhenReadLockedFair() throws InterruptedException {
608 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
609 <        lock.readLock().lock();
610 <        Thread t = new Thread(new Runnable() {
611 <                public void run() {
612 <                    threadAssertTrue(lock.readLock().tryLock());
613 <                    lock.readLock().unlock();
614 <                }
655 <            });
608 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
609 >        lock.readLock().lock();
610 >        Thread t = newStartedThread(new CheckedRunnable() {
611 >            public void realRun() {
612 >                assertTrue(lock.readLock().tryLock());
613 >                lock.readLock().unlock();
614 >            }});
615  
657        t.start();
616          t.join();
617          lock.readLock().unlock();
618      }
# Line 665 | Line 623 | public class ReentrantReadWriteLockTest
623       * Fair write tryLock fails when readlocked
624       */
625      public void testWriteTryLockWhenReadLockedFair() throws InterruptedException {
626 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
627 <        lock.readLock().lock();
628 <        Thread t = new Thread(new Runnable() {
629 <                public void run() {
630 <                    threadAssertFalse(lock.writeLock().tryLock());
631 <                }
674 <            });
626 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
627 >        lock.readLock().lock();
628 >        Thread t = newStartedThread(new CheckedRunnable() {
629 >            public void realRun() {
630 >                assertFalse(lock.writeLock().tryLock());
631 >            }});
632  
676        t.start();
633          t.join();
634          lock.readLock().unlock();
635      }
# Line 684 | Line 640 | public class ReentrantReadWriteLockTest
640       * write timed tryLock times out if locked
641       */
642      public void testWriteTryLock_Timeout() throws InterruptedException {
643 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
644 <        lock.writeLock().lock();
645 <        Thread t = new Thread(new CheckedRunnable() {
643 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
644 >        lock.writeLock().lock();
645 >        Thread t = newStartedThread(new CheckedRunnable() {
646              public void realRun() throws InterruptedException {
647 <                threadAssertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS));
647 >                assertFalse(lock.writeLock().tryLock(1, MILLISECONDS));
648              }});
649  
694        t.start();
650          t.join();
651 +        assertTrue(lock.writeLock().isHeldByCurrentThread());
652          lock.writeLock().unlock();
653      }
654  
# Line 700 | Line 656 | public class ReentrantReadWriteLockTest
656       * read timed tryLock times out if write-locked
657       */
658      public void testReadTryLock_Timeout() throws InterruptedException {
659 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
660 <        lock.writeLock().lock();
661 <        Thread t = new Thread(new CheckedRunnable() {
659 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
660 >        lock.writeLock().lock();
661 >        Thread t = newStartedThread(new CheckedRunnable() {
662              public void realRun() throws InterruptedException {
663 <                threadAssertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS));
663 >                assertFalse(lock.readLock().tryLock(1, MILLISECONDS));
664              }});
665  
710        t.start();
666          t.join();
667 +        assertTrue(lock.writeLock().isHeldByCurrentThread());
668          lock.writeLock().unlock();
669      }
670  
# Line 717 | Line 673 | public class ReentrantReadWriteLockTest
673       * write lockInterruptibly succeeds if lock free else is interruptible
674       */
675      public void testWriteLockInterruptibly() throws InterruptedException {
676 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
676 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
677          lock.writeLock().lockInterruptibly();
678 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
678 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
679              public void realRun() throws InterruptedException {
680                  lock.writeLock().lockInterruptibly();
681              }});
682  
727        t.start();
683          Thread.sleep(SHORT_DELAY_MS);
684          t.interrupt();
685          Thread.sleep(SHORT_DELAY_MS);
# Line 733 | Line 688 | public class ReentrantReadWriteLockTest
688      }
689  
690      /**
691 <     *  read lockInterruptibly succeeds if lock free else is interruptible
691 >     * read lockInterruptibly succeeds if lock free else is interruptible
692       */
693      public void testReadLockInterruptibly() throws InterruptedException {
694 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
694 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
695          lock.writeLock().lockInterruptibly();
696 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
696 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
697              public void realRun() throws InterruptedException {
698                  lock.readLock().lockInterruptibly();
699              }});
700  
746        t.start();
701          Thread.sleep(SHORT_DELAY_MS);
702          t.interrupt();
703          t.join();
# Line 754 | Line 708 | public class ReentrantReadWriteLockTest
708       * Calling await without holding lock throws IllegalMonitorStateException
709       */
710      public void testAwait_IllegalMonitor() throws InterruptedException {
711 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
711 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
712          final Condition c = lock.writeLock().newCondition();
713          try {
714              c.await();
# Line 766 | Line 720 | public class ReentrantReadWriteLockTest
720       * Calling signal without holding lock throws IllegalMonitorStateException
721       */
722      public void testSignal_IllegalMonitor() {
723 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
723 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
724          final Condition c = lock.writeLock().newCondition();
725          try {
726              c.signal();
# Line 778 | Line 732 | public class ReentrantReadWriteLockTest
732       * awaitNanos without a signal times out
733       */
734      public void testAwaitNanos_Timeout() throws InterruptedException {
735 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
735 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
736          final Condition c = lock.writeLock().newCondition();
737  
738          lock.writeLock().lock();
# Line 789 | Line 743 | public class ReentrantReadWriteLockTest
743  
744  
745      /**
746 <     *  timed await without a signal times out
746 >     * timed await without a signal times out
747       */
748 <    public void testAwait_Timeout() {
749 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
748 >    public void testAwait_Timeout() throws InterruptedException {
749 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
750          final Condition c = lock.writeLock().newCondition();
751          lock.writeLock().lock();
752 +        assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS));
753          lock.writeLock().unlock();
754      }
755  
756      /**
757       * awaitUntil without a signal times out
758       */
759 <    public void testAwaitUntil_Timeout() {
760 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
759 >    public void testAwaitUntil_Timeout() throws InterruptedException {
760 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
761          final Condition c = lock.writeLock().newCondition();
762          lock.writeLock().lock();
763          java.util.Date d = new java.util.Date();
764 +        assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
765          lock.writeLock().unlock();
766      }
767  
# Line 813 | Line 769 | public class ReentrantReadWriteLockTest
769       * await returns when signalled
770       */
771      public void testAwait() throws InterruptedException {
772 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
772 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
773          final Condition c = lock.writeLock().newCondition();
774 <        Thread t = new Thread(new CheckedRunnable() {
774 >        Thread t = newStartedThread(new CheckedRunnable() {
775              public void realRun() throws InterruptedException {
776                  lock.writeLock().lock();
777                  c.await();
778                  lock.writeLock().unlock();
779              }});
780  
825        t.start();
781          Thread.sleep(SHORT_DELAY_MS);
782          lock.writeLock().lock();
783          c.signal();
# Line 890 | Line 845 | public class ReentrantReadWriteLockTest
845       * await is interruptible
846       */
847      public void testAwait_Interrupt() throws InterruptedException {
848 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
848 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
849          final Condition c = lock.writeLock().newCondition();
850 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
850 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
851              public void realRun() throws InterruptedException {
852                  lock.writeLock().lock();
853                  c.await();
854                  lock.writeLock().unlock();
855              }});
856  
902        t.start();
857          Thread.sleep(SHORT_DELAY_MS);
858          t.interrupt();
859          t.join(SHORT_DELAY_MS);
# Line 910 | Line 864 | public class ReentrantReadWriteLockTest
864       * awaitNanos is interruptible
865       */
866      public void testAwaitNanos_Interrupt() throws InterruptedException {
867 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
867 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
868          final Condition c = lock.writeLock().newCondition();
869 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
869 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
870              public void realRun() throws InterruptedException {
871                  lock.writeLock().lock();
872 <                c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
872 >                c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
873                  lock.writeLock().unlock();
874              }});
875  
922        t.start();
876          Thread.sleep(SHORT_DELAY_MS);
877          t.interrupt();
878          t.join(SHORT_DELAY_MS);
# Line 930 | Line 883 | public class ReentrantReadWriteLockTest
883       * awaitUntil is interruptible
884       */
885      public void testAwaitUntil_Interrupt() throws InterruptedException {
886 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
886 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
887          final Condition c = lock.writeLock().newCondition();
888 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
888 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
889              public void realRun() throws InterruptedException {
890                  lock.writeLock().lock();
891                  java.util.Date d = new java.util.Date();
# Line 940 | Line 893 | public class ReentrantReadWriteLockTest
893                  lock.writeLock().unlock();
894              }});
895  
943        t.start();
896          Thread.sleep(SHORT_DELAY_MS);
897          t.interrupt();
898          t.join(SHORT_DELAY_MS);
# Line 951 | Line 903 | public class ReentrantReadWriteLockTest
903       * signalAll wakes up all threads
904       */
905      public void testSignalAll() throws InterruptedException {
906 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
906 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
907          final Condition c = lock.writeLock().newCondition();
908 <        Thread t1 = new Thread(new CheckedRunnable() {
908 >        Thread t1 = newStartedThread(new CheckedRunnable() {
909              public void realRun() throws InterruptedException {
910                  lock.writeLock().lock();
911                  c.await();
912                  lock.writeLock().unlock();
913              }});
914  
915 <        Thread t2 = new Thread(new CheckedRunnable() {
915 >        Thread t2 = newStartedThread(new CheckedRunnable() {
916              public void realRun() throws InterruptedException {
917                  lock.writeLock().lock();
918                  c.await();
919                  lock.writeLock().unlock();
920              }});
921  
970        t1.start();
971        t2.start();
922          Thread.sleep(SHORT_DELAY_MS);
923          lock.writeLock().lock();
924          c.signalAll();
# Line 1003 | Line 953 | public class ReentrantReadWriteLockTest
953       * hasQueuedThreads reports whether there are waiting threads
954       */
955      public void testhasQueuedThreads() throws InterruptedException {
956 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
956 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
957          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
958          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
959          assertFalse(lock.hasQueuedThreads());
# Line 1028 | Line 978 | public class ReentrantReadWriteLockTest
978       * hasQueuedThread(null) throws NPE
979       */
980      public void testHasQueuedThreadNPE() {
981 <        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
981 >        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
982          try {
983              sync.hasQueuedThread(null);
984              shouldThrow();
# Line 1039 | Line 989 | public class ReentrantReadWriteLockTest
989       * hasQueuedThread reports whether a thread is queued.
990       */
991      public void testHasQueuedThread() throws InterruptedException {
992 <        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
992 >        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
993          Thread t1 = new Thread(new InterruptedLockRunnable(sync));
994          Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
995          assertFalse(sync.hasQueuedThread(t1));
# Line 1070 | Line 1020 | public class ReentrantReadWriteLockTest
1020       * getQueueLength reports number of waiting threads
1021       */
1022      public void testGetQueueLength() throws InterruptedException {
1023 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1023 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1024          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1025          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1026          assertEquals(0, lock.getQueueLength());
# Line 1095 | Line 1045 | public class ReentrantReadWriteLockTest
1045       * getQueuedThreads includes waiting threads
1046       */
1047      public void testGetQueuedThreads() throws InterruptedException {
1048 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1048 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1049          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1050          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1051          assertTrue(lock.getQueuedThreads().isEmpty());
# Line 1123 | Line 1073 | public class ReentrantReadWriteLockTest
1073       * hasWaiters throws NPE if null
1074       */
1075      public void testHasWaitersNPE() {
1076 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1076 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1077          try {
1078              lock.hasWaiters(null);
1079              shouldThrow();
# Line 1134 | Line 1084 | public class ReentrantReadWriteLockTest
1084       * getWaitQueueLength throws NPE if null
1085       */
1086      public void testGetWaitQueueLengthNPE() {
1087 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1087 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1088          try {
1089              lock.getWaitQueueLength(null);
1090              shouldThrow();
# Line 1146 | Line 1096 | public class ReentrantReadWriteLockTest
1096       * getWaitingThreads throws NPE if null
1097       */
1098      public void testGetWaitingThreadsNPE() {
1099 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1099 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1100          try {
1101              lock.getWaitingThreads(null);
1102              shouldThrow();
# Line 1157 | Line 1107 | public class ReentrantReadWriteLockTest
1107       * hasWaiters throws IAE if not owned
1108       */
1109      public void testHasWaitersIAE() {
1110 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1110 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1111          final Condition c = lock.writeLock().newCondition();
1112 <        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1112 >        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1113          try {
1114              lock2.hasWaiters(c);
1115              shouldThrow();
# Line 1170 | Line 1120 | public class ReentrantReadWriteLockTest
1120       * hasWaiters throws IMSE if not locked
1121       */
1122      public void testHasWaitersIMSE() {
1123 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1123 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1124          final Condition c = lock.writeLock().newCondition();
1125          try {
1126              lock.hasWaiters(c);
# Line 1183 | Line 1133 | public class ReentrantReadWriteLockTest
1133       * getWaitQueueLength throws IAE if not owned
1134       */
1135      public void testGetWaitQueueLengthIAE() {
1136 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1136 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1137          final Condition c = lock.writeLock().newCondition();
1138 <        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1138 >        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1139          try {
1140              lock2.getWaitQueueLength(c);
1141              shouldThrow();
# Line 1196 | Line 1146 | public class ReentrantReadWriteLockTest
1146       * getWaitQueueLength throws IMSE if not locked
1147       */
1148      public void testGetWaitQueueLengthIMSE() {
1149 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1149 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1150          final Condition c = lock.writeLock().newCondition();
1151          try {
1152              lock.getWaitQueueLength(c);
# Line 1209 | Line 1159 | public class ReentrantReadWriteLockTest
1159       * getWaitingThreads throws IAE if not owned
1160       */
1161      public void testGetWaitingThreadsIAE() {
1162 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1162 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1163          final Condition c = lock.writeLock().newCondition();
1164 <        final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock();
1164 >        final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock();
1165          try {
1166              lock2.getWaitingThreads(c);
1167              shouldThrow();
# Line 1222 | Line 1172 | public class ReentrantReadWriteLockTest
1172       * getWaitingThreads throws IMSE if not locked
1173       */
1174      public void testGetWaitingThreadsIMSE() {
1175 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1175 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1176          final Condition c = lock.writeLock().newCondition();
1177          try {
1178              lock.getWaitingThreads(c);
# Line 1235 | Line 1185 | public class ReentrantReadWriteLockTest
1185       * hasWaiters returns true when a thread is waiting, else false
1186       */
1187      public void testHasWaiters() throws InterruptedException {
1188 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1188 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1189          final Condition c = lock.writeLock().newCondition();
1190 <        Thread t = new Thread(new CheckedRunnable() {
1190 >        Thread t = newStartedThread(new CheckedRunnable() {
1191              public void realRun() throws InterruptedException {
1192                  lock.writeLock().lock();
1193 <                threadAssertFalse(lock.hasWaiters(c));
1194 <                threadAssertEquals(0, lock.getWaitQueueLength(c));
1193 >                assertFalse(lock.hasWaiters(c));
1194 >                assertEquals(0, lock.getWaitQueueLength(c));
1195                  c.await();
1196                  lock.writeLock().unlock();
1197              }});
1198  
1249        t.start();
1199          Thread.sleep(SHORT_DELAY_MS);
1200          lock.writeLock().lock();
1201          assertTrue(lock.hasWaiters(c));
# Line 1266 | Line 1215 | public class ReentrantReadWriteLockTest
1215       * getWaitQueueLength returns number of waiting threads
1216       */
1217      public void testGetWaitQueueLength() throws InterruptedException {
1218 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1218 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1219          final Condition c = lock.writeLock().newCondition();
1220 <        Thread t = new Thread(new CheckedRunnable() {
1220 >        Thread t = newStartedThread(new CheckedRunnable() {
1221              public void realRun() throws InterruptedException {
1222                  lock.writeLock().lock();
1223 <                threadAssertFalse(lock.hasWaiters(c));
1224 <                threadAssertEquals(0, lock.getWaitQueueLength(c));
1223 >                assertFalse(lock.hasWaiters(c));
1224 >                assertEquals(0, lock.getWaitQueueLength(c));
1225                  c.await();
1226                  lock.writeLock().unlock();
1227              }});
1228  
1280        t.start();
1229          Thread.sleep(SHORT_DELAY_MS);
1230          lock.writeLock().lock();
1231          assertTrue(lock.hasWaiters(c));
# Line 1298 | Line 1246 | public class ReentrantReadWriteLockTest
1246       * getWaitingThreads returns only and all waiting threads
1247       */
1248      public void testGetWaitingThreads() throws InterruptedException {
1249 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1249 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1250          final Condition c = lock.writeLock().newCondition();
1251 <        Thread t1 = new Thread(new CheckedRunnable() {
1251 >        Thread t1 = new Thread(new CheckedRunnable() {
1252              public void realRun() throws InterruptedException {
1253                  lock.writeLock().lock();
1254 <                threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
1254 >                assertTrue(lock.getWaitingThreads(c).isEmpty());
1255                  c.await();
1256                  lock.writeLock().unlock();
1257              }});
1258  
1259 <        Thread t2 = new Thread(new CheckedRunnable() {
1259 >        Thread t2 = new Thread(new CheckedRunnable() {
1260              public void realRun() throws InterruptedException {
1261                  lock.writeLock().lock();
1262 <                threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
1262 >                assertFalse(lock.getWaitingThreads(c).isEmpty());
1263                  c.await();
1264                  lock.writeLock().unlock();
1265              }});

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines