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.32 by jsr166, Tue Nov 17 14:18:28 2009 UTC vs.
Revision 1.48 by jsr166, Mon May 2 00:06:45 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();
192 <        Thread t = new Thread(new Runnable() {
193 <                public void run() {
194 <                    try {
195 <                        lock.writeLock().lockInterruptibly();
196 <                        lock.writeLock().unlock();
197 <                        lock.writeLock().lockInterruptibly();
198 <                        lock.writeLock().unlock();
199 <                    } catch (InterruptedException success) {}
200 <                }
201 <            });
202 <
201 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
202          lock.writeLock().lock();
203 <        t.start();
203 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
204 >            public void realRun() throws InterruptedException {
205 >                lock.writeLock().lockInterruptibly();
206 >            }});
207 >
208          Thread.sleep(SHORT_DELAY_MS);
209          t.interrupt();
207        Thread.sleep(SHORT_DELAY_MS);
208        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 Runnable() {
221 <                public void run() {
222 <                    try {
223 <                        lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS);
222 <                    } catch (InterruptedException success) {}
223 <                }
224 <            });
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(SMALL_DELAY_MS, MILLISECONDS);
223 >            }});
224  
225 <        t.start();
225 >        Thread.sleep(SHORT_DELAY_MS);
226          t.interrupt();
228        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 Runnable() {
238 <                public void run() {
239 <                    try {
240 <                        lock.readLock().lockInterruptibly();
242 <                    } catch (InterruptedException success) {}
243 <                }
244 <            });
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  
246        t.start();
242          Thread.sleep(SHORT_DELAY_MS);
243          t.interrupt();
249        Thread.sleep(SHORT_DELAY_MS);
250        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 Runnable() {
255 <                public void run() {
256 <                    try {
257 <                        lock.readLock().tryLock(1000,TimeUnit.MILLISECONDS);
264 <                        threadShouldThrow();
265 <                    } catch (InterruptedException success) {}
266 <                }
267 <            });
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(LONG_DELAY_MS, MILLISECONDS);
257 >            }});
258  
259 <        t.start();
259 >        Thread.sleep(SHORT_DELAY_MS);
260          t.interrupt();
261          t.join();
262      }
# Line 276 | Line 266 | public class ReentrantReadWriteLockTest
266       * write-tryLock fails if locked
267       */
268      public void testWriteTryLockWhenLocked() throws InterruptedException {
269 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
270 <        lock.writeLock().lock();
271 <        Thread t = new Thread(new Runnable() {
272 <                public void run() {
273 <                    threadAssertFalse(lock.writeLock().tryLock());
274 <                }
285 <            });
269 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
270 >        lock.writeLock().lock();
271 >        Thread t = newStartedThread(new CheckedRunnable() {
272 >            public void realRun() {
273 >                assertFalse(lock.writeLock().tryLock());
274 >            }});
275  
287        t.start();
276          t.join();
277          lock.writeLock().unlock();
278      }
# Line 293 | Line 281 | public class ReentrantReadWriteLockTest
281       * read-tryLock fails if locked
282       */
283      public void testReadTryLockWhenLocked() throws InterruptedException {
284 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
285 <        lock.writeLock().lock();
286 <        Thread t = new Thread(new Runnable() {
287 <                public void run() {
288 <                    threadAssertFalse(lock.readLock().tryLock());
289 <                }
302 <            });
284 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
285 >        lock.writeLock().lock();
286 >        Thread t = newStartedThread(new CheckedRunnable() {
287 >            public void realRun() {
288 >                assertFalse(lock.readLock().tryLock());
289 >            }});
290  
304        t.start();
291          t.join();
292          lock.writeLock().unlock();
293      }
# Line 310 | Line 296 | public class ReentrantReadWriteLockTest
296       * Multiple threads can hold a read lock when not write-locked
297       */
298      public void testMultipleReadLocks() throws InterruptedException {
299 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
300 <        lock.readLock().lock();
301 <        Thread t = new Thread(new Runnable() {
302 <                public void run() {
303 <                    threadAssertTrue(lock.readLock().tryLock());
304 <                    lock.readLock().unlock();
305 <                }
320 <            });
299 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
300 >        lock.readLock().lock();
301 >        Thread t = newStartedThread(new CheckedRunnable() {
302 >            public void realRun() {
303 >                assertTrue(lock.readLock().tryLock());
304 >                lock.readLock().unlock();
305 >            }});
306  
322        t.start();
307          t.join();
308          lock.readLock().unlock();
309      }
# Line 328 | Line 312 | public class ReentrantReadWriteLockTest
312       * A writelock succeeds after reading threads unlock
313       */
314      public void testWriteAfterMultipleReadLocks() throws InterruptedException {
315 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
316 <        lock.readLock().lock();
317 <        Thread t1 = new Thread(new Runnable() {
318 <                public void run() {
319 <                    lock.readLock().lock();
320 <                    lock.readLock().unlock();
321 <                }
322 <            });
323 <        Thread t2 = new Thread(new Runnable() {
324 <                public void run() {
325 <                    lock.writeLock().lock();
326 <                    lock.writeLock().unlock();
343 <                }
344 <            });
315 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
316 >        lock.readLock().lock();
317 >        Thread t1 = newStartedThread(new CheckedRunnable() {
318 >            public void realRun() {
319 >                lock.readLock().lock();
320 >                lock.readLock().unlock();
321 >            }});
322 >        Thread t2 = newStartedThread(new CheckedRunnable() {
323 >            public void realRun() {
324 >                lock.writeLock().lock();
325 >                lock.writeLock().unlock();
326 >            }});
327  
346        t1.start();
347        t2.start();
328          Thread.sleep(SHORT_DELAY_MS);
329          lock.readLock().unlock();
330          t1.join(MEDIUM_DELAY_MS);
# Line 357 | Line 337 | public class ReentrantReadWriteLockTest
337       * Readlocks succeed after a writing thread unlocks
338       */
339      public void testReadAfterWriteLock() throws InterruptedException {
340 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
341 <        lock.writeLock().lock();
342 <        Thread t1 = new Thread(new Runnable() {
343 <                public void run() {
344 <                    lock.readLock().lock();
345 <                    lock.readLock().unlock();
346 <                }
347 <            });
348 <        Thread t2 = new Thread(new Runnable() {
349 <                public void run() {
350 <                    lock.readLock().lock();
351 <                    lock.readLock().unlock();
372 <                }
373 <            });
340 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
341 >        lock.writeLock().lock();
342 >        Thread t1 = newStartedThread(new CheckedRunnable() {
343 >            public void realRun() {
344 >                lock.readLock().lock();
345 >                lock.readLock().unlock();
346 >            }});
347 >        Thread t2 = newStartedThread(new CheckedRunnable() {
348 >            public void realRun() {
349 >                lock.readLock().lock();
350 >                lock.readLock().unlock();
351 >            }});
352  
375        t1.start();
376        t2.start();
353          Thread.sleep(SHORT_DELAY_MS);
354          lock.writeLock().unlock();
355          t1.join(MEDIUM_DELAY_MS);
# Line 386 | Line 362 | public class ReentrantReadWriteLockTest
362       * Read trylock succeeds if write locked by current thread
363       */
364      public void testReadHoldingWriteLock() {
365 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
366 <        lock.writeLock().lock();
365 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
366 >        lock.writeLock().lock();
367          assertTrue(lock.readLock().tryLock());
368          lock.readLock().unlock();
369          lock.writeLock().unlock();
# Line 398 | Line 374 | public class ReentrantReadWriteLockTest
374       * other threads are waiting for readlock
375       */
376      public void testReadHoldingWriteLock2() throws InterruptedException {
377 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
378 <        lock.writeLock().lock();
379 <        Thread t1 = new Thread(new Runnable() {
380 <                public void run() {
381 <                    lock.readLock().lock();
382 <                    lock.readLock().unlock();
383 <                }
384 <            });
385 <        Thread t2 = new Thread(new Runnable() {
386 <                public void run() {
387 <                    lock.readLock().lock();
388 <                    lock.readLock().unlock();
413 <                }
414 <            });
377 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
378 >        lock.writeLock().lock();
379 >        Thread t1 = newStartedThread(new CheckedRunnable() {
380 >            public void realRun() {
381 >                lock.readLock().lock();
382 >                lock.readLock().unlock();
383 >            }});
384 >        Thread t2 = newStartedThread(new CheckedRunnable() {
385 >            public void realRun() {
386 >                lock.readLock().lock();
387 >                lock.readLock().unlock();
388 >            }});
389  
416        t1.start();
417        t2.start();
390          lock.readLock().lock();
391          lock.readLock().unlock();
392          Thread.sleep(SHORT_DELAY_MS);
# Line 428 | Line 400 | public class ReentrantReadWriteLockTest
400      }
401  
402      /**
403 <     *  Read lock succeeds if write locked by current thread even if
403 >     * Read lock succeeds if write locked by current thread even if
404       * other threads are waiting for writelock
405       */
406      public void testReadHoldingWriteLock3() throws InterruptedException {
407 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
408 <        lock.writeLock().lock();
409 <        Thread t1 = new Thread(new Runnable() {
410 <                public void run() {
411 <                    lock.writeLock().lock();
412 <                    lock.writeLock().unlock();
413 <                }
414 <            });
415 <        Thread t2 = new Thread(new Runnable() {
416 <                public void run() {
417 <                    lock.writeLock().lock();
418 <                    lock.writeLock().unlock();
447 <                }
448 <            });
407 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
408 >        lock.writeLock().lock();
409 >        Thread t1 = newStartedThread(new CheckedRunnable() {
410 >            public void realRun() {
411 >                lock.writeLock().lock();
412 >                lock.writeLock().unlock();
413 >            }});
414 >        Thread t2 = newStartedThread(new CheckedRunnable() {
415 >            public void realRun() {
416 >                lock.writeLock().lock();
417 >                lock.writeLock().unlock();
418 >            }});
419  
450        t1.start();
451        t2.start();
420          lock.readLock().lock();
421          lock.readLock().unlock();
422          Thread.sleep(SHORT_DELAY_MS);
# Line 463 | Line 431 | public class ReentrantReadWriteLockTest
431  
432  
433      /**
434 <     *  Write lock succeeds if write locked by current thread even if
434 >     * Write lock succeeds if write locked by current thread even if
435       * other threads are waiting for writelock
436       */
437      public void testWriteHoldingWriteLock4() throws InterruptedException {
438 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
439 <        lock.writeLock().lock();
440 <        Thread t1 = new Thread(new Runnable() {
441 <                public void run() {
442 <                    lock.writeLock().lock();
443 <                    lock.writeLock().unlock();
444 <                }
445 <            });
446 <        Thread t2 = new Thread(new Runnable() {
447 <                public void run() {
448 <                    lock.writeLock().lock();
449 <                    lock.writeLock().unlock();
482 <                }
483 <            });
438 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
439 >        lock.writeLock().lock();
440 >        Thread t1 = newStartedThread(new CheckedRunnable() {
441 >            public void realRun() {
442 >                lock.writeLock().lock();
443 >                lock.writeLock().unlock();
444 >            }});
445 >        Thread t2 = newStartedThread(new CheckedRunnable() {
446 >            public void realRun() {
447 >                lock.writeLock().lock();
448 >                lock.writeLock().unlock();
449 >            }});
450  
485        t1.start();
486        t2.start();
451          lock.writeLock().lock();
452          lock.writeLock().unlock();
453          Thread.sleep(SHORT_DELAY_MS);
# Line 501 | Line 465 | public class ReentrantReadWriteLockTest
465       * Fair Read trylock succeeds if write locked by current thread
466       */
467      public void testReadHoldingWriteLockFair() {
468 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
469 <        lock.writeLock().lock();
468 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
469 >        lock.writeLock().lock();
470          assertTrue(lock.readLock().tryLock());
471          lock.readLock().unlock();
472          lock.writeLock().unlock();
# Line 513 | Line 477 | public class ReentrantReadWriteLockTest
477       * other threads are waiting for readlock
478       */
479      public void testReadHoldingWriteLockFair2() throws InterruptedException {
480 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
481 <        lock.writeLock().lock();
482 <        Thread t1 = new Thread(new Runnable() {
483 <                public void run() {
484 <                    lock.readLock().lock();
485 <                    lock.readLock().unlock();
486 <                }
487 <            });
488 <        Thread t2 = new Thread(new Runnable() {
489 <                public void run() {
490 <                    lock.readLock().lock();
491 <                    lock.readLock().unlock();
528 <                }
529 <            });
480 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
481 >        lock.writeLock().lock();
482 >        Thread t1 = newStartedThread(new CheckedRunnable() {
483 >            public void realRun() {
484 >                lock.readLock().lock();
485 >                lock.readLock().unlock();
486 >            }});
487 >        Thread t2 = newStartedThread(new CheckedRunnable() {
488 >            public void realRun() {
489 >                lock.readLock().lock();
490 >                lock.readLock().unlock();
491 >            }});
492  
531        t1.start();
532        t2.start();
493          lock.readLock().lock();
494          lock.readLock().unlock();
495          Thread.sleep(SHORT_DELAY_MS);
# Line 548 | Line 508 | public class ReentrantReadWriteLockTest
508       * other threads are waiting for writelock
509       */
510      public void testReadHoldingWriteLockFair3() throws InterruptedException {
511 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
512 <        lock.writeLock().lock();
513 <        Thread t1 = new Thread(new Runnable() {
514 <                public void run() {
515 <                    lock.writeLock().lock();
516 <                    lock.writeLock().unlock();
517 <                }
518 <            });
519 <        Thread t2 = new Thread(new Runnable() {
520 <                public void run() {
521 <                    lock.writeLock().lock();
522 <                    lock.writeLock().unlock();
563 <                }
564 <            });
511 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
512 >        lock.writeLock().lock();
513 >        Thread t1 = newStartedThread(new CheckedRunnable() {
514 >            public void realRun() {
515 >                lock.writeLock().lock();
516 >                lock.writeLock().unlock();
517 >            }});
518 >        Thread t2 = newStartedThread(new CheckedRunnable() {
519 >            public void realRun() {
520 >                lock.writeLock().lock();
521 >                lock.writeLock().unlock();
522 >            }});
523  
566        t1.start();
567        t2.start();
524          lock.readLock().lock();
525          lock.readLock().unlock();
526          Thread.sleep(SHORT_DELAY_MS);
# Line 583 | Line 539 | public class ReentrantReadWriteLockTest
539       * other threads are waiting for writelock
540       */
541      public void testWriteHoldingWriteLockFair4() throws InterruptedException {
542 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
543 <        lock.writeLock().lock();
544 <        Thread t1 = new Thread(new Runnable() {
545 <                public void run() {
546 <                    lock.writeLock().lock();
547 <                    lock.writeLock().unlock();
548 <                }
549 <            });
550 <        Thread t2 = new Thread(new Runnable() {
551 <                public void run() {
552 <                    lock.writeLock().lock();
553 <                    lock.writeLock().unlock();
598 <                }
599 <            });
542 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
543 >        lock.writeLock().lock();
544 >        Thread t1 = newStartedThread(new CheckedRunnable() {
545 >            public void realRun() {
546 >                lock.writeLock().lock();
547 >                lock.writeLock().unlock();
548 >            }});
549 >        Thread t2 = newStartedThread(new CheckedRunnable() {
550 >            public void realRun() {
551 >                lock.writeLock().lock();
552 >                lock.writeLock().unlock();
553 >            }});
554  
601        t1.start();
602        t2.start();
555          Thread.sleep(SHORT_DELAY_MS);
556          assertTrue(lock.isWriteLockedByCurrentThread());
557 <        assertTrue(lock.getWriteHoldCount() == 1);
557 >        assertEquals(1, lock.getWriteHoldCount());
558          lock.writeLock().lock();
559 <        assertTrue(lock.getWriteHoldCount() == 2);
559 >        assertEquals(2, lock.getWriteHoldCount());
560          lock.writeLock().unlock();
561          lock.writeLock().lock();
562          lock.writeLock().unlock();
# Line 620 | Line 572 | public class ReentrantReadWriteLockTest
572       * Read tryLock succeeds if readlocked but not writelocked
573       */
574      public void testTryLockWhenReadLocked() throws InterruptedException {
575 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
576 <        lock.readLock().lock();
577 <        Thread t = new Thread(new Runnable() {
578 <                public void run() {
579 <                    threadAssertTrue(lock.readLock().tryLock());
580 <                    lock.readLock().unlock();
581 <                }
630 <            });
575 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
576 >        lock.readLock().lock();
577 >        Thread t = newStartedThread(new CheckedRunnable() {
578 >            public void realRun() {
579 >                assertTrue(lock.readLock().tryLock());
580 >                lock.readLock().unlock();
581 >            }});
582  
632        t.start();
583          t.join();
584          lock.readLock().unlock();
585      }
586  
637
638
587      /**
588       * write tryLock fails when readlocked
589       */
590      public void testWriteTryLockWhenReadLocked() throws InterruptedException {
591 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
592 <        lock.readLock().lock();
593 <        Thread t = new Thread(new Runnable() {
594 <                public void run() {
595 <                    threadAssertFalse(lock.writeLock().tryLock());
596 <                }
649 <            });
591 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
592 >        lock.readLock().lock();
593 >        Thread t = newStartedThread(new CheckedRunnable() {
594 >            public void realRun() {
595 >                assertFalse(lock.writeLock().tryLock());
596 >            }});
597  
651        t.start();
598          t.join();
599          lock.readLock().unlock();
600      }
# Line 658 | Line 604 | public class ReentrantReadWriteLockTest
604       * Fair Read tryLock succeeds if readlocked but not writelocked
605       */
606      public void testTryLockWhenReadLockedFair() throws InterruptedException {
607 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
608 <        lock.readLock().lock();
609 <        Thread t = new Thread(new Runnable() {
610 <                public void run() {
611 <                    threadAssertTrue(lock.readLock().tryLock());
612 <                    lock.readLock().unlock();
613 <                }
668 <            });
607 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
608 >        lock.readLock().lock();
609 >        Thread t = newStartedThread(new CheckedRunnable() {
610 >            public void realRun() {
611 >                assertTrue(lock.readLock().tryLock());
612 >                lock.readLock().unlock();
613 >            }});
614  
670        t.start();
615          t.join();
616          lock.readLock().unlock();
617      }
# Line 678 | Line 622 | public class ReentrantReadWriteLockTest
622       * Fair write tryLock fails when readlocked
623       */
624      public void testWriteTryLockWhenReadLockedFair() throws InterruptedException {
625 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
626 <        lock.readLock().lock();
627 <        Thread t = new Thread(new Runnable() {
628 <                public void run() {
629 <                    threadAssertFalse(lock.writeLock().tryLock());
630 <                }
687 <            });
625 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
626 >        lock.readLock().lock();
627 >        Thread t = newStartedThread(new CheckedRunnable() {
628 >            public void realRun() {
629 >                assertFalse(lock.writeLock().tryLock());
630 >            }});
631  
689        t.start();
632          t.join();
633          lock.readLock().unlock();
634      }
# Line 697 | Line 639 | public class ReentrantReadWriteLockTest
639       * write timed tryLock times out if locked
640       */
641      public void testWriteTryLock_Timeout() throws InterruptedException {
642 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
643 <        lock.writeLock().lock();
644 <        Thread t = new Thread(new Runnable() {
645 <                public void run() {
646 <                    try {
647 <                        threadAssertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS));
706 <                    } catch (Exception ex) {
707 <                        threadUnexpectedException();
708 <                    }
709 <                }
710 <            });
642 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
643 >        lock.writeLock().lock();
644 >        Thread t = newStartedThread(new CheckedRunnable() {
645 >            public void realRun() throws InterruptedException {
646 >                assertFalse(lock.writeLock().tryLock(1, MILLISECONDS));
647 >            }});
648  
712        t.start();
649          t.join();
650 +        assertTrue(lock.writeLock().isHeldByCurrentThread());
651          lock.writeLock().unlock();
652      }
653  
# Line 718 | Line 655 | public class ReentrantReadWriteLockTest
655       * read timed tryLock times out if write-locked
656       */
657      public void testReadTryLock_Timeout() throws InterruptedException {
658 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
659 <        lock.writeLock().lock();
660 <        Thread t = new Thread(new Runnable() {
661 <                public void run() {
662 <                    try {
663 <                        threadAssertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS));
727 <                    } catch (Exception ex) {
728 <                        threadUnexpectedException();
729 <                    }
730 <                }
731 <            });
658 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
659 >        lock.writeLock().lock();
660 >        Thread t = newStartedThread(new CheckedRunnable() {
661 >            public void realRun() throws InterruptedException {
662 >                assertFalse(lock.readLock().tryLock(1, MILLISECONDS));
663 >            }});
664  
733        t.start();
665          t.join();
666 +        assertTrue(lock.writeLock().isHeldByCurrentThread());
667          lock.writeLock().unlock();
668      }
669  
# Line 740 | Line 672 | public class ReentrantReadWriteLockTest
672       * write lockInterruptibly succeeds if lock free else is interruptible
673       */
674      public void testWriteLockInterruptibly() throws InterruptedException {
675 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
675 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
676          lock.writeLock().lockInterruptibly();
677 <        Thread t = new Thread(new Runnable() {
678 <                public void run() {
679 <                    try {
680 <                        lock.writeLock().lockInterruptibly();
749 <                        threadShouldThrow();
750 <                    }
751 <                    catch (InterruptedException success) {
752 <                    }
753 <                }
754 <            });
677 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
678 >            public void realRun() throws InterruptedException {
679 >                lock.writeLock().lockInterruptibly();
680 >            }});
681  
756        t.start();
682          Thread.sleep(SHORT_DELAY_MS);
683          t.interrupt();
684          Thread.sleep(SHORT_DELAY_MS);
# Line 762 | Line 687 | public class ReentrantReadWriteLockTest
687      }
688  
689      /**
690 <     *  read lockInterruptibly succeeds if lock free else is interruptible
690 >     * read lockInterruptibly succeeds if lock free else is interruptible
691       */
692      public void testReadLockInterruptibly() throws InterruptedException {
693 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
693 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
694          lock.writeLock().lockInterruptibly();
695 <        Thread t = new Thread(new Runnable() {
696 <                public void run() {
697 <                    try {
698 <                        lock.readLock().lockInterruptibly();
774 <                        threadShouldThrow();
775 <                    }
776 <                    catch (InterruptedException success) {
777 <                    }
778 <                }
779 <            });
695 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
696 >            public void realRun() throws InterruptedException {
697 >                lock.readLock().lockInterruptibly();
698 >            }});
699  
781        t.start();
700          Thread.sleep(SHORT_DELAY_MS);
701          t.interrupt();
702          t.join();
# Line 789 | Line 707 | public class ReentrantReadWriteLockTest
707       * Calling await without holding lock throws IllegalMonitorStateException
708       */
709      public void testAwait_IllegalMonitor() throws InterruptedException {
710 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
710 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
711          final Condition c = lock.writeLock().newCondition();
712          try {
713              c.await();
# Line 801 | Line 719 | public class ReentrantReadWriteLockTest
719       * Calling signal without holding lock throws IllegalMonitorStateException
720       */
721      public void testSignal_IllegalMonitor() {
722 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
722 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
723          final Condition c = lock.writeLock().newCondition();
724          try {
725              c.signal();
# Line 813 | Line 731 | public class ReentrantReadWriteLockTest
731       * awaitNanos without a signal times out
732       */
733      public void testAwaitNanos_Timeout() throws InterruptedException {
734 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
734 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
735          final Condition c = lock.writeLock().newCondition();
736  
737          lock.writeLock().lock();
# Line 824 | Line 742 | public class ReentrantReadWriteLockTest
742  
743  
744      /**
745 <     *  timed await without a signal times out
745 >     * timed await without a signal times out
746       */
747 <    public void testAwait_Timeout() {
748 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
747 >    public void testAwait_Timeout() throws InterruptedException {
748 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
749          final Condition c = lock.writeLock().newCondition();
750          lock.writeLock().lock();
751 +        assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS));
752          lock.writeLock().unlock();
753      }
754  
755      /**
756       * awaitUntil without a signal times out
757       */
758 <    public void testAwaitUntil_Timeout() {
759 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
758 >    public void testAwaitUntil_Timeout() throws InterruptedException {
759 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
760          final Condition c = lock.writeLock().newCondition();
761          lock.writeLock().lock();
762          java.util.Date d = new java.util.Date();
763 +        assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
764          lock.writeLock().unlock();
765      }
766  
# Line 848 | Line 768 | public class ReentrantReadWriteLockTest
768       * await returns when signalled
769       */
770      public void testAwait() throws InterruptedException {
771 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
771 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
772          final Condition c = lock.writeLock().newCondition();
773 <        Thread t = new Thread(new Runnable() {
774 <                public void run() {
775 <                    try {
776 <                        lock.writeLock().lock();
777 <                        c.await();
778 <                        lock.writeLock().unlock();
859 <                    }
860 <                    catch (InterruptedException e) {
861 <                        threadUnexpectedException();
862 <                    }
863 <                }
864 <            });
773 >        Thread t = newStartedThread(new CheckedRunnable() {
774 >            public void realRun() throws InterruptedException {
775 >                lock.writeLock().lock();
776 >                c.await();
777 >                lock.writeLock().unlock();
778 >            }});
779  
866        t.start();
780          Thread.sleep(SHORT_DELAY_MS);
781          lock.writeLock().lock();
782          c.signal();
# Line 931 | Line 844 | public class ReentrantReadWriteLockTest
844       * await is interruptible
845       */
846      public void testAwait_Interrupt() throws InterruptedException {
847 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
847 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
848          final Condition c = lock.writeLock().newCondition();
849 <        Thread t = new Thread(new Runnable() {
850 <                public void run() {
851 <                    try {
852 <                        lock.writeLock().lock();
853 <                        c.await();
854 <                        lock.writeLock().unlock();
942 <                        threadShouldThrow();
943 <                    }
944 <                    catch (InterruptedException success) {
945 <                    }
946 <                }
947 <            });
849 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
850 >            public void realRun() throws InterruptedException {
851 >                lock.writeLock().lock();
852 >                c.await();
853 >                lock.writeLock().unlock();
854 >            }});
855  
949        t.start();
856          Thread.sleep(SHORT_DELAY_MS);
857          t.interrupt();
858          t.join(SHORT_DELAY_MS);
# Line 957 | Line 863 | public class ReentrantReadWriteLockTest
863       * awaitNanos is interruptible
864       */
865      public void testAwaitNanos_Interrupt() throws InterruptedException {
866 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
866 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
867          final Condition c = lock.writeLock().newCondition();
868 <        Thread t = new Thread(new Runnable() {
869 <                public void run() {
870 <                    try {
871 <                        lock.writeLock().lock();
872 <                        c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
873 <                        lock.writeLock().unlock();
968 <                        threadShouldThrow();
969 <                    }
970 <                    catch (InterruptedException success) {
971 <                    }
972 <                }
973 <            });
868 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
869 >            public void realRun() throws InterruptedException {
870 >                lock.writeLock().lock();
871 >                c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
872 >                lock.writeLock().unlock();
873 >            }});
874  
975        t.start();
875          Thread.sleep(SHORT_DELAY_MS);
876          t.interrupt();
877          t.join(SHORT_DELAY_MS);
# Line 983 | Line 882 | public class ReentrantReadWriteLockTest
882       * awaitUntil is interruptible
883       */
884      public void testAwaitUntil_Interrupt() throws InterruptedException {
885 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
885 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
886          final Condition c = lock.writeLock().newCondition();
887 <        Thread t = new Thread(new Runnable() {
888 <                public void run() {
889 <                    try {
890 <                        lock.writeLock().lock();
891 <                        java.util.Date d = new java.util.Date();
892 <                        c.awaitUntil(new java.util.Date(d.getTime() + 10000));
893 <                        lock.writeLock().unlock();
995 <                        threadShouldThrow();
996 <                    }
997 <                    catch (InterruptedException success) {
998 <                    }
999 <                }
1000 <            });
887 >        Thread t = newStartedThread(new CheckedInterruptedRunnable() {
888 >            public void realRun() throws InterruptedException {
889 >                lock.writeLock().lock();
890 >                java.util.Date d = new java.util.Date();
891 >                c.awaitUntil(new java.util.Date(d.getTime() + 10000));
892 >                lock.writeLock().unlock();
893 >            }});
894  
1002        t.start();
895          Thread.sleep(SHORT_DELAY_MS);
896          t.interrupt();
897          t.join(SHORT_DELAY_MS);
# Line 1010 | Line 902 | public class ReentrantReadWriteLockTest
902       * signalAll wakes up all threads
903       */
904      public void testSignalAll() throws InterruptedException {
905 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
905 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
906          final Condition c = lock.writeLock().newCondition();
907 <        Thread t1 = new Thread(new Runnable() {
908 <                public void run() {
909 <                    try {
910 <                        lock.writeLock().lock();
911 <                        c.await();
912 <                        lock.writeLock().unlock();
913 <                    }
914 <                    catch (InterruptedException e) {
915 <                        threadUnexpectedException();
916 <                    }
917 <                }
918 <            });
919 <
1028 <        Thread t2 = new Thread(new Runnable() {
1029 <                public void run() {
1030 <                    try {
1031 <                        lock.writeLock().lock();
1032 <                        c.await();
1033 <                        lock.writeLock().unlock();
1034 <                    }
1035 <                    catch (InterruptedException e) {
1036 <                        threadUnexpectedException();
1037 <                    }
1038 <                }
1039 <            });
907 >        Thread t1 = newStartedThread(new CheckedRunnable() {
908 >            public void realRun() throws InterruptedException {
909 >                lock.writeLock().lock();
910 >                c.await();
911 >                lock.writeLock().unlock();
912 >            }});
913 >
914 >        Thread t2 = newStartedThread(new CheckedRunnable() {
915 >            public void realRun() throws InterruptedException {
916 >                lock.writeLock().lock();
917 >                c.await();
918 >                lock.writeLock().unlock();
919 >            }});
920  
1041        t1.start();
1042        t2.start();
921          Thread.sleep(SHORT_DELAY_MS);
922          lock.writeLock().lock();
923          c.signalAll();
# Line 1074 | Line 952 | public class ReentrantReadWriteLockTest
952       * hasQueuedThreads reports whether there are waiting threads
953       */
954      public void testhasQueuedThreads() throws InterruptedException {
955 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
955 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
956          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
957          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
958          assertFalse(lock.hasQueuedThreads());
# Line 1099 | Line 977 | public class ReentrantReadWriteLockTest
977       * hasQueuedThread(null) throws NPE
978       */
979      public void testHasQueuedThreadNPE() {
980 <        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
980 >        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
981          try {
982              sync.hasQueuedThread(null);
983              shouldThrow();
984 <        } catch (NullPointerException success) {
1107 <        }
984 >        } catch (NullPointerException success) {}
985      }
986  
987      /**
988       * hasQueuedThread reports whether a thread is queued.
989       */
990      public void testHasQueuedThread() throws InterruptedException {
991 <        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
991 >        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
992          Thread t1 = new Thread(new InterruptedLockRunnable(sync));
993          Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
994          assertFalse(sync.hasQueuedThread(t1));
# Line 1142 | Line 1019 | public class ReentrantReadWriteLockTest
1019       * getQueueLength reports number of waiting threads
1020       */
1021      public void testGetQueueLength() throws InterruptedException {
1022 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1022 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1023          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1024          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1025          assertEquals(0, lock.getQueueLength());
# Line 1167 | Line 1044 | public class ReentrantReadWriteLockTest
1044       * getQueuedThreads includes waiting threads
1045       */
1046      public void testGetQueuedThreads() throws InterruptedException {
1047 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1047 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1048          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1049          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1050          assertTrue(lock.getQueuedThreads().isEmpty());
# Line 1195 | Line 1072 | public class ReentrantReadWriteLockTest
1072       * hasWaiters throws NPE if null
1073       */
1074      public void testHasWaitersNPE() {
1075 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1075 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1076          try {
1077              lock.hasWaiters(null);
1078              shouldThrow();
# Line 1206 | Line 1083 | public class ReentrantReadWriteLockTest
1083       * getWaitQueueLength throws NPE if null
1084       */
1085      public void testGetWaitQueueLengthNPE() {
1086 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1086 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1087          try {
1088              lock.getWaitQueueLength(null);
1089              shouldThrow();
# Line 1218 | Line 1095 | public class ReentrantReadWriteLockTest
1095       * getWaitingThreads throws NPE if null
1096       */
1097      public void testGetWaitingThreadsNPE() {
1098 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1098 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1099          try {
1100              lock.getWaitingThreads(null);
1101              shouldThrow();
# Line 1229 | Line 1106 | public class ReentrantReadWriteLockTest
1106       * hasWaiters throws IAE if not owned
1107       */
1108      public void testHasWaitersIAE() {
1109 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1109 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1110          final Condition c = lock.writeLock().newCondition();
1111 <        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1111 >        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1112          try {
1113              lock2.hasWaiters(c);
1114              shouldThrow();
# Line 1242 | Line 1119 | public class ReentrantReadWriteLockTest
1119       * hasWaiters throws IMSE if not locked
1120       */
1121      public void testHasWaitersIMSE() {
1122 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1122 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1123          final Condition c = lock.writeLock().newCondition();
1124          try {
1125              lock.hasWaiters(c);
# Line 1255 | Line 1132 | public class ReentrantReadWriteLockTest
1132       * getWaitQueueLength throws IAE if not owned
1133       */
1134      public void testGetWaitQueueLengthIAE() {
1135 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1135 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1136          final Condition c = lock.writeLock().newCondition();
1137 <        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1137 >        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1138          try {
1139              lock2.getWaitQueueLength(c);
1140              shouldThrow();
# Line 1268 | Line 1145 | public class ReentrantReadWriteLockTest
1145       * getWaitQueueLength throws IMSE if not locked
1146       */
1147      public void testGetWaitQueueLengthIMSE() {
1148 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1148 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1149          final Condition c = lock.writeLock().newCondition();
1150          try {
1151              lock.getWaitQueueLength(c);
# Line 1281 | Line 1158 | public class ReentrantReadWriteLockTest
1158       * getWaitingThreads throws IAE if not owned
1159       */
1160      public void testGetWaitingThreadsIAE() {
1161 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1161 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1162          final Condition c = lock.writeLock().newCondition();
1163 <        final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock();
1163 >        final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock();
1164          try {
1165              lock2.getWaitingThreads(c);
1166              shouldThrow();
# Line 1294 | Line 1171 | public class ReentrantReadWriteLockTest
1171       * getWaitingThreads throws IMSE if not locked
1172       */
1173      public void testGetWaitingThreadsIMSE() {
1174 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1174 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1175          final Condition c = lock.writeLock().newCondition();
1176          try {
1177              lock.getWaitingThreads(c);
# Line 1307 | Line 1184 | public class ReentrantReadWriteLockTest
1184       * hasWaiters returns true when a thread is waiting, else false
1185       */
1186      public void testHasWaiters() throws InterruptedException {
1187 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1187 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1188          final Condition c = lock.writeLock().newCondition();
1189 <        Thread t = new Thread(new Runnable() {
1190 <                public void run() {
1191 <                    try {
1192 <                        lock.writeLock().lock();
1193 <                        threadAssertFalse(lock.hasWaiters(c));
1194 <                        threadAssertEquals(0, lock.getWaitQueueLength(c));
1195 <                        c.await();
1196 <                        lock.writeLock().unlock();
1320 <                    }
1321 <                    catch (InterruptedException e) {
1322 <                        threadUnexpectedException();
1323 <                    }
1324 <                }
1325 <            });
1189 >        Thread t = newStartedThread(new CheckedRunnable() {
1190 >            public void realRun() throws InterruptedException {
1191 >                lock.writeLock().lock();
1192 >                assertFalse(lock.hasWaiters(c));
1193 >                assertEquals(0, lock.getWaitQueueLength(c));
1194 >                c.await();
1195 >                lock.writeLock().unlock();
1196 >            }});
1197  
1327        t.start();
1198          Thread.sleep(SHORT_DELAY_MS);
1199          lock.writeLock().lock();
1200          assertTrue(lock.hasWaiters(c));
# Line 1344 | Line 1214 | public class ReentrantReadWriteLockTest
1214       * getWaitQueueLength returns number of waiting threads
1215       */
1216      public void testGetWaitQueueLength() throws InterruptedException {
1217 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1217 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1218          final Condition c = lock.writeLock().newCondition();
1219 <        Thread t = new Thread(new Runnable() {
1220 <                public void run() {
1221 <                    try {
1222 <                        lock.writeLock().lock();
1223 <                        threadAssertFalse(lock.hasWaiters(c));
1224 <                        threadAssertEquals(0, lock.getWaitQueueLength(c));
1225 <                        c.await();
1226 <                        lock.writeLock().unlock();
1357 <                    }
1358 <                    catch (InterruptedException e) {
1359 <                        threadUnexpectedException();
1360 <                    }
1361 <                }
1362 <            });
1219 >        Thread t = newStartedThread(new CheckedRunnable() {
1220 >            public void realRun() throws InterruptedException {
1221 >                lock.writeLock().lock();
1222 >                assertFalse(lock.hasWaiters(c));
1223 >                assertEquals(0, lock.getWaitQueueLength(c));
1224 >                c.await();
1225 >                lock.writeLock().unlock();
1226 >            }});
1227  
1364        t.start();
1228          Thread.sleep(SHORT_DELAY_MS);
1229          lock.writeLock().lock();
1230          assertTrue(lock.hasWaiters(c));
# Line 1382 | Line 1245 | public class ReentrantReadWriteLockTest
1245       * getWaitingThreads returns only and all waiting threads
1246       */
1247      public void testGetWaitingThreads() throws InterruptedException {
1248 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1248 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1249          final Condition c = lock.writeLock().newCondition();
1250 <        Thread t1 = new Thread(new Runnable() {
1251 <                public void run() {
1252 <                    try {
1253 <                        lock.writeLock().lock();
1254 <                        threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
1255 <                        c.await();
1256 <                        lock.writeLock().unlock();
1257 <                    }
1258 <                    catch (InterruptedException e) {
1259 <                        threadUnexpectedException();
1260 <                    }
1261 <                }
1262 <            });
1263 <
1264 <        Thread t2 = new Thread(new Runnable() {
1402 <                public void run() {
1403 <                    try {
1404 <                        lock.writeLock().lock();
1405 <                        threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
1406 <                        c.await();
1407 <                        lock.writeLock().unlock();
1408 <                    }
1409 <                    catch (InterruptedException e) {
1410 <                        threadUnexpectedException();
1411 <                    }
1412 <                }
1413 <            });
1250 >        Thread t1 = new Thread(new CheckedRunnable() {
1251 >            public void realRun() throws InterruptedException {
1252 >                lock.writeLock().lock();
1253 >                assertTrue(lock.getWaitingThreads(c).isEmpty());
1254 >                c.await();
1255 >                lock.writeLock().unlock();
1256 >            }});
1257 >
1258 >        Thread t2 = new Thread(new CheckedRunnable() {
1259 >            public void realRun() throws InterruptedException {
1260 >                lock.writeLock().lock();
1261 >                assertFalse(lock.getWaitingThreads(c).isEmpty());
1262 >                c.await();
1263 >                lock.writeLock().unlock();
1264 >            }});
1265  
1266          lock.writeLock().lock();
1267          assertTrue(lock.getWaitingThreads(c).isEmpty());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines