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.34 by jsr166, Tue Nov 17 14:45:32 2009 UTC vs.
Revision 1.35 by jsr166, Sat Nov 21 02:07:27 2009 UTC

# Line 14 | Line 14 | import java.util.*;
14  
15   public class ReentrantReadWriteLockTest extends JSR166TestCase {
16      public static void main(String[] args) {
17 <        junit.textui.TestRunner.run (suite());
17 >        junit.textui.TestRunner.run (suite());
18      }
19      public static Test suite() {
20 <        return new TestSuite(ReentrantReadWriteLockTest.class);
20 >        return new TestSuite(ReentrantReadWriteLockTest.class);
21      }
22  
23      /**
# Line 61 | Line 61 | public class ReentrantReadWriteLockTest
61       * Constructor sets given fairness, and is in unlocked state
62       */
63      public void testConstructor() {
64 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
64 >        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
65          assertFalse(rl.isFair());
66          assertFalse(rl.isWriteLocked());
67          assertEquals(0, rl.getReadLockCount());
68 <        ReentrantReadWriteLock r2 = new ReentrantReadWriteLock(true);
68 >        ReentrantReadWriteLock r2 = new ReentrantReadWriteLock(true);
69          assertTrue(r2.isFair());
70          assertFalse(r2.isWriteLocked());
71          assertEquals(0, r2.getReadLockCount());
72 <        ReentrantReadWriteLock r3 = new ReentrantReadWriteLock(false);
72 >        ReentrantReadWriteLock r3 = new ReentrantReadWriteLock(false);
73          assertFalse(r3.isFair());
74          assertFalse(r3.isWriteLocked());
75          assertEquals(0, r3.getReadLockCount());
# Line 79 | Line 79 | public class ReentrantReadWriteLockTest
79       * write-locking and read-locking an unlocked lock succeed
80       */
81      public void testLock() {
82 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
82 >        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
83          rl.writeLock().lock();
84          assertTrue(rl.isWriteLocked());
85          assertTrue(rl.isWriteLockedByCurrentThread());
# Line 105 | Line 105 | public class ReentrantReadWriteLockTest
105       * locking an unlocked fair lock succeeds
106       */
107      public void testFairLock() {
108 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true);
108 >        ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true);
109          rl.writeLock().lock();
110          assertTrue(rl.isWriteLocked());
111          assertTrue(rl.isWriteLockedByCurrentThread());
# Line 130 | Line 130 | public class ReentrantReadWriteLockTest
130       * getWriteHoldCount returns number of recursive holds
131       */
132      public void testGetWriteHoldCount() {
133 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
134 <        for (int i = 1; i <= SIZE; i++) {
135 <            lock.writeLock().lock();
136 <            assertEquals(i,lock.getWriteHoldCount());
137 <        }
138 <        for (int i = SIZE; i > 0; i--) {
139 <            lock.writeLock().unlock();
140 <            assertEquals(i-1,lock.getWriteHoldCount());
141 <        }
133 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
134 >        for (int i = 1; i <= SIZE; i++) {
135 >            lock.writeLock().lock();
136 >            assertEquals(i,lock.getWriteHoldCount());
137 >        }
138 >        for (int i = SIZE; i > 0; i--) {
139 >            lock.writeLock().unlock();
140 >            assertEquals(i-1,lock.getWriteHoldCount());
141 >        }
142      }
143  
144      /**
145       * WriteLock.getHoldCount returns number of recursive holds
146       */
147      public void testGetHoldCount() {
148 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
149 <        for (int i = 1; i <= SIZE; i++) {
150 <            lock.writeLock().lock();
151 <            assertEquals(i,lock.writeLock().getHoldCount());
152 <        }
153 <        for (int i = SIZE; i > 0; i--) {
154 <            lock.writeLock().unlock();
155 <            assertEquals(i-1,lock.writeLock().getHoldCount());
156 <        }
148 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
149 >        for (int i = 1; i <= SIZE; i++) {
150 >            lock.writeLock().lock();
151 >            assertEquals(i,lock.writeLock().getHoldCount());
152 >        }
153 >        for (int i = SIZE; i > 0; i--) {
154 >            lock.writeLock().unlock();
155 >            assertEquals(i-1,lock.writeLock().getHoldCount());
156 >        }
157      }
158  
159      /**
160       * getReadHoldCount returns number of recursive holds
161       */
162      public void testGetReadHoldCount() {
163 <        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
164 <        for (int i = 1; i <= SIZE; i++) {
165 <            lock.readLock().lock();
166 <            assertEquals(i,lock.getReadHoldCount());
167 <        }
168 <        for (int i = SIZE; i > 0; i--) {
169 <            lock.readLock().unlock();
170 <            assertEquals(i-1,lock.getReadHoldCount());
171 <        }
163 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
164 >        for (int i = 1; i <= SIZE; i++) {
165 >            lock.readLock().lock();
166 >            assertEquals(i,lock.getReadHoldCount());
167 >        }
168 >        for (int i = SIZE; i > 0; i--) {
169 >            lock.readLock().unlock();
170 >            assertEquals(i-1,lock.getReadHoldCount());
171 >        }
172      }
173  
174  
# Line 176 | Line 176 | public class ReentrantReadWriteLockTest
176       * write-unlocking an unlocked lock throws IllegalMonitorStateException
177       */
178      public void testUnlock_IllegalMonitorStateException() {
179 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
180 <        try {
181 <            rl.writeLock().unlock();
182 <            shouldThrow();
183 <        } catch (IllegalMonitorStateException success) {}
179 >        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
180 >        try {
181 >            rl.writeLock().unlock();
182 >            shouldThrow();
183 >        } catch (IllegalMonitorStateException success) {}
184      }
185  
186  
# Line 188 | Line 188 | public class ReentrantReadWriteLockTest
188       * write-lockInterruptibly is interruptible
189       */
190      public void testWriteLockInterruptibly_Interrupted() throws Exception {
191 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
192 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
191 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
192 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
193              public void realRun() throws InterruptedException {
194                  lock.writeLock().lockInterruptibly();
195                  lock.writeLock().unlock();
# Line 210 | Line 210 | public class ReentrantReadWriteLockTest
210       * timed write-tryLock is interruptible
211       */
212      public void testWriteTryLock_Interrupted() throws InterruptedException {
213 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
214 <        lock.writeLock().lock();
215 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
213 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
214 >        lock.writeLock().lock();
215 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
216              public void realRun() throws InterruptedException {
217                  lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS);
218              }});
# Line 227 | Line 227 | public class ReentrantReadWriteLockTest
227       * read-lockInterruptibly is interruptible
228       */
229      public void testReadLockInterruptibly_Interrupted() throws InterruptedException {
230 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
231 <        lock.writeLock().lock();
232 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
230 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
231 >        lock.writeLock().lock();
232 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
233              public void realRun() throws InterruptedException {
234                  lock.readLock().lockInterruptibly();
235              }});
# Line 246 | Line 246 | public class ReentrantReadWriteLockTest
246       * timed read-tryLock is interruptible
247       */
248      public void testReadTryLock_Interrupted() throws InterruptedException {
249 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
250 <        lock.writeLock().lock();
251 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
249 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
250 >        lock.writeLock().lock();
251 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
252              public void realRun() throws InterruptedException {
253                  lock.readLock().tryLock(1000,TimeUnit.MILLISECONDS);
254              }});
# Line 263 | Line 263 | public class ReentrantReadWriteLockTest
263       * write-tryLock fails if locked
264       */
265      public void testWriteTryLockWhenLocked() throws InterruptedException {
266 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
267 <        lock.writeLock().lock();
268 <        Thread t = new Thread(new Runnable() {
266 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
267 >        lock.writeLock().lock();
268 >        Thread t = new Thread(new Runnable() {
269                  public void run() {
270                      threadAssertFalse(lock.writeLock().tryLock());
271 <                }
272 <            });
271 >                }
272 >            });
273  
274          t.start();
275          t.join();
# Line 280 | Line 280 | public class ReentrantReadWriteLockTest
280       * read-tryLock fails if locked
281       */
282      public void testReadTryLockWhenLocked() throws InterruptedException {
283 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
284 <        lock.writeLock().lock();
285 <        Thread t = new Thread(new Runnable() {
283 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
284 >        lock.writeLock().lock();
285 >        Thread t = new Thread(new Runnable() {
286                  public void run() {
287                      threadAssertFalse(lock.readLock().tryLock());
288 <                }
289 <            });
288 >                }
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() {
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 <            });
306 >                }
307 >            });
308  
309          t.start();
310          t.join();
# Line 315 | Line 315 | public class ReentrantReadWriteLockTest
315       * A writelock succeeds after reading threads unlock
316       */
317      public void testWriteAfterMultipleReadLocks() throws InterruptedException {
318 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
319 <        lock.readLock().lock();
320 <        Thread t1 = new Thread(new Runnable() {
318 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
319 >        lock.readLock().lock();
320 >        Thread t1 = new Thread(new Runnable() {
321                  public void run() {
322                      lock.readLock().lock();
323                      lock.readLock().unlock();
324 <                }
325 <            });
326 <        Thread t2 = new Thread(new Runnable() {
324 >                }
325 >            });
326 >        Thread t2 = new Thread(new Runnable() {
327                  public void run() {
328                      lock.writeLock().lock();
329                      lock.writeLock().unlock();
330 <                }
331 <            });
330 >                }
331 >            });
332  
333          t1.start();
334          t2.start();
# Line 344 | Line 344 | public class ReentrantReadWriteLockTest
344       * Readlocks succeed after a writing thread unlocks
345       */
346      public void testReadAfterWriteLock() throws InterruptedException {
347 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
348 <        lock.writeLock().lock();
349 <        Thread t1 = new Thread(new Runnable() {
347 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
348 >        lock.writeLock().lock();
349 >        Thread t1 = new Thread(new Runnable() {
350                  public void run() {
351                      lock.readLock().lock();
352                      lock.readLock().unlock();
353 <                }
354 <            });
355 <        Thread t2 = new Thread(new Runnable() {
353 >                }
354 >            });
355 >        Thread t2 = new Thread(new Runnable() {
356                  public void run() {
357                      lock.readLock().lock();
358                      lock.readLock().unlock();
359 <                }
360 <            });
359 >                }
360 >            });
361  
362          t1.start();
363          t2.start();
# Line 373 | Line 373 | public class ReentrantReadWriteLockTest
373       * Read trylock succeeds if write locked by current thread
374       */
375      public void testReadHoldingWriteLock() {
376 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
377 <        lock.writeLock().lock();
376 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
377 >        lock.writeLock().lock();
378          assertTrue(lock.readLock().tryLock());
379          lock.readLock().unlock();
380          lock.writeLock().unlock();
# Line 385 | Line 385 | public class ReentrantReadWriteLockTest
385       * other threads are waiting for readlock
386       */
387      public void testReadHoldingWriteLock2() throws InterruptedException {
388 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
389 <        lock.writeLock().lock();
390 <        Thread t1 = new Thread(new Runnable() {
388 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
389 >        lock.writeLock().lock();
390 >        Thread t1 = new Thread(new Runnable() {
391                  public void run() {
392                      lock.readLock().lock();
393                      lock.readLock().unlock();
394 <                }
395 <            });
396 <        Thread t2 = new Thread(new Runnable() {
394 >                }
395 >            });
396 >        Thread t2 = new Thread(new Runnable() {
397                  public void run() {
398                      lock.readLock().lock();
399                      lock.readLock().unlock();
400 <                }
401 <            });
400 >                }
401 >            });
402  
403          t1.start();
404          t2.start();
# Line 419 | Line 419 | public class ReentrantReadWriteLockTest
419       * other threads are waiting for writelock
420       */
421      public void testReadHoldingWriteLock3() throws InterruptedException {
422 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
423 <        lock.writeLock().lock();
424 <        Thread t1 = new Thread(new Runnable() {
422 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
423 >        lock.writeLock().lock();
424 >        Thread t1 = new Thread(new Runnable() {
425                  public void run() {
426                      lock.writeLock().lock();
427                      lock.writeLock().unlock();
428 <                }
429 <            });
430 <        Thread t2 = new Thread(new Runnable() {
428 >                }
429 >            });
430 >        Thread t2 = new Thread(new Runnable() {
431                  public void run() {
432                      lock.writeLock().lock();
433                      lock.writeLock().unlock();
434 <                }
435 <            });
434 >                }
435 >            });
436  
437          t1.start();
438          t2.start();
# Line 454 | Line 454 | public class ReentrantReadWriteLockTest
454       * other threads are waiting for writelock
455       */
456      public void testWriteHoldingWriteLock4() throws InterruptedException {
457 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
458 <        lock.writeLock().lock();
459 <        Thread t1 = new Thread(new Runnable() {
457 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
458 >        lock.writeLock().lock();
459 >        Thread t1 = new Thread(new Runnable() {
460                  public void run() {
461                      lock.writeLock().lock();
462                      lock.writeLock().unlock();
463 <                }
464 <            });
465 <        Thread t2 = new Thread(new Runnable() {
463 >                }
464 >            });
465 >        Thread t2 = new Thread(new Runnable() {
466                  public void run() {
467                      lock.writeLock().lock();
468                      lock.writeLock().unlock();
469 <                }
470 <            });
469 >                }
470 >            });
471  
472          t1.start();
473          t2.start();
# Line 488 | Line 488 | public class ReentrantReadWriteLockTest
488       * Fair Read trylock succeeds if write locked by current thread
489       */
490      public void testReadHoldingWriteLockFair() {
491 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
492 <        lock.writeLock().lock();
491 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
492 >        lock.writeLock().lock();
493          assertTrue(lock.readLock().tryLock());
494          lock.readLock().unlock();
495          lock.writeLock().unlock();
# Line 500 | Line 500 | public class ReentrantReadWriteLockTest
500       * other threads are waiting for readlock
501       */
502      public void testReadHoldingWriteLockFair2() throws InterruptedException {
503 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
504 <        lock.writeLock().lock();
505 <        Thread t1 = new Thread(new Runnable() {
503 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
504 >        lock.writeLock().lock();
505 >        Thread t1 = new Thread(new Runnable() {
506                  public void run() {
507                      lock.readLock().lock();
508                      lock.readLock().unlock();
509 <                }
510 <            });
511 <        Thread t2 = new Thread(new Runnable() {
509 >                }
510 >            });
511 >        Thread t2 = new Thread(new Runnable() {
512                  public void run() {
513                      lock.readLock().lock();
514                      lock.readLock().unlock();
515 <                }
516 <            });
515 >                }
516 >            });
517  
518          t1.start();
519          t2.start();
# Line 535 | Line 535 | public class ReentrantReadWriteLockTest
535       * other threads are waiting for writelock
536       */
537      public void testReadHoldingWriteLockFair3() throws InterruptedException {
538 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
539 <        lock.writeLock().lock();
540 <        Thread t1 = new Thread(new Runnable() {
538 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
539 >        lock.writeLock().lock();
540 >        Thread t1 = new Thread(new Runnable() {
541                  public void run() {
542                      lock.writeLock().lock();
543                      lock.writeLock().unlock();
544 <                }
545 <            });
546 <        Thread t2 = new Thread(new Runnable() {
544 >                }
545 >            });
546 >        Thread t2 = new Thread(new Runnable() {
547                  public void run() {
548                      lock.writeLock().lock();
549                      lock.writeLock().unlock();
550 <                }
551 <            });
550 >                }
551 >            });
552  
553          t1.start();
554          t2.start();
# Line 570 | Line 570 | public class ReentrantReadWriteLockTest
570       * other threads are waiting for writelock
571       */
572      public void testWriteHoldingWriteLockFair4() throws InterruptedException {
573 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
574 <        lock.writeLock().lock();
575 <        Thread t1 = new Thread(new Runnable() {
573 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
574 >        lock.writeLock().lock();
575 >        Thread t1 = new Thread(new Runnable() {
576                  public void run() {
577                      lock.writeLock().lock();
578                      lock.writeLock().unlock();
579 <                }
580 <            });
581 <        Thread t2 = new Thread(new Runnable() {
579 >                }
580 >            });
581 >        Thread t2 = new Thread(new Runnable() {
582                  public void run() {
583                      lock.writeLock().lock();
584                      lock.writeLock().unlock();
585 <                }
586 <            });
585 >                }
586 >            });
587  
588          t1.start();
589          t2.start();
# Line 607 | Line 607 | public class ReentrantReadWriteLockTest
607       * Read tryLock succeeds if readlocked but not writelocked
608       */
609      public void testTryLockWhenReadLocked() throws InterruptedException {
610 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
611 <        lock.readLock().lock();
612 <        Thread t = new Thread(new Runnable() {
610 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
611 >        lock.readLock().lock();
612 >        Thread t = new Thread(new Runnable() {
613                  public void run() {
614                      threadAssertTrue(lock.readLock().tryLock());
615                      lock.readLock().unlock();
616 <                }
617 <            });
616 >                }
617 >            });
618  
619          t.start();
620          t.join();
# Line 627 | Line 627 | public class ReentrantReadWriteLockTest
627       * write tryLock fails when readlocked
628       */
629      public void testWriteTryLockWhenReadLocked() throws InterruptedException {
630 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
631 <        lock.readLock().lock();
632 <        Thread t = new Thread(new Runnable() {
630 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
631 >        lock.readLock().lock();
632 >        Thread t = new Thread(new Runnable() {
633                  public void run() {
634                      threadAssertFalse(lock.writeLock().tryLock());
635 <                }
636 <            });
635 >                }
636 >            });
637  
638          t.start();
639          t.join();
# Line 645 | Line 645 | public class ReentrantReadWriteLockTest
645       * Fair Read tryLock succeeds if readlocked but not writelocked
646       */
647      public void testTryLockWhenReadLockedFair() throws InterruptedException {
648 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
649 <        lock.readLock().lock();
650 <        Thread t = new Thread(new Runnable() {
648 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
649 >        lock.readLock().lock();
650 >        Thread t = new Thread(new Runnable() {
651                  public void run() {
652                      threadAssertTrue(lock.readLock().tryLock());
653                      lock.readLock().unlock();
654 <                }
655 <            });
654 >                }
655 >            });
656  
657          t.start();
658          t.join();
# Line 665 | Line 665 | public class ReentrantReadWriteLockTest
665       * Fair write tryLock fails when readlocked
666       */
667      public void testWriteTryLockWhenReadLockedFair() throws InterruptedException {
668 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
669 <        lock.readLock().lock();
670 <        Thread t = new Thread(new Runnable() {
668 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
669 >        lock.readLock().lock();
670 >        Thread t = new Thread(new Runnable() {
671                  public void run() {
672                      threadAssertFalse(lock.writeLock().tryLock());
673 <                }
674 <            });
673 >                }
674 >            });
675  
676          t.start();
677          t.join();
# Line 684 | Line 684 | public class ReentrantReadWriteLockTest
684       * write timed tryLock times out if locked
685       */
686      public void testWriteTryLock_Timeout() throws InterruptedException {
687 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
688 <        lock.writeLock().lock();
689 <        Thread t = new Thread(new CheckedRunnable() {
687 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
688 >        lock.writeLock().lock();
689 >        Thread t = new Thread(new CheckedRunnable() {
690              public void realRun() throws InterruptedException {
691                  threadAssertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS));
692              }});
# Line 701 | Line 701 | public class ReentrantReadWriteLockTest
701       * read timed tryLock times out if write-locked
702       */
703      public void testReadTryLock_Timeout() throws InterruptedException {
704 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
705 <        lock.writeLock().lock();
706 <        Thread t = new Thread(new CheckedRunnable() {
704 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
705 >        lock.writeLock().lock();
706 >        Thread t = new Thread(new CheckedRunnable() {
707              public void realRun() throws InterruptedException {
708                  threadAssertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS));
709              }});
# Line 719 | Line 719 | public class ReentrantReadWriteLockTest
719       * write lockInterruptibly succeeds if lock free else is interruptible
720       */
721      public void testWriteLockInterruptibly() throws InterruptedException {
722 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
722 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
723          lock.writeLock().lockInterruptibly();
724 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
724 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
725              public void realRun() throws InterruptedException {
726                  lock.writeLock().lockInterruptibly();
727              }});
# Line 738 | Line 738 | public class ReentrantReadWriteLockTest
738       *  read lockInterruptibly succeeds if lock free else is interruptible
739       */
740      public void testReadLockInterruptibly() throws InterruptedException {
741 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
741 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
742          lock.writeLock().lockInterruptibly();
743 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
743 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
744              public void realRun() throws InterruptedException {
745                  lock.readLock().lockInterruptibly();
746              }});
# Line 756 | Line 756 | public class ReentrantReadWriteLockTest
756       * Calling await without holding lock throws IllegalMonitorStateException
757       */
758      public void testAwait_IllegalMonitor() throws InterruptedException {
759 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
759 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
760          final Condition c = lock.writeLock().newCondition();
761          try {
762              c.await();
# Line 768 | Line 768 | public class ReentrantReadWriteLockTest
768       * Calling signal without holding lock throws IllegalMonitorStateException
769       */
770      public void testSignal_IllegalMonitor() {
771 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
771 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
772          final Condition c = lock.writeLock().newCondition();
773          try {
774              c.signal();
# Line 780 | Line 780 | public class ReentrantReadWriteLockTest
780       * awaitNanos without a signal times out
781       */
782      public void testAwaitNanos_Timeout() throws InterruptedException {
783 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
783 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
784          final Condition c = lock.writeLock().newCondition();
785  
786          lock.writeLock().lock();
# Line 794 | Line 794 | public class ReentrantReadWriteLockTest
794       *  timed await without a signal times out
795       */
796      public void testAwait_Timeout() throws InterruptedException {
797 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
797 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
798          final Condition c = lock.writeLock().newCondition();
799          lock.writeLock().lock();
800          assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
# Line 805 | Line 805 | public class ReentrantReadWriteLockTest
805       * awaitUntil without a signal times out
806       */
807      public void testAwaitUntil_Timeout() throws InterruptedException {
808 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
808 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
809          final Condition c = lock.writeLock().newCondition();
810          lock.writeLock().lock();
811          java.util.Date d = new java.util.Date();
# Line 817 | Line 817 | public class ReentrantReadWriteLockTest
817       * await returns when signalled
818       */
819      public void testAwait() throws InterruptedException {
820 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
820 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
821          final Condition c = lock.writeLock().newCondition();
822 <        Thread t = new Thread(new CheckedRunnable() {
822 >        Thread t = new Thread(new CheckedRunnable() {
823              public void realRun() throws InterruptedException {
824                  lock.writeLock().lock();
825                  c.await();
# Line 894 | Line 894 | public class ReentrantReadWriteLockTest
894       * await is interruptible
895       */
896      public void testAwait_Interrupt() throws InterruptedException {
897 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
897 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
898          final Condition c = lock.writeLock().newCondition();
899 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
899 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
900              public void realRun() throws InterruptedException {
901                  lock.writeLock().lock();
902                  c.await();
# Line 914 | Line 914 | public class ReentrantReadWriteLockTest
914       * awaitNanos is interruptible
915       */
916      public void testAwaitNanos_Interrupt() throws InterruptedException {
917 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
917 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
918          final Condition c = lock.writeLock().newCondition();
919 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
919 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
920              public void realRun() throws InterruptedException {
921                  lock.writeLock().lock();
922                  c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
# Line 934 | Line 934 | public class ReentrantReadWriteLockTest
934       * awaitUntil is interruptible
935       */
936      public void testAwaitUntil_Interrupt() throws InterruptedException {
937 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
937 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
938          final Condition c = lock.writeLock().newCondition();
939 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
939 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
940              public void realRun() throws InterruptedException {
941                  lock.writeLock().lock();
942                  java.util.Date d = new java.util.Date();
# Line 955 | Line 955 | public class ReentrantReadWriteLockTest
955       * signalAll wakes up all threads
956       */
957      public void testSignalAll() throws InterruptedException {
958 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
958 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
959          final Condition c = lock.writeLock().newCondition();
960 <        Thread t1 = new Thread(new CheckedRunnable() {
960 >        Thread t1 = new Thread(new CheckedRunnable() {
961              public void realRun() throws InterruptedException {
962                  lock.writeLock().lock();
963                  c.await();
964                  lock.writeLock().unlock();
965              }});
966  
967 <        Thread t2 = new Thread(new CheckedRunnable() {
967 >        Thread t2 = new Thread(new CheckedRunnable() {
968              public void realRun() throws InterruptedException {
969                  lock.writeLock().lock();
970                  c.await();
# Line 1007 | Line 1007 | public class ReentrantReadWriteLockTest
1007       * hasQueuedThreads reports whether there are waiting threads
1008       */
1009      public void testhasQueuedThreads() throws InterruptedException {
1010 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1010 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1011          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1012          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1013          assertFalse(lock.hasQueuedThreads());
# Line 1032 | Line 1032 | public class ReentrantReadWriteLockTest
1032       * hasQueuedThread(null) throws NPE
1033       */
1034      public void testHasQueuedThreadNPE() {
1035 <        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1035 >        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1036          try {
1037              sync.hasQueuedThread(null);
1038              shouldThrow();
# Line 1043 | Line 1043 | public class ReentrantReadWriteLockTest
1043       * hasQueuedThread reports whether a thread is queued.
1044       */
1045      public void testHasQueuedThread() throws InterruptedException {
1046 <        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1046 >        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1047          Thread t1 = new Thread(new InterruptedLockRunnable(sync));
1048          Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
1049          assertFalse(sync.hasQueuedThread(t1));
# Line 1074 | Line 1074 | public class ReentrantReadWriteLockTest
1074       * getQueueLength reports number of waiting threads
1075       */
1076      public void testGetQueueLength() throws InterruptedException {
1077 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1077 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1078          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1079          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1080          assertEquals(0, lock.getQueueLength());
# Line 1099 | Line 1099 | public class ReentrantReadWriteLockTest
1099       * getQueuedThreads includes waiting threads
1100       */
1101      public void testGetQueuedThreads() throws InterruptedException {
1102 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1102 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1103          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1104          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1105          assertTrue(lock.getQueuedThreads().isEmpty());
# Line 1127 | Line 1127 | public class ReentrantReadWriteLockTest
1127       * hasWaiters throws NPE if null
1128       */
1129      public void testHasWaitersNPE() {
1130 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1130 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1131          try {
1132              lock.hasWaiters(null);
1133              shouldThrow();
# Line 1138 | Line 1138 | public class ReentrantReadWriteLockTest
1138       * getWaitQueueLength throws NPE if null
1139       */
1140      public void testGetWaitQueueLengthNPE() {
1141 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1141 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1142          try {
1143              lock.getWaitQueueLength(null);
1144              shouldThrow();
# Line 1150 | Line 1150 | public class ReentrantReadWriteLockTest
1150       * getWaitingThreads throws NPE if null
1151       */
1152      public void testGetWaitingThreadsNPE() {
1153 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1153 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1154          try {
1155              lock.getWaitingThreads(null);
1156              shouldThrow();
# Line 1161 | Line 1161 | public class ReentrantReadWriteLockTest
1161       * hasWaiters throws IAE if not owned
1162       */
1163      public void testHasWaitersIAE() {
1164 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1164 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1165          final Condition c = lock.writeLock().newCondition();
1166 <        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1166 >        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1167          try {
1168              lock2.hasWaiters(c);
1169              shouldThrow();
# Line 1174 | Line 1174 | public class ReentrantReadWriteLockTest
1174       * hasWaiters throws IMSE if not locked
1175       */
1176      public void testHasWaitersIMSE() {
1177 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1177 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1178          final Condition c = lock.writeLock().newCondition();
1179          try {
1180              lock.hasWaiters(c);
# Line 1187 | Line 1187 | public class ReentrantReadWriteLockTest
1187       * getWaitQueueLength throws IAE if not owned
1188       */
1189      public void testGetWaitQueueLengthIAE() {
1190 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1190 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1191          final Condition c = lock.writeLock().newCondition();
1192 <        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1192 >        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1193          try {
1194              lock2.getWaitQueueLength(c);
1195              shouldThrow();
# Line 1200 | Line 1200 | public class ReentrantReadWriteLockTest
1200       * getWaitQueueLength throws IMSE if not locked
1201       */
1202      public void testGetWaitQueueLengthIMSE() {
1203 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1203 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1204          final Condition c = lock.writeLock().newCondition();
1205          try {
1206              lock.getWaitQueueLength(c);
# Line 1213 | Line 1213 | public class ReentrantReadWriteLockTest
1213       * getWaitingThreads throws IAE if not owned
1214       */
1215      public void testGetWaitingThreadsIAE() {
1216 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1216 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1217          final Condition c = lock.writeLock().newCondition();
1218 <        final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock();
1218 >        final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock();
1219          try {
1220              lock2.getWaitingThreads(c);
1221              shouldThrow();
# Line 1226 | Line 1226 | public class ReentrantReadWriteLockTest
1226       * getWaitingThreads throws IMSE if not locked
1227       */
1228      public void testGetWaitingThreadsIMSE() {
1229 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1229 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1230          final Condition c = lock.writeLock().newCondition();
1231          try {
1232              lock.getWaitingThreads(c);
# Line 1239 | Line 1239 | public class ReentrantReadWriteLockTest
1239       * hasWaiters returns true when a thread is waiting, else false
1240       */
1241      public void testHasWaiters() throws InterruptedException {
1242 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1242 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1243          final Condition c = lock.writeLock().newCondition();
1244 <        Thread t = new Thread(new CheckedRunnable() {
1244 >        Thread t = new Thread(new CheckedRunnable() {
1245              public void realRun() throws InterruptedException {
1246                  lock.writeLock().lock();
1247                  threadAssertFalse(lock.hasWaiters(c));
# Line 1270 | Line 1270 | public class ReentrantReadWriteLockTest
1270       * getWaitQueueLength returns number of waiting threads
1271       */
1272      public void testGetWaitQueueLength() throws InterruptedException {
1273 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1273 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1274          final Condition c = lock.writeLock().newCondition();
1275 <        Thread t = new Thread(new CheckedRunnable() {
1275 >        Thread t = new Thread(new CheckedRunnable() {
1276              public void realRun() throws InterruptedException {
1277                  lock.writeLock().lock();
1278                  threadAssertFalse(lock.hasWaiters(c));
# Line 1302 | Line 1302 | public class ReentrantReadWriteLockTest
1302       * getWaitingThreads returns only and all waiting threads
1303       */
1304      public void testGetWaitingThreads() throws InterruptedException {
1305 <        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1305 >        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1306          final Condition c = lock.writeLock().newCondition();
1307 <        Thread t1 = new Thread(new CheckedRunnable() {
1307 >        Thread t1 = new Thread(new CheckedRunnable() {
1308              public void realRun() throws InterruptedException {
1309                  lock.writeLock().lock();
1310                  threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
# Line 1312 | Line 1312 | public class ReentrantReadWriteLockTest
1312                  lock.writeLock().unlock();
1313              }});
1314  
1315 <        Thread t2 = new Thread(new CheckedRunnable() {
1315 >        Thread t2 = new Thread(new CheckedRunnable() {
1316              public void realRun() throws InterruptedException {
1317                  lock.writeLock().lock();
1318                  threadAssertFalse(lock.getWaitingThreads(c).isEmpty());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines