1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
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); |
59 |
|
} |
60 |
|
|
61 |
|
/** |
62 |
+ |
* Releases lock, checking that it had a hold count of 1. |
63 |
+ |
*/ |
64 |
+ |
void releaseLock(ReentrantReadWriteLock.WriteLock lock) { |
65 |
+ |
assertTrue(lock.isHeldByCurrentThread()); |
66 |
+ |
lock.unlock(); |
67 |
+ |
assertFalse(lock.isHeldByCurrentThread()); |
68 |
+ |
} |
69 |
+ |
|
70 |
+ |
/** |
71 |
|
* Constructor sets given fairness, and is in unlocked state |
72 |
|
*/ |
73 |
|
public void testConstructor() { |
199 |
|
*/ |
200 |
|
public void testWriteLockInterruptibly_Interrupted() throws Exception { |
201 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
202 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
202 |
> |
lock.writeLock().lock(); |
203 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
204 |
|
public void realRun() throws InterruptedException { |
205 |
|
lock.writeLock().lockInterruptibly(); |
196 |
– |
lock.writeLock().unlock(); |
197 |
– |
lock.writeLock().lockInterruptibly(); |
198 |
– |
lock.writeLock().unlock(); |
206 |
|
}}); |
207 |
|
|
201 |
– |
lock.writeLock().lock(); |
202 |
– |
t.start(); |
208 |
|
Thread.sleep(SHORT_DELAY_MS); |
209 |
|
t.interrupt(); |
205 |
– |
Thread.sleep(SHORT_DELAY_MS); |
206 |
– |
lock.writeLock().unlock(); |
210 |
|
t.join(); |
211 |
+ |
releaseLock(lock.writeLock()); |
212 |
|
} |
213 |
|
|
214 |
|
/** |
217 |
|
public void testWriteTryLock_Interrupted() throws InterruptedException { |
218 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
219 |
|
lock.writeLock().lock(); |
220 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
220 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
221 |
|
public void realRun() throws InterruptedException { |
222 |
|
lock.writeLock().tryLock(SMALL_DELAY_MS, MILLISECONDS); |
223 |
|
}}); |
224 |
|
|
221 |
– |
t.start(); |
225 |
|
Thread.sleep(SHORT_DELAY_MS); |
226 |
|
t.interrupt(); |
224 |
– |
lock.writeLock().unlock(); |
227 |
|
t.join(); |
228 |
+ |
releaseLock(lock.writeLock()); |
229 |
|
} |
230 |
|
|
231 |
|
/** |
234 |
|
public void testReadLockInterruptibly_Interrupted() throws InterruptedException { |
235 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
236 |
|
lock.writeLock().lock(); |
237 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
237 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
238 |
|
public void realRun() throws InterruptedException { |
239 |
|
lock.readLock().lockInterruptibly(); |
240 |
|
}}); |
241 |
|
|
239 |
– |
t.start(); |
242 |
|
Thread.sleep(SHORT_DELAY_MS); |
243 |
|
t.interrupt(); |
242 |
– |
Thread.sleep(SHORT_DELAY_MS); |
243 |
– |
lock.writeLock().unlock(); |
244 |
|
t.join(); |
245 |
+ |
releaseLock(lock.writeLock()); |
246 |
|
} |
247 |
|
|
248 |
|
/** |
251 |
|
public void testReadTryLock_Interrupted() throws InterruptedException { |
252 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
253 |
|
lock.writeLock().lock(); |
254 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
254 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
255 |
|
public void realRun() throws InterruptedException { |
256 |
|
lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS); |
257 |
|
}}); |
258 |
|
|
258 |
– |
t.start(); |
259 |
|
Thread.sleep(SHORT_DELAY_MS); |
260 |
|
t.interrupt(); |
261 |
|
t.join(); |
268 |
|
public void testWriteTryLockWhenLocked() throws InterruptedException { |
269 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
270 |
|
lock.writeLock().lock(); |
271 |
< |
Thread t = new Thread(new CheckedRunnable() { |
271 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
272 |
|
public void realRun() { |
273 |
< |
threadAssertFalse(lock.writeLock().tryLock()); |
273 |
> |
assertFalse(lock.writeLock().tryLock()); |
274 |
|
}}); |
275 |
|
|
276 |
– |
t.start(); |
276 |
|
t.join(); |
277 |
|
lock.writeLock().unlock(); |
278 |
|
} |
283 |
|
public void testReadTryLockWhenLocked() throws InterruptedException { |
284 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
285 |
|
lock.writeLock().lock(); |
286 |
< |
Thread t = new Thread(new CheckedRunnable() { |
286 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
287 |
|
public void realRun() { |
288 |
< |
threadAssertFalse(lock.readLock().tryLock()); |
288 |
> |
assertFalse(lock.readLock().tryLock()); |
289 |
|
}}); |
290 |
|
|
292 |
– |
t.start(); |
291 |
|
t.join(); |
292 |
|
lock.writeLock().unlock(); |
293 |
|
} |
298 |
|
public void testMultipleReadLocks() throws InterruptedException { |
299 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
300 |
|
lock.readLock().lock(); |
301 |
< |
Thread t = new Thread(new CheckedRunnable() { |
301 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
302 |
|
public void realRun() { |
303 |
< |
threadAssertTrue(lock.readLock().tryLock()); |
303 |
> |
assertTrue(lock.readLock().tryLock()); |
304 |
|
lock.readLock().unlock(); |
305 |
|
}}); |
306 |
|
|
309 |
– |
t.start(); |
307 |
|
t.join(); |
308 |
|
lock.readLock().unlock(); |
309 |
|
} |
314 |
|
public void testWriteAfterMultipleReadLocks() throws InterruptedException { |
315 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
316 |
|
lock.readLock().lock(); |
317 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
317 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
318 |
|
public void realRun() { |
319 |
|
lock.readLock().lock(); |
320 |
|
lock.readLock().unlock(); |
321 |
|
}}); |
322 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
322 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
323 |
|
public void realRun() { |
324 |
|
lock.writeLock().lock(); |
325 |
|
lock.writeLock().unlock(); |
326 |
|
}}); |
327 |
|
|
331 |
– |
t1.start(); |
332 |
– |
t2.start(); |
328 |
|
Thread.sleep(SHORT_DELAY_MS); |
329 |
|
lock.readLock().unlock(); |
330 |
|
t1.join(MEDIUM_DELAY_MS); |
339 |
|
public void testReadAfterWriteLock() throws InterruptedException { |
340 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
341 |
|
lock.writeLock().lock(); |
342 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
342 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
343 |
|
public void realRun() { |
344 |
|
lock.readLock().lock(); |
345 |
|
lock.readLock().unlock(); |
346 |
|
}}); |
347 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
347 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
348 |
|
public void realRun() { |
349 |
|
lock.readLock().lock(); |
350 |
|
lock.readLock().unlock(); |
351 |
|
}}); |
352 |
|
|
358 |
– |
t1.start(); |
359 |
– |
t2.start(); |
353 |
|
Thread.sleep(SHORT_DELAY_MS); |
354 |
|
lock.writeLock().unlock(); |
355 |
|
t1.join(MEDIUM_DELAY_MS); |
376 |
|
public void testReadHoldingWriteLock2() throws InterruptedException { |
377 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
378 |
|
lock.writeLock().lock(); |
379 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
379 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
380 |
|
public void realRun() { |
381 |
|
lock.readLock().lock(); |
382 |
|
lock.readLock().unlock(); |
383 |
|
}}); |
384 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
384 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
385 |
|
public void realRun() { |
386 |
|
lock.readLock().lock(); |
387 |
|
lock.readLock().unlock(); |
388 |
|
}}); |
389 |
|
|
397 |
– |
t1.start(); |
398 |
– |
t2.start(); |
390 |
|
lock.readLock().lock(); |
391 |
|
lock.readLock().unlock(); |
392 |
|
Thread.sleep(SHORT_DELAY_MS); |
400 |
|
} |
401 |
|
|
402 |
|
/** |
403 |
< |
* Read lock succeeds if write locked by current thread even if |
403 |
> |
* Read lock succeeds if write locked by current thread even if |
404 |
|
* other threads are waiting for writelock |
405 |
|
*/ |
406 |
|
public void testReadHoldingWriteLock3() throws InterruptedException { |
407 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
408 |
|
lock.writeLock().lock(); |
409 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
409 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
410 |
|
public void realRun() { |
411 |
|
lock.writeLock().lock(); |
412 |
|
lock.writeLock().unlock(); |
413 |
|
}}); |
414 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
414 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
415 |
|
public void realRun() { |
416 |
|
lock.writeLock().lock(); |
417 |
|
lock.writeLock().unlock(); |
418 |
|
}}); |
419 |
|
|
429 |
– |
t1.start(); |
430 |
– |
t2.start(); |
420 |
|
lock.readLock().lock(); |
421 |
|
lock.readLock().unlock(); |
422 |
|
Thread.sleep(SHORT_DELAY_MS); |
431 |
|
|
432 |
|
|
433 |
|
/** |
434 |
< |
* Write lock succeeds if write locked by current thread even if |
434 |
> |
* Write lock succeeds if write locked by current thread even if |
435 |
|
* other threads are waiting for writelock |
436 |
|
*/ |
437 |
|
public void testWriteHoldingWriteLock4() throws InterruptedException { |
438 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
439 |
|
lock.writeLock().lock(); |
440 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
440 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
441 |
|
public void realRun() { |
442 |
|
lock.writeLock().lock(); |
443 |
|
lock.writeLock().unlock(); |
444 |
|
}}); |
445 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
445 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
446 |
|
public void realRun() { |
447 |
|
lock.writeLock().lock(); |
448 |
|
lock.writeLock().unlock(); |
449 |
|
}}); |
450 |
|
|
462 |
– |
t1.start(); |
463 |
– |
t2.start(); |
451 |
|
lock.writeLock().lock(); |
452 |
|
lock.writeLock().unlock(); |
453 |
|
Thread.sleep(SHORT_DELAY_MS); |
479 |
|
public void testReadHoldingWriteLockFair2() throws InterruptedException { |
480 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
481 |
|
lock.writeLock().lock(); |
482 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
482 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
483 |
|
public void realRun() { |
484 |
|
lock.readLock().lock(); |
485 |
|
lock.readLock().unlock(); |
486 |
|
}}); |
487 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
487 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
488 |
|
public void realRun() { |
489 |
|
lock.readLock().lock(); |
490 |
|
lock.readLock().unlock(); |
491 |
|
}}); |
492 |
|
|
506 |
– |
t1.start(); |
507 |
– |
t2.start(); |
493 |
|
lock.readLock().lock(); |
494 |
|
lock.readLock().unlock(); |
495 |
|
Thread.sleep(SHORT_DELAY_MS); |
510 |
|
public void testReadHoldingWriteLockFair3() throws InterruptedException { |
511 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
512 |
|
lock.writeLock().lock(); |
513 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
513 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
514 |
|
public void realRun() { |
515 |
|
lock.writeLock().lock(); |
516 |
|
lock.writeLock().unlock(); |
517 |
|
}}); |
518 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
518 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
519 |
|
public void realRun() { |
520 |
|
lock.writeLock().lock(); |
521 |
|
lock.writeLock().unlock(); |
522 |
|
}}); |
523 |
|
|
539 |
– |
t1.start(); |
540 |
– |
t2.start(); |
524 |
|
lock.readLock().lock(); |
525 |
|
lock.readLock().unlock(); |
526 |
|
Thread.sleep(SHORT_DELAY_MS); |
541 |
|
public void testWriteHoldingWriteLockFair4() throws InterruptedException { |
542 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
543 |
|
lock.writeLock().lock(); |
544 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
544 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
545 |
|
public void realRun() { |
546 |
|
lock.writeLock().lock(); |
547 |
|
lock.writeLock().unlock(); |
548 |
|
}}); |
549 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
549 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
550 |
|
public void realRun() { |
551 |
|
lock.writeLock().lock(); |
552 |
|
lock.writeLock().unlock(); |
553 |
|
}}); |
554 |
|
|
572 |
– |
t1.start(); |
573 |
– |
t2.start(); |
555 |
|
Thread.sleep(SHORT_DELAY_MS); |
556 |
|
assertTrue(lock.isWriteLockedByCurrentThread()); |
557 |
< |
assertTrue(lock.getWriteHoldCount() == 1); |
557 |
> |
assertEquals(1, lock.getWriteHoldCount()); |
558 |
|
lock.writeLock().lock(); |
559 |
< |
assertTrue(lock.getWriteHoldCount() == 2); |
559 |
> |
assertEquals(2, lock.getWriteHoldCount()); |
560 |
|
lock.writeLock().unlock(); |
561 |
|
lock.writeLock().lock(); |
562 |
|
lock.writeLock().unlock(); |
574 |
|
public void testTryLockWhenReadLocked() throws InterruptedException { |
575 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
576 |
|
lock.readLock().lock(); |
577 |
< |
Thread t = new Thread(new CheckedRunnable() { |
577 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
578 |
|
public void realRun() { |
579 |
< |
threadAssertTrue(lock.readLock().tryLock()); |
579 |
> |
assertTrue(lock.readLock().tryLock()); |
580 |
|
lock.readLock().unlock(); |
581 |
|
}}); |
582 |
|
|
602 |
– |
t.start(); |
583 |
|
t.join(); |
584 |
|
lock.readLock().unlock(); |
585 |
|
} |
586 |
|
|
607 |
– |
|
608 |
– |
|
587 |
|
/** |
588 |
|
* write tryLock fails when readlocked |
589 |
|
*/ |
590 |
|
public void testWriteTryLockWhenReadLocked() throws InterruptedException { |
591 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
592 |
|
lock.readLock().lock(); |
593 |
< |
Thread t = new Thread(new CheckedRunnable() { |
593 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
594 |
|
public void realRun() { |
595 |
< |
threadAssertFalse(lock.writeLock().tryLock()); |
595 |
> |
assertFalse(lock.writeLock().tryLock()); |
596 |
|
}}); |
597 |
|
|
620 |
– |
t.start(); |
598 |
|
t.join(); |
599 |
|
lock.readLock().unlock(); |
600 |
|
} |
606 |
|
public void testTryLockWhenReadLockedFair() throws InterruptedException { |
607 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
608 |
|
lock.readLock().lock(); |
609 |
< |
Thread t = new Thread(new CheckedRunnable() { |
609 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
610 |
|
public void realRun() { |
611 |
< |
threadAssertTrue(lock.readLock().tryLock()); |
611 |
> |
assertTrue(lock.readLock().tryLock()); |
612 |
|
lock.readLock().unlock(); |
613 |
|
}}); |
614 |
|
|
638 |
– |
t.start(); |
615 |
|
t.join(); |
616 |
|
lock.readLock().unlock(); |
617 |
|
} |
624 |
|
public void testWriteTryLockWhenReadLockedFair() throws InterruptedException { |
625 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
626 |
|
lock.readLock().lock(); |
627 |
< |
Thread t = new Thread(new CheckedRunnable() { |
627 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
628 |
|
public void realRun() { |
629 |
< |
threadAssertFalse(lock.writeLock().tryLock()); |
629 |
> |
assertFalse(lock.writeLock().tryLock()); |
630 |
|
}}); |
631 |
|
|
656 |
– |
t.start(); |
632 |
|
t.join(); |
633 |
|
lock.readLock().unlock(); |
634 |
|
} |
641 |
|
public void testWriteTryLock_Timeout() throws InterruptedException { |
642 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
643 |
|
lock.writeLock().lock(); |
644 |
< |
Thread t = new Thread(new CheckedRunnable() { |
644 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
645 |
|
public void realRun() throws InterruptedException { |
646 |
< |
threadAssertFalse(lock.writeLock().tryLock(1, MILLISECONDS)); |
646 |
> |
assertFalse(lock.writeLock().tryLock(1, MILLISECONDS)); |
647 |
|
}}); |
648 |
|
|
674 |
– |
t.start(); |
649 |
|
t.join(); |
650 |
|
assertTrue(lock.writeLock().isHeldByCurrentThread()); |
651 |
|
lock.writeLock().unlock(); |
657 |
|
public void testReadTryLock_Timeout() throws InterruptedException { |
658 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
659 |
|
lock.writeLock().lock(); |
660 |
< |
Thread t = new Thread(new CheckedRunnable() { |
660 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
661 |
|
public void realRun() throws InterruptedException { |
662 |
< |
threadAssertFalse(lock.readLock().tryLock(1, MILLISECONDS)); |
662 |
> |
assertFalse(lock.readLock().tryLock(1, MILLISECONDS)); |
663 |
|
}}); |
664 |
|
|
691 |
– |
t.start(); |
665 |
|
t.join(); |
666 |
|
assertTrue(lock.writeLock().isHeldByCurrentThread()); |
667 |
|
lock.writeLock().unlock(); |
674 |
|
public void testWriteLockInterruptibly() throws InterruptedException { |
675 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
676 |
|
lock.writeLock().lockInterruptibly(); |
677 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
677 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
678 |
|
public void realRun() throws InterruptedException { |
679 |
|
lock.writeLock().lockInterruptibly(); |
680 |
|
}}); |
681 |
|
|
709 |
– |
t.start(); |
682 |
|
Thread.sleep(SHORT_DELAY_MS); |
683 |
|
t.interrupt(); |
684 |
|
Thread.sleep(SHORT_DELAY_MS); |
687 |
|
} |
688 |
|
|
689 |
|
/** |
690 |
< |
* read lockInterruptibly succeeds if lock free else is interruptible |
690 |
> |
* read lockInterruptibly succeeds if lock free else is interruptible |
691 |
|
*/ |
692 |
|
public void testReadLockInterruptibly() throws InterruptedException { |
693 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
694 |
|
lock.writeLock().lockInterruptibly(); |
695 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
695 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
696 |
|
public void realRun() throws InterruptedException { |
697 |
|
lock.readLock().lockInterruptibly(); |
698 |
|
}}); |
699 |
|
|
728 |
– |
t.start(); |
700 |
|
Thread.sleep(SHORT_DELAY_MS); |
701 |
|
t.interrupt(); |
702 |
|
t.join(); |
742 |
|
|
743 |
|
|
744 |
|
/** |
745 |
< |
* timed await without a signal times out |
745 |
> |
* timed await without a signal times out |
746 |
|
*/ |
747 |
|
public void testAwait_Timeout() throws InterruptedException { |
748 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
770 |
|
public void testAwait() throws InterruptedException { |
771 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
772 |
|
final Condition c = lock.writeLock().newCondition(); |
773 |
< |
Thread t = new Thread(new CheckedRunnable() { |
773 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
774 |
|
public void realRun() throws InterruptedException { |
775 |
|
lock.writeLock().lock(); |
776 |
|
c.await(); |
777 |
|
lock.writeLock().unlock(); |
778 |
|
}}); |
779 |
|
|
809 |
– |
t.start(); |
780 |
|
Thread.sleep(SHORT_DELAY_MS); |
781 |
|
lock.writeLock().lock(); |
782 |
|
c.signal(); |
846 |
|
public void testAwait_Interrupt() throws InterruptedException { |
847 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
848 |
|
final Condition c = lock.writeLock().newCondition(); |
849 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
849 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
850 |
|
public void realRun() throws InterruptedException { |
851 |
|
lock.writeLock().lock(); |
852 |
|
c.await(); |
853 |
|
lock.writeLock().unlock(); |
854 |
|
}}); |
855 |
|
|
886 |
– |
t.start(); |
856 |
|
Thread.sleep(SHORT_DELAY_MS); |
857 |
|
t.interrupt(); |
858 |
|
t.join(SHORT_DELAY_MS); |
865 |
|
public void testAwaitNanos_Interrupt() throws InterruptedException { |
866 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
867 |
|
final Condition c = lock.writeLock().newCondition(); |
868 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
868 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
869 |
|
public void realRun() throws InterruptedException { |
870 |
|
lock.writeLock().lock(); |
871 |
< |
c.awaitNanos(LONG_DELAY_MS * 1000L * 1000L); |
871 |
> |
c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); |
872 |
|
lock.writeLock().unlock(); |
873 |
|
}}); |
874 |
|
|
906 |
– |
t.start(); |
875 |
|
Thread.sleep(SHORT_DELAY_MS); |
876 |
|
t.interrupt(); |
877 |
|
t.join(SHORT_DELAY_MS); |
884 |
|
public void testAwaitUntil_Interrupt() throws InterruptedException { |
885 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
886 |
|
final Condition c = lock.writeLock().newCondition(); |
887 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
887 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
888 |
|
public void realRun() throws InterruptedException { |
889 |
|
lock.writeLock().lock(); |
890 |
|
java.util.Date d = new java.util.Date(); |
892 |
|
lock.writeLock().unlock(); |
893 |
|
}}); |
894 |
|
|
927 |
– |
t.start(); |
895 |
|
Thread.sleep(SHORT_DELAY_MS); |
896 |
|
t.interrupt(); |
897 |
|
t.join(SHORT_DELAY_MS); |
904 |
|
public void testSignalAll() throws InterruptedException { |
905 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
906 |
|
final Condition c = lock.writeLock().newCondition(); |
907 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
907 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
908 |
|
public void realRun() throws InterruptedException { |
909 |
|
lock.writeLock().lock(); |
910 |
|
c.await(); |
911 |
|
lock.writeLock().unlock(); |
912 |
|
}}); |
913 |
|
|
914 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
914 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
915 |
|
public void realRun() throws InterruptedException { |
916 |
|
lock.writeLock().lock(); |
917 |
|
c.await(); |
918 |
|
lock.writeLock().unlock(); |
919 |
|
}}); |
920 |
|
|
954 |
– |
t1.start(); |
955 |
– |
t2.start(); |
921 |
|
Thread.sleep(SHORT_DELAY_MS); |
922 |
|
lock.writeLock().lock(); |
923 |
|
c.signalAll(); |
1186 |
|
public void testHasWaiters() throws InterruptedException { |
1187 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1188 |
|
final Condition c = lock.writeLock().newCondition(); |
1189 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1189 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1190 |
|
public void realRun() throws InterruptedException { |
1191 |
|
lock.writeLock().lock(); |
1192 |
< |
threadAssertFalse(lock.hasWaiters(c)); |
1193 |
< |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
1192 |
> |
assertFalse(lock.hasWaiters(c)); |
1193 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
1194 |
|
c.await(); |
1195 |
|
lock.writeLock().unlock(); |
1196 |
|
}}); |
1197 |
|
|
1233 |
– |
t.start(); |
1198 |
|
Thread.sleep(SHORT_DELAY_MS); |
1199 |
|
lock.writeLock().lock(); |
1200 |
|
assertTrue(lock.hasWaiters(c)); |
1216 |
|
public void testGetWaitQueueLength() throws InterruptedException { |
1217 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1218 |
|
final Condition c = lock.writeLock().newCondition(); |
1219 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1219 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1220 |
|
public void realRun() throws InterruptedException { |
1221 |
|
lock.writeLock().lock(); |
1222 |
< |
threadAssertFalse(lock.hasWaiters(c)); |
1223 |
< |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
1222 |
> |
assertFalse(lock.hasWaiters(c)); |
1223 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
1224 |
|
c.await(); |
1225 |
|
lock.writeLock().unlock(); |
1226 |
|
}}); |
1227 |
|
|
1264 |
– |
t.start(); |
1228 |
|
Thread.sleep(SHORT_DELAY_MS); |
1229 |
|
lock.writeLock().lock(); |
1230 |
|
assertTrue(lock.hasWaiters(c)); |
1250 |
|
Thread t1 = new Thread(new CheckedRunnable() { |
1251 |
|
public void realRun() throws InterruptedException { |
1252 |
|
lock.writeLock().lock(); |
1253 |
< |
threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); |
1253 |
> |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
1254 |
|
c.await(); |
1255 |
|
lock.writeLock().unlock(); |
1256 |
|
}}); |
1258 |
|
Thread t2 = new Thread(new CheckedRunnable() { |
1259 |
|
public void realRun() throws InterruptedException { |
1260 |
|
lock.writeLock().lock(); |
1261 |
< |
threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); |
1261 |
> |
assertFalse(lock.getWaitingThreads(c).isEmpty()); |
1262 |
|
c.await(); |
1263 |
|
lock.writeLock().unlock(); |
1264 |
|
}}); |