126 |
|
/** |
127 |
|
* getWriteHoldCount returns number of recursive holds |
128 |
|
*/ |
129 |
< |
public void testGetHoldCount() { |
129 |
> |
public void testGetWriteHoldCount() { |
130 |
|
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
131 |
|
for(int i = 1; i <= SIZE; i++) { |
132 |
|
lock.writeLock().lock(); |
137 |
|
assertEquals(i-1,lock.getWriteHoldCount()); |
138 |
|
} |
139 |
|
} |
140 |
+ |
|
141 |
+ |
/** |
142 |
+ |
* getReadHoldCount returns number of recursive holds |
143 |
+ |
*/ |
144 |
+ |
public void testGetReadHoldCount() { |
145 |
+ |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
146 |
+ |
for(int i = 1; i <= SIZE; i++) { |
147 |
+ |
lock.readLock().lock(); |
148 |
+ |
assertEquals(i,lock.getReadHoldCount()); |
149 |
+ |
} |
150 |
+ |
for(int i = SIZE; i > 0; i--) { |
151 |
+ |
lock.readLock().unlock(); |
152 |
+ |
assertEquals(i-1,lock.getReadHoldCount()); |
153 |
+ |
} |
154 |
+ |
} |
155 |
|
|
156 |
|
|
157 |
|
/** |
404 |
|
|
405 |
|
/** |
406 |
|
* Read lock succeeds if write locked by current thread even if |
407 |
< |
* other threads are waiting |
407 |
> |
* other threads are waiting for readlock |
408 |
|
*/ |
409 |
|
public void testReadHoldingWriteLock2() { |
410 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
442 |
|
} |
443 |
|
|
444 |
|
/** |
445 |
+ |
* Read lock succeeds if write locked by current thread even if |
446 |
+ |
* other threads are waiting for writelock |
447 |
+ |
*/ |
448 |
+ |
public void testReadHoldingWriteLock3() { |
449 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
450 |
+ |
lock.writeLock().lock(); |
451 |
+ |
Thread t1 = new Thread(new Runnable() { |
452 |
+ |
public void run() { |
453 |
+ |
lock.writeLock().lock(); |
454 |
+ |
lock.writeLock().unlock(); |
455 |
+ |
} |
456 |
+ |
}); |
457 |
+ |
Thread t2 = new Thread(new Runnable() { |
458 |
+ |
public void run() { |
459 |
+ |
lock.writeLock().lock(); |
460 |
+ |
lock.writeLock().unlock(); |
461 |
+ |
} |
462 |
+ |
}); |
463 |
+ |
|
464 |
+ |
try { |
465 |
+ |
t1.start(); |
466 |
+ |
t2.start(); |
467 |
+ |
lock.readLock().lock(); |
468 |
+ |
lock.readLock().unlock(); |
469 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
470 |
+ |
lock.readLock().lock(); |
471 |
+ |
lock.readLock().unlock(); |
472 |
+ |
lock.writeLock().unlock(); |
473 |
+ |
t1.join(MEDIUM_DELAY_MS); |
474 |
+ |
t2.join(MEDIUM_DELAY_MS); |
475 |
+ |
assertTrue(!t1.isAlive()); |
476 |
+ |
assertTrue(!t2.isAlive()); |
477 |
+ |
|
478 |
+ |
} catch(Exception e){ |
479 |
+ |
unexpectedException(); |
480 |
+ |
} |
481 |
+ |
} |
482 |
+ |
|
483 |
+ |
|
484 |
+ |
/** |
485 |
+ |
* Write lock succeeds if write locked by current thread even if |
486 |
+ |
* other threads are waiting for writelock |
487 |
+ |
*/ |
488 |
+ |
public void testWriteHoldingWriteLock4() { |
489 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
490 |
+ |
lock.writeLock().lock(); |
491 |
+ |
Thread t1 = new Thread(new Runnable() { |
492 |
+ |
public void run() { |
493 |
+ |
lock.writeLock().lock(); |
494 |
+ |
lock.writeLock().unlock(); |
495 |
+ |
} |
496 |
+ |
}); |
497 |
+ |
Thread t2 = new Thread(new Runnable() { |
498 |
+ |
public void run() { |
499 |
+ |
lock.writeLock().lock(); |
500 |
+ |
lock.writeLock().unlock(); |
501 |
+ |
} |
502 |
+ |
}); |
503 |
+ |
|
504 |
+ |
try { |
505 |
+ |
t1.start(); |
506 |
+ |
t2.start(); |
507 |
+ |
lock.writeLock().lock(); |
508 |
+ |
lock.writeLock().unlock(); |
509 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
510 |
+ |
lock.writeLock().lock(); |
511 |
+ |
lock.writeLock().unlock(); |
512 |
+ |
lock.writeLock().unlock(); |
513 |
+ |
t1.join(MEDIUM_DELAY_MS); |
514 |
+ |
t2.join(MEDIUM_DELAY_MS); |
515 |
+ |
assertTrue(!t1.isAlive()); |
516 |
+ |
assertTrue(!t2.isAlive()); |
517 |
+ |
|
518 |
+ |
} catch(Exception e){ |
519 |
+ |
unexpectedException(); |
520 |
+ |
} |
521 |
+ |
} |
522 |
+ |
|
523 |
+ |
|
524 |
+ |
/** |
525 |
|
* Fair Read trylock succeeds if write locked by current thread |
526 |
|
*/ |
527 |
|
public void testReadHoldingWriteLockFair() { |
534 |
|
|
535 |
|
/** |
536 |
|
* Fair Read lock succeeds if write locked by current thread even if |
537 |
< |
* other threads are waiting |
537 |
> |
* other threads are waiting for readlock |
538 |
|
*/ |
539 |
|
public void testReadHoldingWriteLockFair2() { |
540 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
563 |
|
lock.writeLock().unlock(); |
564 |
|
t1.join(MEDIUM_DELAY_MS); |
565 |
|
t2.join(MEDIUM_DELAY_MS); |
566 |
+ |
assertTrue(!t1.isAlive()); |
567 |
+ |
assertTrue(!t2.isAlive()); |
568 |
+ |
|
569 |
+ |
} catch(Exception e){ |
570 |
+ |
unexpectedException(); |
571 |
+ |
} |
572 |
+ |
} |
573 |
+ |
|
574 |
+ |
|
575 |
+ |
/** |
576 |
+ |
* Fair Read lock succeeds if write locked by current thread even if |
577 |
+ |
* other threads are waiting for writelock |
578 |
+ |
*/ |
579 |
+ |
public void testReadHoldingWriteLockFair3() { |
580 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
581 |
+ |
lock.writeLock().lock(); |
582 |
+ |
Thread t1 = new Thread(new Runnable() { |
583 |
+ |
public void run() { |
584 |
+ |
lock.writeLock().lock(); |
585 |
+ |
lock.writeLock().unlock(); |
586 |
+ |
} |
587 |
+ |
}); |
588 |
+ |
Thread t2 = new Thread(new Runnable() { |
589 |
+ |
public void run() { |
590 |
+ |
lock.writeLock().lock(); |
591 |
+ |
lock.writeLock().unlock(); |
592 |
+ |
} |
593 |
+ |
}); |
594 |
+ |
|
595 |
+ |
try { |
596 |
+ |
t1.start(); |
597 |
+ |
t2.start(); |
598 |
+ |
lock.readLock().lock(); |
599 |
+ |
lock.readLock().unlock(); |
600 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
601 |
+ |
lock.readLock().lock(); |
602 |
+ |
lock.readLock().unlock(); |
603 |
+ |
lock.writeLock().unlock(); |
604 |
+ |
t1.join(MEDIUM_DELAY_MS); |
605 |
+ |
t2.join(MEDIUM_DELAY_MS); |
606 |
+ |
assertTrue(!t1.isAlive()); |
607 |
+ |
assertTrue(!t2.isAlive()); |
608 |
+ |
|
609 |
+ |
} catch(Exception e){ |
610 |
+ |
unexpectedException(); |
611 |
+ |
} |
612 |
+ |
} |
613 |
+ |
|
614 |
+ |
|
615 |
+ |
/** |
616 |
+ |
* Fair Write lock succeeds if write locked by current thread even if |
617 |
+ |
* other threads are waiting for writelock |
618 |
+ |
*/ |
619 |
+ |
public void testWriteHoldingWriteLockFair4() { |
620 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
621 |
+ |
lock.writeLock().lock(); |
622 |
+ |
Thread t1 = new Thread(new Runnable() { |
623 |
+ |
public void run() { |
624 |
+ |
lock.writeLock().lock(); |
625 |
+ |
lock.writeLock().unlock(); |
626 |
+ |
} |
627 |
+ |
}); |
628 |
+ |
Thread t2 = new Thread(new Runnable() { |
629 |
+ |
public void run() { |
630 |
+ |
lock.writeLock().lock(); |
631 |
+ |
lock.writeLock().unlock(); |
632 |
+ |
} |
633 |
+ |
}); |
634 |
+ |
|
635 |
+ |
try { |
636 |
+ |
t1.start(); |
637 |
+ |
t2.start(); |
638 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
639 |
+ |
assertTrue(lock.isWriteLockedByCurrentThread()); |
640 |
+ |
assertTrue(lock.getWriteHoldCount() == 1); |
641 |
+ |
lock.writeLock().lock(); |
642 |
+ |
assertTrue(lock.getWriteHoldCount() == 2); |
643 |
+ |
lock.writeLock().unlock(); |
644 |
+ |
lock.writeLock().lock(); |
645 |
+ |
lock.writeLock().unlock(); |
646 |
+ |
lock.writeLock().unlock(); |
647 |
+ |
t1.join(MEDIUM_DELAY_MS); |
648 |
+ |
t2.join(MEDIUM_DELAY_MS); |
649 |
|
assertTrue(!t1.isAlive()); |
650 |
|
assertTrue(!t2.isAlive()); |
651 |
|
|