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.38 by jsr166, Sat Nov 21 21:59:50 2009 UTC

# 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 61 | Line 62 | public class ReentrantReadWriteLockTest
62       * Constructor sets given fairness, and is in unlocked state
63       */
64      public void testConstructor() {
65 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
65 >        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
66          assertFalse(rl.isFair());
67          assertFalse(rl.isWriteLocked());
68          assertEquals(0, rl.getReadLockCount());
69 <        ReentrantReadWriteLock r2 = new ReentrantReadWriteLock(true);
69 >        ReentrantReadWriteLock r2 = new ReentrantReadWriteLock(true);
70          assertTrue(r2.isFair());
71          assertFalse(r2.isWriteLocked());
72          assertEquals(0, r2.getReadLockCount());
73 <        ReentrantReadWriteLock r3 = new ReentrantReadWriteLock(false);
73 >        ReentrantReadWriteLock r3 = new ReentrantReadWriteLock(false);
74          assertFalse(r3.isFair());
75          assertFalse(r3.isWriteLocked());
76          assertEquals(0, r3.getReadLockCount());
# Line 79 | Line 80 | public class ReentrantReadWriteLockTest
80       * write-locking and read-locking an unlocked lock succeed
81       */
82      public void testLock() {
83 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
83 >        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
84          rl.writeLock().lock();
85          assertTrue(rl.isWriteLocked());
86          assertTrue(rl.isWriteLockedByCurrentThread());
# Line 105 | Line 106 | public class ReentrantReadWriteLockTest
106       * locking an unlocked fair lock succeeds
107       */
108      public void testFairLock() {
109 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true);
109 >        ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true);
110          rl.writeLock().lock();
111          assertTrue(rl.isWriteLocked());
112          assertTrue(rl.isWriteLockedByCurrentThread());
# Line 130 | Line 131 | public class ReentrantReadWriteLockTest
131       * getWriteHoldCount returns number of recursive holds
132       */
133      public void testGetWriteHoldCount() {
134 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
135 <        for (int i = 1; i <= SIZE; i++) {
136 <            lock.writeLock().lock();
137 <            assertEquals(i,lock.getWriteHoldCount());
138 <        }
139 <        for (int i = SIZE; i > 0; i--) {
140 <            lock.writeLock().unlock();
141 <            assertEquals(i-1,lock.getWriteHoldCount());
142 <        }
134 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
135 >        for (int i = 1; i <= SIZE; i++) {
136 >            lock.writeLock().lock();
137 >            assertEquals(i,lock.getWriteHoldCount());
138 >        }
139 >        for (int i = SIZE; i > 0; i--) {
140 >            lock.writeLock().unlock();
141 >            assertEquals(i-1,lock.getWriteHoldCount());
142 >        }
143      }
144  
145      /**
146       * WriteLock.getHoldCount returns number of recursive holds
147       */
148      public void testGetHoldCount() {
149 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
150 <        for (int i = 1; i <= SIZE; i++) {
151 <            lock.writeLock().lock();
152 <            assertEquals(i,lock.writeLock().getHoldCount());
153 <        }
154 <        for (int i = SIZE; i > 0; i--) {
155 <            lock.writeLock().unlock();
156 <            assertEquals(i-1,lock.writeLock().getHoldCount());
157 <        }
149 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
150 >        for (int i = 1; i <= SIZE; i++) {
151 >            lock.writeLock().lock();
152 >            assertEquals(i,lock.writeLock().getHoldCount());
153 >        }
154 >        for (int i = SIZE; i > 0; i--) {
155 >            lock.writeLock().unlock();
156 >            assertEquals(i-1,lock.writeLock().getHoldCount());
157 >        }
158      }
159  
160      /**
161       * getReadHoldCount returns number of recursive holds
162       */
163      public void testGetReadHoldCount() {
164 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
165 <        for (int i = 1; i <= SIZE; i++) {
166 <            lock.readLock().lock();
167 <            assertEquals(i,lock.getReadHoldCount());
168 <        }
169 <        for (int i = SIZE; i > 0; i--) {
170 <            lock.readLock().unlock();
171 <            assertEquals(i-1,lock.getReadHoldCount());
172 <        }
164 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
165 >        for (int i = 1; i <= SIZE; i++) {
166 >            lock.readLock().lock();
167 >            assertEquals(i,lock.getReadHoldCount());
168 >        }
169 >        for (int i = SIZE; i > 0; i--) {
170 >            lock.readLock().unlock();
171 >            assertEquals(i-1,lock.getReadHoldCount());
172 >        }
173      }
174  
175  
# Line 176 | Line 177 | public class ReentrantReadWriteLockTest
177       * write-unlocking an unlocked lock throws IllegalMonitorStateException
178       */
179      public void testUnlock_IllegalMonitorStateException() {
180 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
181 <        try {
182 <            rl.writeLock().unlock();
183 <            shouldThrow();
184 <        } catch (IllegalMonitorStateException success) {}
180 >        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
181 >        try {
182 >            rl.writeLock().unlock();
183 >            shouldThrow();
184 >        } catch (IllegalMonitorStateException success) {}
185      }
186  
187  
# Line 188 | Line 189 | public class ReentrantReadWriteLockTest
189       * write-lockInterruptibly is interruptible
190       */
191      public void testWriteLockInterruptibly_Interrupted() throws Exception {
192 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
193 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
192 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
193 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
194              public void realRun() throws InterruptedException {
195                  lock.writeLock().lockInterruptibly();
196                  lock.writeLock().unlock();
# Line 210 | Line 211 | public class ReentrantReadWriteLockTest
211       * timed write-tryLock is interruptible
212       */
213      public void testWriteTryLock_Interrupted() throws InterruptedException {
214 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
215 <        lock.writeLock().lock();
216 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
214 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
215 >        lock.writeLock().lock();
216 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
217              public void realRun() throws InterruptedException {
218 <                lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS);
218 >                lock.writeLock().tryLock(SMALL_DELAY_MS, MILLISECONDS);
219              }});
220  
221          t.start();
222 +        Thread.sleep(SHORT_DELAY_MS);
223          t.interrupt();
224          lock.writeLock().unlock();
225          t.join();
# Line 227 | Line 229 | public class ReentrantReadWriteLockTest
229       * read-lockInterruptibly is interruptible
230       */
231      public void testReadLockInterruptibly_Interrupted() throws InterruptedException {
232 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
233 <        lock.writeLock().lock();
234 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
232 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
233 >        lock.writeLock().lock();
234 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
235              public void realRun() throws InterruptedException {
236                  lock.readLock().lockInterruptibly();
237              }});
# Line 246 | Line 248 | public class ReentrantReadWriteLockTest
248       * timed read-tryLock is interruptible
249       */
250      public void testReadTryLock_Interrupted() throws InterruptedException {
251 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
252 <        lock.writeLock().lock();
253 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
251 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
252 >        lock.writeLock().lock();
253 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
254              public void realRun() throws InterruptedException {
255 <                lock.readLock().tryLock(1000,TimeUnit.MILLISECONDS);
255 >                lock.readLock().tryLock(1000,MILLISECONDS);
256              }});
257  
258          t.start();
# Line 263 | Line 265 | public class ReentrantReadWriteLockTest
265       * write-tryLock fails if locked
266       */
267      public void testWriteTryLockWhenLocked() throws InterruptedException {
268 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
269 <        lock.writeLock().lock();
270 <        Thread t = new Thread(new Runnable() {
271 <                public void run() {
272 <                    threadAssertFalse(lock.writeLock().tryLock());
273 <                }
272 <            });
268 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
269 >        lock.writeLock().lock();
270 >        Thread t = new Thread(new CheckedRunnable() {
271 >            public void realRun() {
272 >                threadAssertFalse(lock.writeLock().tryLock());
273 >            }});
274  
275          t.start();
276          t.join();
# Line 280 | 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 <                }
289 <            });
284 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
285 >        lock.writeLock().lock();
286 >        Thread t = new Thread(new CheckedRunnable() {
287 >            public void realRun() {
288 >                threadAssertFalse(lock.readLock().tryLock());
289 >            }});
290  
291          t.start();
292          t.join();
# 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 = new Thread(new CheckedRunnable() {
303 >            public void realRun() {
304 >                threadAssertTrue(lock.readLock().tryLock());
305 >                lock.readLock().unlock();
306 >            }});
307  
308          t.start();
309          t.join();
# Line 315 | Line 314 | public class ReentrantReadWriteLockTest
314       * A writelock succeeds after reading threads unlock
315       */
316      public void testWriteAfterMultipleReadLocks() throws InterruptedException {
317 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
318 <        lock.readLock().lock();
319 <        Thread t1 = new Thread(new Runnable() {
320 <                public void run() {
321 <                    lock.readLock().lock();
322 <                    lock.readLock().unlock();
323 <                }
324 <            });
325 <        Thread t2 = new Thread(new Runnable() {
326 <                public void run() {
327 <                    lock.writeLock().lock();
328 <                    lock.writeLock().unlock();
330 <                }
331 <            });
317 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
318 >        lock.readLock().lock();
319 >        Thread t1 = new Thread(new CheckedRunnable() {
320 >            public void realRun() {
321 >                lock.readLock().lock();
322 >                lock.readLock().unlock();
323 >            }});
324 >        Thread t2 = new Thread(new CheckedRunnable() {
325 >            public void realRun() {
326 >                lock.writeLock().lock();
327 >                lock.writeLock().unlock();
328 >            }});
329  
330          t1.start();
331          t2.start();
# Line 344 | Line 341 | public class ReentrantReadWriteLockTest
341       * Readlocks succeed after a writing thread unlocks
342       */
343      public void testReadAfterWriteLock() throws InterruptedException {
344 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
345 <        lock.writeLock().lock();
346 <        Thread t1 = new Thread(new Runnable() {
347 <                public void run() {
348 <                    lock.readLock().lock();
349 <                    lock.readLock().unlock();
350 <                }
351 <            });
352 <        Thread t2 = new Thread(new Runnable() {
353 <                public void run() {
354 <                    lock.readLock().lock();
355 <                    lock.readLock().unlock();
359 <                }
360 <            });
344 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
345 >        lock.writeLock().lock();
346 >        Thread t1 = new Thread(new CheckedRunnable() {
347 >            public void realRun() {
348 >                lock.readLock().lock();
349 >                lock.readLock().unlock();
350 >            }});
351 >        Thread t2 = new Thread(new CheckedRunnable() {
352 >            public void realRun() {
353 >                lock.readLock().lock();
354 >                lock.readLock().unlock();
355 >            }});
356  
357          t1.start();
358          t2.start();
# Line 373 | Line 368 | public class ReentrantReadWriteLockTest
368       * Read trylock succeeds if write locked by current thread
369       */
370      public void testReadHoldingWriteLock() {
371 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
372 <        lock.writeLock().lock();
371 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
372 >        lock.writeLock().lock();
373          assertTrue(lock.readLock().tryLock());
374          lock.readLock().unlock();
375          lock.writeLock().unlock();
# Line 385 | Line 380 | public class ReentrantReadWriteLockTest
380       * other threads are waiting for readlock
381       */
382      public void testReadHoldingWriteLock2() throws InterruptedException {
383 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
384 <        lock.writeLock().lock();
385 <        Thread t1 = new Thread(new Runnable() {
386 <                public void run() {
387 <                    lock.readLock().lock();
388 <                    lock.readLock().unlock();
389 <                }
390 <            });
391 <        Thread t2 = new Thread(new Runnable() {
392 <                public void run() {
393 <                    lock.readLock().lock();
394 <                    lock.readLock().unlock();
400 <                }
401 <            });
383 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
384 >        lock.writeLock().lock();
385 >        Thread t1 = new Thread(new CheckedRunnable() {
386 >            public void realRun() {
387 >                lock.readLock().lock();
388 >                lock.readLock().unlock();
389 >            }});
390 >        Thread t2 = new Thread(new CheckedRunnable() {
391 >            public void realRun() {
392 >                lock.readLock().lock();
393 >                lock.readLock().unlock();
394 >            }});
395  
396          t1.start();
397          t2.start();
# Line 419 | Line 412 | public class ReentrantReadWriteLockTest
412       * other threads are waiting for writelock
413       */
414      public void testReadHoldingWriteLock3() throws InterruptedException {
415 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
416 <        lock.writeLock().lock();
417 <        Thread t1 = new Thread(new Runnable() {
418 <                public void run() {
419 <                    lock.writeLock().lock();
420 <                    lock.writeLock().unlock();
421 <                }
422 <            });
423 <        Thread t2 = new Thread(new Runnable() {
424 <                public void run() {
425 <                    lock.writeLock().lock();
426 <                    lock.writeLock().unlock();
434 <                }
435 <            });
415 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
416 >        lock.writeLock().lock();
417 >        Thread t1 = new Thread(new CheckedRunnable() {
418 >            public void realRun() {
419 >                lock.writeLock().lock();
420 >                lock.writeLock().unlock();
421 >            }});
422 >        Thread t2 = new Thread(new CheckedRunnable() {
423 >            public void realRun() {
424 >                lock.writeLock().lock();
425 >                lock.writeLock().unlock();
426 >            }});
427  
428          t1.start();
429          t2.start();
# Line 454 | Line 445 | public class ReentrantReadWriteLockTest
445       * other threads are waiting for writelock
446       */
447      public void testWriteHoldingWriteLock4() throws InterruptedException {
448 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
449 <        lock.writeLock().lock();
450 <        Thread t1 = new Thread(new Runnable() {
451 <                public void run() {
452 <                    lock.writeLock().lock();
453 <                    lock.writeLock().unlock();
454 <                }
455 <            });
456 <        Thread t2 = new Thread(new Runnable() {
457 <                public void run() {
458 <                    lock.writeLock().lock();
459 <                    lock.writeLock().unlock();
469 <                }
470 <            });
448 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
449 >        lock.writeLock().lock();
450 >        Thread t1 = new Thread(new CheckedRunnable() {
451 >            public void realRun() {
452 >                lock.writeLock().lock();
453 >                lock.writeLock().unlock();
454 >            }});
455 >        Thread t2 = new Thread(new CheckedRunnable() {
456 >            public void realRun() {
457 >                lock.writeLock().lock();
458 >                lock.writeLock().unlock();
459 >            }});
460  
461          t1.start();
462          t2.start();
# Line 488 | Line 477 | public class ReentrantReadWriteLockTest
477       * Fair Read trylock succeeds if write locked by current thread
478       */
479      public void testReadHoldingWriteLockFair() {
480 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
481 <        lock.writeLock().lock();
480 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
481 >        lock.writeLock().lock();
482          assertTrue(lock.readLock().tryLock());
483          lock.readLock().unlock();
484          lock.writeLock().unlock();
# Line 500 | Line 489 | public class ReentrantReadWriteLockTest
489       * other threads are waiting for readlock
490       */
491      public void testReadHoldingWriteLockFair2() throws InterruptedException {
492 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
493 <        lock.writeLock().lock();
494 <        Thread t1 = new Thread(new Runnable() {
495 <                public void run() {
496 <                    lock.readLock().lock();
497 <                    lock.readLock().unlock();
498 <                }
499 <            });
500 <        Thread t2 = new Thread(new Runnable() {
501 <                public void run() {
502 <                    lock.readLock().lock();
503 <                    lock.readLock().unlock();
515 <                }
516 <            });
492 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
493 >        lock.writeLock().lock();
494 >        Thread t1 = new Thread(new CheckedRunnable() {
495 >            public void realRun() {
496 >                lock.readLock().lock();
497 >                lock.readLock().unlock();
498 >            }});
499 >        Thread t2 = new Thread(new CheckedRunnable() {
500 >            public void realRun() {
501 >                lock.readLock().lock();
502 >                lock.readLock().unlock();
503 >            }});
504  
505          t1.start();
506          t2.start();
# Line 535 | Line 522 | public class ReentrantReadWriteLockTest
522       * other threads are waiting for writelock
523       */
524      public void testReadHoldingWriteLockFair3() throws InterruptedException {
525 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
526 <        lock.writeLock().lock();
527 <        Thread t1 = new Thread(new Runnable() {
528 <                public void run() {
529 <                    lock.writeLock().lock();
530 <                    lock.writeLock().unlock();
531 <                }
532 <            });
533 <        Thread t2 = new Thread(new Runnable() {
534 <                public void run() {
535 <                    lock.writeLock().lock();
536 <                    lock.writeLock().unlock();
550 <                }
551 <            });
525 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
526 >        lock.writeLock().lock();
527 >        Thread t1 = new Thread(new CheckedRunnable() {
528 >            public void realRun() {
529 >                lock.writeLock().lock();
530 >                lock.writeLock().unlock();
531 >            }});
532 >        Thread t2 = new Thread(new CheckedRunnable() {
533 >            public void realRun() {
534 >                lock.writeLock().lock();
535 >                lock.writeLock().unlock();
536 >            }});
537  
538          t1.start();
539          t2.start();
# Line 570 | Line 555 | public class ReentrantReadWriteLockTest
555       * other threads are waiting for writelock
556       */
557      public void testWriteHoldingWriteLockFair4() throws InterruptedException {
558 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
559 <        lock.writeLock().lock();
560 <        Thread t1 = new Thread(new Runnable() {
561 <                public void run() {
562 <                    lock.writeLock().lock();
563 <                    lock.writeLock().unlock();
564 <                }
565 <            });
566 <        Thread t2 = new Thread(new Runnable() {
567 <                public void run() {
568 <                    lock.writeLock().lock();
569 <                    lock.writeLock().unlock();
585 <                }
586 <            });
558 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
559 >        lock.writeLock().lock();
560 >        Thread t1 = new Thread(new CheckedRunnable() {
561 >            public void realRun() {
562 >                lock.writeLock().lock();
563 >                lock.writeLock().unlock();
564 >            }});
565 >        Thread t2 = new Thread(new CheckedRunnable() {
566 >            public void realRun() {
567 >                lock.writeLock().lock();
568 >                lock.writeLock().unlock();
569 >            }});
570  
571          t1.start();
572          t2.start();
# Line 607 | Line 590 | public class ReentrantReadWriteLockTest
590       * Read tryLock succeeds if readlocked but not writelocked
591       */
592      public void testTryLockWhenReadLocked() throws InterruptedException {
593 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
594 <        lock.readLock().lock();
595 <        Thread t = new Thread(new Runnable() {
596 <                public void run() {
597 <                    threadAssertTrue(lock.readLock().tryLock());
598 <                    lock.readLock().unlock();
599 <                }
617 <            });
593 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
594 >        lock.readLock().lock();
595 >        Thread t = new Thread(new CheckedRunnable() {
596 >            public void realRun() {
597 >                threadAssertTrue(lock.readLock().tryLock());
598 >                lock.readLock().unlock();
599 >            }});
600  
601          t.start();
602          t.join();
# Line 627 | Line 609 | public class ReentrantReadWriteLockTest
609       * write tryLock fails when readlocked
610       */
611      public void testWriteTryLockWhenReadLocked() throws InterruptedException {
612 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
613 <        lock.readLock().lock();
614 <        Thread t = new Thread(new Runnable() {
615 <                public void run() {
616 <                    threadAssertFalse(lock.writeLock().tryLock());
617 <                }
636 <            });
612 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
613 >        lock.readLock().lock();
614 >        Thread t = new Thread(new CheckedRunnable() {
615 >            public void realRun() {
616 >                threadAssertFalse(lock.writeLock().tryLock());
617 >            }});
618  
619          t.start();
620          t.join();
# Line 645 | Line 626 | public class ReentrantReadWriteLockTest
626       * Fair Read tryLock succeeds if readlocked but not writelocked
627       */
628      public void testTryLockWhenReadLockedFair() throws InterruptedException {
629 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
630 <        lock.readLock().lock();
631 <        Thread t = new Thread(new Runnable() {
632 <                public void run() {
633 <                    threadAssertTrue(lock.readLock().tryLock());
634 <                    lock.readLock().unlock();
635 <                }
655 <            });
629 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
630 >        lock.readLock().lock();
631 >        Thread t = new Thread(new CheckedRunnable() {
632 >            public void realRun() {
633 >                threadAssertTrue(lock.readLock().tryLock());
634 >                lock.readLock().unlock();
635 >            }});
636  
637          t.start();
638          t.join();
# Line 665 | Line 645 | public class ReentrantReadWriteLockTest
645       * Fair write tryLock fails when readlocked
646       */
647      public void testWriteTryLockWhenReadLockedFair() throws InterruptedException {
648 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
649 <        lock.readLock().lock();
650 <        Thread t = new Thread(new Runnable() {
651 <                public void run() {
652 <                    threadAssertFalse(lock.writeLock().tryLock());
653 <                }
674 <            });
648 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
649 >        lock.readLock().lock();
650 >        Thread t = new Thread(new CheckedRunnable() {
651 >            public void realRun() {
652 >                threadAssertFalse(lock.writeLock().tryLock());
653 >            }});
654  
655          t.start();
656          t.join();
# Line 684 | Line 663 | public class ReentrantReadWriteLockTest
663       * write timed tryLock times out if locked
664       */
665      public void testWriteTryLock_Timeout() throws InterruptedException {
666 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
667 <        lock.writeLock().lock();
668 <        Thread t = new Thread(new CheckedRunnable() {
666 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
667 >        lock.writeLock().lock();
668 >        Thread t = new Thread(new CheckedRunnable() {
669              public void realRun() throws InterruptedException {
670 <                threadAssertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS));
670 >                threadAssertFalse(lock.writeLock().tryLock(1, MILLISECONDS));
671              }});
672  
673          t.start();
674          t.join();
675 +        assertTrue(lock.writeLock().isHeldByCurrentThread());
676          lock.writeLock().unlock();
677      }
678  
# Line 700 | Line 680 | public class ReentrantReadWriteLockTest
680       * read timed tryLock times out if write-locked
681       */
682      public void testReadTryLock_Timeout() throws InterruptedException {
683 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
684 <        lock.writeLock().lock();
685 <        Thread t = new Thread(new CheckedRunnable() {
683 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
684 >        lock.writeLock().lock();
685 >        Thread t = new Thread(new CheckedRunnable() {
686              public void realRun() throws InterruptedException {
687 <                threadAssertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS));
687 >                threadAssertFalse(lock.readLock().tryLock(1, MILLISECONDS));
688              }});
689  
690          t.start();
691          t.join();
692 +        assertTrue(lock.writeLock().isHeldByCurrentThread());
693          lock.writeLock().unlock();
694      }
695  
# Line 717 | Line 698 | public class ReentrantReadWriteLockTest
698       * write lockInterruptibly succeeds if lock free else is interruptible
699       */
700      public void testWriteLockInterruptibly() throws InterruptedException {
701 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
701 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
702          lock.writeLock().lockInterruptibly();
703 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
703 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
704              public void realRun() throws InterruptedException {
705                  lock.writeLock().lockInterruptibly();
706              }});
# Line 736 | Line 717 | public class ReentrantReadWriteLockTest
717       *  read lockInterruptibly succeeds if lock free else is interruptible
718       */
719      public void testReadLockInterruptibly() throws InterruptedException {
720 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
720 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
721          lock.writeLock().lockInterruptibly();
722 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
722 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
723              public void realRun() throws InterruptedException {
724                  lock.readLock().lockInterruptibly();
725              }});
# Line 754 | Line 735 | public class ReentrantReadWriteLockTest
735       * Calling await without holding lock throws IllegalMonitorStateException
736       */
737      public void testAwait_IllegalMonitor() throws InterruptedException {
738 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
738 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
739          final Condition c = lock.writeLock().newCondition();
740          try {
741              c.await();
# Line 766 | Line 747 | public class ReentrantReadWriteLockTest
747       * Calling signal without holding lock throws IllegalMonitorStateException
748       */
749      public void testSignal_IllegalMonitor() {
750 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
750 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
751          final Condition c = lock.writeLock().newCondition();
752          try {
753              c.signal();
# Line 778 | Line 759 | public class ReentrantReadWriteLockTest
759       * awaitNanos without a signal times out
760       */
761      public void testAwaitNanos_Timeout() throws InterruptedException {
762 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
762 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
763          final Condition c = lock.writeLock().newCondition();
764  
765          lock.writeLock().lock();
# Line 791 | Line 772 | public class ReentrantReadWriteLockTest
772      /**
773       *  timed await without a signal times out
774       */
775 <    public void testAwait_Timeout() {
776 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
775 >    public void testAwait_Timeout() throws InterruptedException {
776 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
777          final Condition c = lock.writeLock().newCondition();
778          lock.writeLock().lock();
779 +        assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS));
780          lock.writeLock().unlock();
781      }
782  
783      /**
784       * awaitUntil without a signal times out
785       */
786 <    public void testAwaitUntil_Timeout() {
787 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
786 >    public void testAwaitUntil_Timeout() throws InterruptedException {
787 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
788          final Condition c = lock.writeLock().newCondition();
789          lock.writeLock().lock();
790          java.util.Date d = new java.util.Date();
791 +        assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
792          lock.writeLock().unlock();
793      }
794  
# Line 813 | Line 796 | public class ReentrantReadWriteLockTest
796       * await returns when signalled
797       */
798      public void testAwait() throws InterruptedException {
799 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
799 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
800          final Condition c = lock.writeLock().newCondition();
801 <        Thread t = new Thread(new CheckedRunnable() {
801 >        Thread t = new Thread(new CheckedRunnable() {
802              public void realRun() throws InterruptedException {
803                  lock.writeLock().lock();
804                  c.await();
# Line 890 | Line 873 | public class ReentrantReadWriteLockTest
873       * await is interruptible
874       */
875      public void testAwait_Interrupt() throws InterruptedException {
876 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
876 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
877          final Condition c = lock.writeLock().newCondition();
878 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
878 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
879              public void realRun() throws InterruptedException {
880                  lock.writeLock().lock();
881                  c.await();
# Line 910 | Line 893 | public class ReentrantReadWriteLockTest
893       * awaitNanos is interruptible
894       */
895      public void testAwaitNanos_Interrupt() throws InterruptedException {
896 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
896 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
897          final Condition c = lock.writeLock().newCondition();
898 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
898 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
899              public void realRun() throws InterruptedException {
900                  lock.writeLock().lock();
901                  c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
# Line 930 | Line 913 | public class ReentrantReadWriteLockTest
913       * awaitUntil is interruptible
914       */
915      public void testAwaitUntil_Interrupt() throws InterruptedException {
916 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
916 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
917          final Condition c = lock.writeLock().newCondition();
918 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
918 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
919              public void realRun() throws InterruptedException {
920                  lock.writeLock().lock();
921                  java.util.Date d = new java.util.Date();
# Line 951 | Line 934 | public class ReentrantReadWriteLockTest
934       * signalAll wakes up all threads
935       */
936      public void testSignalAll() throws InterruptedException {
937 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
937 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
938          final Condition c = lock.writeLock().newCondition();
939 <        Thread t1 = new Thread(new CheckedRunnable() {
939 >        Thread t1 = new Thread(new CheckedRunnable() {
940              public void realRun() throws InterruptedException {
941                  lock.writeLock().lock();
942                  c.await();
943                  lock.writeLock().unlock();
944              }});
945  
946 <        Thread t2 = new Thread(new CheckedRunnable() {
946 >        Thread t2 = new Thread(new CheckedRunnable() {
947              public void realRun() throws InterruptedException {
948                  lock.writeLock().lock();
949                  c.await();
# Line 1003 | Line 986 | public class ReentrantReadWriteLockTest
986       * hasQueuedThreads reports whether there are waiting threads
987       */
988      public void testhasQueuedThreads() throws InterruptedException {
989 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
989 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
990          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
991          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
992          assertFalse(lock.hasQueuedThreads());
# Line 1028 | Line 1011 | public class ReentrantReadWriteLockTest
1011       * hasQueuedThread(null) throws NPE
1012       */
1013      public void testHasQueuedThreadNPE() {
1014 <        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1014 >        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1015          try {
1016              sync.hasQueuedThread(null);
1017              shouldThrow();
# Line 1039 | Line 1022 | public class ReentrantReadWriteLockTest
1022       * hasQueuedThread reports whether a thread is queued.
1023       */
1024      public void testHasQueuedThread() throws InterruptedException {
1025 <        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1025 >        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1026          Thread t1 = new Thread(new InterruptedLockRunnable(sync));
1027          Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
1028          assertFalse(sync.hasQueuedThread(t1));
# Line 1070 | Line 1053 | public class ReentrantReadWriteLockTest
1053       * getQueueLength reports number of waiting threads
1054       */
1055      public void testGetQueueLength() throws InterruptedException {
1056 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1056 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1057          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1058          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1059          assertEquals(0, lock.getQueueLength());
# Line 1095 | Line 1078 | public class ReentrantReadWriteLockTest
1078       * getQueuedThreads includes waiting threads
1079       */
1080      public void testGetQueuedThreads() throws InterruptedException {
1081 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1081 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1082          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1083          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1084          assertTrue(lock.getQueuedThreads().isEmpty());
# Line 1123 | Line 1106 | public class ReentrantReadWriteLockTest
1106       * hasWaiters throws NPE if null
1107       */
1108      public void testHasWaitersNPE() {
1109 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1109 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1110          try {
1111              lock.hasWaiters(null);
1112              shouldThrow();
# Line 1134 | Line 1117 | public class ReentrantReadWriteLockTest
1117       * getWaitQueueLength throws NPE if null
1118       */
1119      public void testGetWaitQueueLengthNPE() {
1120 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1120 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1121          try {
1122              lock.getWaitQueueLength(null);
1123              shouldThrow();
# Line 1146 | Line 1129 | public class ReentrantReadWriteLockTest
1129       * getWaitingThreads throws NPE if null
1130       */
1131      public void testGetWaitingThreadsNPE() {
1132 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1132 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1133          try {
1134              lock.getWaitingThreads(null);
1135              shouldThrow();
# Line 1157 | Line 1140 | public class ReentrantReadWriteLockTest
1140       * hasWaiters throws IAE if not owned
1141       */
1142      public void testHasWaitersIAE() {
1143 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1143 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1144          final Condition c = lock.writeLock().newCondition();
1145 <        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1145 >        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1146          try {
1147              lock2.hasWaiters(c);
1148              shouldThrow();
# Line 1170 | Line 1153 | public class ReentrantReadWriteLockTest
1153       * hasWaiters throws IMSE if not locked
1154       */
1155      public void testHasWaitersIMSE() {
1156 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1156 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1157          final Condition c = lock.writeLock().newCondition();
1158          try {
1159              lock.hasWaiters(c);
# Line 1183 | Line 1166 | public class ReentrantReadWriteLockTest
1166       * getWaitQueueLength throws IAE if not owned
1167       */
1168      public void testGetWaitQueueLengthIAE() {
1169 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1169 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1170          final Condition c = lock.writeLock().newCondition();
1171 <        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1171 >        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1172          try {
1173              lock2.getWaitQueueLength(c);
1174              shouldThrow();
# Line 1196 | Line 1179 | public class ReentrantReadWriteLockTest
1179       * getWaitQueueLength throws IMSE if not locked
1180       */
1181      public void testGetWaitQueueLengthIMSE() {
1182 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1182 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1183          final Condition c = lock.writeLock().newCondition();
1184          try {
1185              lock.getWaitQueueLength(c);
# Line 1209 | Line 1192 | public class ReentrantReadWriteLockTest
1192       * getWaitingThreads throws IAE if not owned
1193       */
1194      public void testGetWaitingThreadsIAE() {
1195 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1195 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1196          final Condition c = lock.writeLock().newCondition();
1197 <        final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock();
1197 >        final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock();
1198          try {
1199              lock2.getWaitingThreads(c);
1200              shouldThrow();
# Line 1222 | Line 1205 | public class ReentrantReadWriteLockTest
1205       * getWaitingThreads throws IMSE if not locked
1206       */
1207      public void testGetWaitingThreadsIMSE() {
1208 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1208 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1209          final Condition c = lock.writeLock().newCondition();
1210          try {
1211              lock.getWaitingThreads(c);
# Line 1235 | Line 1218 | public class ReentrantReadWriteLockTest
1218       * hasWaiters returns true when a thread is waiting, else false
1219       */
1220      public void testHasWaiters() throws InterruptedException {
1221 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1221 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1222          final Condition c = lock.writeLock().newCondition();
1223 <        Thread t = new Thread(new CheckedRunnable() {
1223 >        Thread t = new Thread(new CheckedRunnable() {
1224              public void realRun() throws InterruptedException {
1225                  lock.writeLock().lock();
1226                  threadAssertFalse(lock.hasWaiters(c));
# Line 1266 | Line 1249 | public class ReentrantReadWriteLockTest
1249       * getWaitQueueLength returns number of waiting threads
1250       */
1251      public void testGetWaitQueueLength() throws InterruptedException {
1252 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1252 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1253          final Condition c = lock.writeLock().newCondition();
1254 <        Thread t = new Thread(new CheckedRunnable() {
1254 >        Thread t = new Thread(new CheckedRunnable() {
1255              public void realRun() throws InterruptedException {
1256                  lock.writeLock().lock();
1257                  threadAssertFalse(lock.hasWaiters(c));
# Line 1298 | Line 1281 | public class ReentrantReadWriteLockTest
1281       * getWaitingThreads returns only and all waiting threads
1282       */
1283      public void testGetWaitingThreads() throws InterruptedException {
1284 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1284 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1285          final Condition c = lock.writeLock().newCondition();
1286 <        Thread t1 = new Thread(new CheckedRunnable() {
1286 >        Thread t1 = new Thread(new CheckedRunnable() {
1287              public void realRun() throws InterruptedException {
1288                  lock.writeLock().lock();
1289                  threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
# Line 1308 | Line 1291 | public class ReentrantReadWriteLockTest
1291                  lock.writeLock().unlock();
1292              }});
1293  
1294 <        Thread t2 = new Thread(new CheckedRunnable() {
1294 >        Thread t2 = new Thread(new CheckedRunnable() {
1295              public void realRun() throws InterruptedException {
1296                  lock.writeLock().lock();
1297                  threadAssertFalse(lock.getWaitingThreads(c).isEmpty());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines