156 |
|
*/ |
157 |
|
public void testWriteLockInterruptibly_Interrupted() { |
158 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
159 |
– |
lock.writeLock().lock(); |
159 |
|
Thread t = new Thread(new Runnable() { |
160 |
|
public void run() { |
161 |
|
try { |
162 |
|
lock.writeLock().lockInterruptibly(); |
163 |
< |
threadShouldThrow(); |
163 |
> |
lock.writeLock().unlock(); |
164 |
> |
lock.writeLock().lockInterruptibly(); |
165 |
> |
lock.writeLock().unlock(); |
166 |
|
} catch(InterruptedException success){} |
167 |
|
} |
168 |
|
}); |
169 |
|
try { |
170 |
+ |
lock.writeLock().lock(); |
171 |
|
t.start(); |
172 |
|
t.interrupt(); |
173 |
|
lock.writeLock().unlock(); |
187 |
|
public void run() { |
188 |
|
try { |
189 |
|
lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS); |
188 |
– |
threadShouldThrow(); |
190 |
|
} catch(InterruptedException success){} |
191 |
|
} |
192 |
|
}); |
210 |
|
public void run() { |
211 |
|
try { |
212 |
|
lock.readLock().lockInterruptibly(); |
212 |
– |
threadShouldThrow(); |
213 |
|
} catch(InterruptedException success){} |
214 |
|
} |
215 |
|
}); |
376 |
|
} |
377 |
|
} |
378 |
|
|
379 |
+ |
/** |
380 |
+ |
* Read trylock succeeds if write locked by current thread |
381 |
+ |
*/ |
382 |
+ |
public void testReadHoldingWriteLock() { |
383 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
384 |
+ |
lock.writeLock().lock(); |
385 |
+ |
assertTrue(lock.readLock().tryLock()); |
386 |
+ |
lock.readLock().unlock(); |
387 |
+ |
lock.writeLock().unlock(); |
388 |
+ |
} |
389 |
+ |
|
390 |
+ |
/** |
391 |
+ |
* Read lock succeeds if write locked by current thread even if |
392 |
+ |
* other threads are waiting |
393 |
+ |
*/ |
394 |
+ |
public void testReadHoldingWriteLock2() { |
395 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
396 |
+ |
lock.writeLock().lock(); |
397 |
+ |
Thread t1 = new Thread(new Runnable() { |
398 |
+ |
public void run() { |
399 |
+ |
lock.readLock().lock(); |
400 |
+ |
lock.readLock().unlock(); |
401 |
+ |
} |
402 |
+ |
}); |
403 |
+ |
Thread t2 = new Thread(new Runnable() { |
404 |
+ |
public void run() { |
405 |
+ |
lock.readLock().lock(); |
406 |
+ |
lock.readLock().unlock(); |
407 |
+ |
} |
408 |
+ |
}); |
409 |
+ |
|
410 |
+ |
try { |
411 |
+ |
t1.start(); |
412 |
+ |
t2.start(); |
413 |
+ |
lock.readLock().lock(); |
414 |
+ |
lock.readLock().unlock(); |
415 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
416 |
+ |
lock.readLock().lock(); |
417 |
+ |
lock.readLock().unlock(); |
418 |
+ |
lock.writeLock().unlock(); |
419 |
+ |
t1.join(MEDIUM_DELAY_MS); |
420 |
+ |
t2.join(MEDIUM_DELAY_MS); |
421 |
+ |
assertTrue(!t1.isAlive()); |
422 |
+ |
assertTrue(!t2.isAlive()); |
423 |
+ |
|
424 |
+ |
} catch(Exception e){ |
425 |
+ |
unexpectedException(); |
426 |
+ |
} |
427 |
+ |
} |
428 |
+ |
|
429 |
+ |
/** |
430 |
+ |
* Fair Read trylock succeeds if write locked by current thread |
431 |
+ |
*/ |
432 |
+ |
public void testReadHoldingWriteLockFair() { |
433 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
434 |
+ |
lock.writeLock().lock(); |
435 |
+ |
assertTrue(lock.readLock().tryLock()); |
436 |
+ |
lock.readLock().unlock(); |
437 |
+ |
lock.writeLock().unlock(); |
438 |
+ |
} |
439 |
+ |
|
440 |
+ |
/** |
441 |
+ |
* Fair Read lock succeeds if write locked by current thread even if |
442 |
+ |
* other threads are waiting |
443 |
+ |
*/ |
444 |
+ |
public void testReadHoldingWriteLockFair2() { |
445 |
+ |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
446 |
+ |
lock.writeLock().lock(); |
447 |
+ |
Thread t1 = new Thread(new Runnable() { |
448 |
+ |
public void run() { |
449 |
+ |
lock.readLock().lock(); |
450 |
+ |
lock.readLock().unlock(); |
451 |
+ |
} |
452 |
+ |
}); |
453 |
+ |
Thread t2 = new Thread(new Runnable() { |
454 |
+ |
public void run() { |
455 |
+ |
lock.readLock().lock(); |
456 |
+ |
lock.readLock().unlock(); |
457 |
+ |
} |
458 |
+ |
}); |
459 |
+ |
|
460 |
+ |
try { |
461 |
+ |
t1.start(); |
462 |
+ |
t2.start(); |
463 |
+ |
lock.readLock().lock(); |
464 |
+ |
lock.readLock().unlock(); |
465 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
466 |
+ |
lock.readLock().lock(); |
467 |
+ |
lock.readLock().unlock(); |
468 |
+ |
lock.writeLock().unlock(); |
469 |
+ |
t1.join(MEDIUM_DELAY_MS); |
470 |
+ |
t2.join(MEDIUM_DELAY_MS); |
471 |
+ |
assertTrue(!t1.isAlive()); |
472 |
+ |
assertTrue(!t2.isAlive()); |
473 |
+ |
|
474 |
+ |
} catch(Exception e){ |
475 |
+ |
unexpectedException(); |
476 |
+ |
} |
477 |
+ |
} |
478 |
+ |
|
479 |
|
|
480 |
|
/** |
481 |
|
* Read tryLock succeeds if readlocked but not writelocked |
691 |
|
final Condition c = lock.writeLock().newCondition(); |
692 |
|
try { |
693 |
|
lock.writeLock().lock(); |
594 |
– |
assertFalse(c.await(10, TimeUnit.MILLISECONDS)); |
694 |
|
lock.writeLock().unlock(); |
695 |
|
} |
696 |
|
catch (Exception ex) { |
707 |
|
try { |
708 |
|
lock.writeLock().lock(); |
709 |
|
java.util.Date d = new java.util.Date(); |
611 |
– |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
710 |
|
lock.writeLock().unlock(); |
711 |
|
} |
712 |
|
catch (Exception ex) { |
747 |
|
} |
748 |
|
} |
749 |
|
|
750 |
+ |
/** A helper class for uninterruptible wait tests */ |
751 |
+ |
class UninterruptableThread extends Thread { |
752 |
+ |
private Lock lock; |
753 |
+ |
private Condition c; |
754 |
+ |
|
755 |
+ |
public volatile boolean canAwake = false; |
756 |
+ |
public volatile boolean interrupted = false; |
757 |
+ |
public volatile boolean lockStarted = false; |
758 |
+ |
|
759 |
+ |
public UninterruptableThread(Lock lock, Condition c) { |
760 |
+ |
this.lock = lock; |
761 |
+ |
this.c = c; |
762 |
+ |
} |
763 |
+ |
|
764 |
+ |
public synchronized void run() { |
765 |
+ |
lock.lock(); |
766 |
+ |
lockStarted = true; |
767 |
+ |
|
768 |
+ |
while (!canAwake) { |
769 |
+ |
c.awaitUninterruptibly(); |
770 |
+ |
} |
771 |
+ |
|
772 |
+ |
interrupted = isInterrupted(); |
773 |
+ |
lock.unlock(); |
774 |
+ |
} |
775 |
+ |
} |
776 |
+ |
|
777 |
|
/** |
778 |
|
* awaitUninterruptibly doesn't abort on interrupt |
779 |
|
*/ |
780 |
|
public void testAwaitUninterruptibly() { |
781 |
< |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
781 |
> |
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
782 |
|
final Condition c = lock.writeLock().newCondition(); |
783 |
< |
Thread t = new Thread(new Runnable() { |
659 |
< |
public void run() { |
660 |
< |
lock.writeLock().lock(); |
661 |
< |
c.awaitUninterruptibly(); |
662 |
< |
lock.writeLock().unlock(); |
663 |
< |
} |
664 |
< |
}); |
783 |
> |
UninterruptableThread thread = new UninterruptableThread(lock.writeLock(), c); |
784 |
|
|
785 |
|
try { |
786 |
< |
t.start(); |
787 |
< |
Thread.sleep(SHORT_DELAY_MS); |
788 |
< |
t.interrupt(); |
786 |
> |
thread.start(); |
787 |
> |
|
788 |
> |
while (!thread.lockStarted) { |
789 |
> |
Thread.sleep(100); |
790 |
> |
} |
791 |
> |
|
792 |
|
lock.writeLock().lock(); |
793 |
< |
c.signal(); |
794 |
< |
lock.writeLock().unlock(); |
795 |
< |
assert(t.isInterrupted()); |
796 |
< |
t.join(SHORT_DELAY_MS); |
797 |
< |
assertFalse(t.isAlive()); |
798 |
< |
} |
799 |
< |
catch (Exception ex) { |
793 |
> |
try { |
794 |
> |
thread.interrupt(); |
795 |
> |
thread.canAwake = true; |
796 |
> |
c.signal(); |
797 |
> |
} finally { |
798 |
> |
lock.writeLock().unlock(); |
799 |
> |
} |
800 |
> |
|
801 |
> |
thread.join(); |
802 |
> |
assertTrue(thread.interrupted); |
803 |
> |
assertFalse(thread.isAlive()); |
804 |
> |
} catch (Exception ex) { |
805 |
|
unexpectedException(); |
806 |
|
} |
807 |
|
} |
1004 |
|
} |
1005 |
|
|
1006 |
|
/** |
1007 |
+ |
* hasQueuedThread(null) throws NPE |
1008 |
+ |
*/ |
1009 |
+ |
public void testHasQueuedThreadNPE() { |
1010 |
+ |
final ReentrantReadWriteLock sync = new ReentrantReadWriteLock(); |
1011 |
+ |
try { |
1012 |
+ |
sync.hasQueuedThread(null); |
1013 |
+ |
shouldThrow(); |
1014 |
+ |
} catch (NullPointerException success) { |
1015 |
+ |
} |
1016 |
+ |
} |
1017 |
+ |
|
1018 |
+ |
/** |
1019 |
+ |
* hasQueuedThread reports whether a thread is queued. |
1020 |
+ |
*/ |
1021 |
+ |
public void testHasQueuedThread() { |
1022 |
+ |
final ReentrantReadWriteLock sync = new ReentrantReadWriteLock(); |
1023 |
+ |
Thread t1 = new Thread(new InterruptedLockRunnable(sync)); |
1024 |
+ |
Thread t2 = new Thread(new InterruptibleLockRunnable(sync)); |
1025 |
+ |
try { |
1026 |
+ |
assertFalse(sync.hasQueuedThread(t1)); |
1027 |
+ |
assertFalse(sync.hasQueuedThread(t2)); |
1028 |
+ |
sync.writeLock().lock(); |
1029 |
+ |
t1.start(); |
1030 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
1031 |
+ |
assertTrue(sync.hasQueuedThread(t1)); |
1032 |
+ |
t2.start(); |
1033 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
1034 |
+ |
assertTrue(sync.hasQueuedThread(t1)); |
1035 |
+ |
assertTrue(sync.hasQueuedThread(t2)); |
1036 |
+ |
t1.interrupt(); |
1037 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
1038 |
+ |
assertFalse(sync.hasQueuedThread(t1)); |
1039 |
+ |
assertTrue(sync.hasQueuedThread(t2)); |
1040 |
+ |
sync.writeLock().unlock(); |
1041 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
1042 |
+ |
assertFalse(sync.hasQueuedThread(t1)); |
1043 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
1044 |
+ |
assertFalse(sync.hasQueuedThread(t2)); |
1045 |
+ |
t1.join(); |
1046 |
+ |
t2.join(); |
1047 |
+ |
} catch(Exception e){ |
1048 |
+ |
unexpectedException(); |
1049 |
+ |
} |
1050 |
+ |
} |
1051 |
+ |
|
1052 |
+ |
|
1053 |
+ |
/** |
1054 |
|
* getQueueLength reports number of waiting threads |
1055 |
|
*/ |
1056 |
|
public void testGetQueueLength() { |
1398 |
|
} |
1399 |
|
} |
1400 |
|
|
1401 |
+ |
/** |
1402 |
+ |
* toString indicates current lock state |
1403 |
+ |
*/ |
1404 |
+ |
public void testToString() { |
1405 |
+ |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1406 |
+ |
String us = lock.toString(); |
1407 |
+ |
assertTrue(us.indexOf("Write locks = 0") >= 0); |
1408 |
+ |
assertTrue(us.indexOf("Read locks = 0") >= 0); |
1409 |
+ |
lock.writeLock().lock(); |
1410 |
+ |
String ws = lock.toString(); |
1411 |
+ |
assertTrue(ws.indexOf("Write locks = 1") >= 0); |
1412 |
+ |
assertTrue(ws.indexOf("Read locks = 0") >= 0); |
1413 |
+ |
lock.writeLock().unlock(); |
1414 |
+ |
lock.readLock().lock(); |
1415 |
+ |
lock.readLock().lock(); |
1416 |
+ |
String rs = lock.toString(); |
1417 |
+ |
assertTrue(rs.indexOf("Write locks = 0") >= 0); |
1418 |
+ |
assertTrue(rs.indexOf("Read locks = 2") >= 0); |
1419 |
+ |
} |
1420 |
+ |
|
1421 |
+ |
/** |
1422 |
+ |
* readLock.toString indicates current lock state |
1423 |
+ |
*/ |
1424 |
+ |
public void testReadLockToString() { |
1425 |
+ |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1426 |
+ |
String us = lock.readLock().toString(); |
1427 |
+ |
assertTrue(us.indexOf("Read locks = 0") >= 0); |
1428 |
+ |
lock.readLock().lock(); |
1429 |
+ |
lock.readLock().lock(); |
1430 |
+ |
String rs = lock.readLock().toString(); |
1431 |
+ |
assertTrue(rs.indexOf("Read locks = 2") >= 0); |
1432 |
+ |
} |
1433 |
+ |
|
1434 |
+ |
/** |
1435 |
+ |
* writeLock.toString indicates current lock state |
1436 |
+ |
*/ |
1437 |
+ |
public void testWriteLockToString() { |
1438 |
+ |
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1439 |
+ |
String us = lock.writeLock().toString(); |
1440 |
+ |
assertTrue(us.indexOf("Unlocked") >= 0); |
1441 |
+ |
lock.writeLock().lock(); |
1442 |
+ |
String ls = lock.writeLock().toString(); |
1443 |
+ |
assertTrue(ls.indexOf("Locked") >= 0); |
1444 |
+ |
} |
1445 |
+ |
|
1446 |
|
} |