736 |
|
public void testHasWaiters(boolean fair) { |
737 |
|
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
738 |
|
final Condition c = lock.newCondition(); |
739 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
739 |
> |
final CountDownLatch pleaseSignal = new CountDownLatch(1); |
740 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
741 |
|
public void realRun() throws InterruptedException { |
742 |
|
lock.lock(); |
743 |
|
assertHasNoWaiters(lock, c); |
744 |
|
assertFalse(lock.hasWaiters(c)); |
745 |
< |
locked.countDown(); |
745 |
> |
pleaseSignal.countDown(); |
746 |
|
c.await(); |
747 |
|
assertHasNoWaiters(lock, c); |
748 |
|
assertFalse(lock.hasWaiters(c)); |
749 |
|
lock.unlock(); |
750 |
|
}}); |
751 |
|
|
752 |
< |
await(locked); |
752 |
> |
await(pleaseSignal); |
753 |
|
lock.lock(); |
754 |
|
assertHasWaiters(lock, c, t); |
755 |
|
assertTrue(lock.hasWaiters(c)); |
880 |
|
} |
881 |
|
|
882 |
|
/** |
883 |
< |
* awaitUninterruptibly doesn't abort on interrupt |
883 |
> |
* awaitUninterruptibly is uninterruptible |
884 |
|
*/ |
885 |
|
public void testAwaitUninterruptibly() { testAwaitUninterruptibly(false); } |
886 |
|
public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); } |
887 |
|
public void testAwaitUninterruptibly(boolean fair) { |
888 |
|
final ReentrantLock lock = new ReentrantLock(fair); |
889 |
|
final Condition c = lock.newCondition(); |
890 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
891 |
< |
Thread t = newStartedThread(new CheckedRunnable() { |
890 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(2); |
891 |
> |
|
892 |
> |
Thread t1 = newStartedThread(new CheckedRunnable() { |
893 |
> |
public void realRun() { |
894 |
> |
// Interrupt before awaitUninterruptibly |
895 |
> |
lock.lock(); |
896 |
> |
pleaseInterrupt.countDown(); |
897 |
> |
Thread.currentThread().interrupt(); |
898 |
> |
c.awaitUninterruptibly(); |
899 |
> |
assertTrue(Thread.interrupted()); |
900 |
> |
lock.unlock(); |
901 |
> |
}}); |
902 |
> |
|
903 |
> |
Thread t2 = newStartedThread(new CheckedRunnable() { |
904 |
|
public void realRun() { |
905 |
+ |
// Interrupt during awaitUninterruptibly |
906 |
|
lock.lock(); |
907 |
< |
locked.countDown(); |
907 |
> |
pleaseInterrupt.countDown(); |
908 |
|
c.awaitUninterruptibly(); |
909 |
|
assertTrue(Thread.interrupted()); |
910 |
|
lock.unlock(); |
911 |
|
}}); |
912 |
|
|
913 |
< |
await(locked); |
913 |
> |
await(pleaseInterrupt); |
914 |
|
lock.lock(); |
915 |
|
lock.unlock(); |
916 |
< |
t.interrupt(); |
917 |
< |
long timeoutMillis = 10; |
918 |
< |
assertThreadStaysAlive(t, timeoutMillis); |
916 |
> |
t2.interrupt(); |
917 |
> |
|
918 |
> |
assertThreadStaysAlive(t1); |
919 |
> |
assertTrue(t2.isAlive()); |
920 |
> |
|
921 |
|
lock.lock(); |
922 |
< |
c.signal(); |
922 |
> |
c.signalAll(); |
923 |
|
lock.unlock(); |
924 |
< |
awaitTermination(t); |
924 |
> |
|
925 |
> |
awaitTermination(t1); |
926 |
> |
awaitTermination(t2); |
927 |
|
} |
928 |
|
|
929 |
|
/** |
941 |
|
final PublicReentrantLock lock = |
942 |
|
new PublicReentrantLock(fair); |
943 |
|
final Condition c = lock.newCondition(); |
944 |
< |
final CountDownLatch locked = new CountDownLatch(1); |
944 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
945 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
946 |
|
public void realRun() throws InterruptedException { |
947 |
|
lock.lock(); |
948 |
|
assertLockedByMoi(lock); |
949 |
|
assertHasNoWaiters(lock, c); |
950 |
< |
locked.countDown(); |
950 |
> |
pleaseInterrupt.countDown(); |
951 |
|
try { |
952 |
|
await(c, awaitMethod); |
953 |
|
} finally { |
958 |
|
} |
959 |
|
}}); |
960 |
|
|
961 |
< |
await(locked); |
961 |
> |
await(pleaseInterrupt); |
962 |
|
assertHasWaiters(lock, c, t); |
963 |
|
t.interrupt(); |
964 |
|
awaitTermination(t); |
979 |
|
public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) { |
980 |
|
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
981 |
|
final Condition c = lock.newCondition(); |
982 |
< |
final CountDownLatch locked = new CountDownLatch(2); |
982 |
> |
final CountDownLatch pleaseSignal = new CountDownLatch(2); |
983 |
|
class Awaiter extends CheckedRunnable { |
984 |
|
public void realRun() throws InterruptedException { |
985 |
|
lock.lock(); |
986 |
< |
locked.countDown(); |
986 |
> |
pleaseSignal.countDown(); |
987 |
|
await(c, awaitMethod); |
988 |
|
lock.unlock(); |
989 |
|
} |
992 |
|
Thread t1 = newStartedThread(new Awaiter()); |
993 |
|
Thread t2 = newStartedThread(new Awaiter()); |
994 |
|
|
995 |
< |
await(locked); |
995 |
> |
await(pleaseSignal); |
996 |
|
lock.lock(); |
997 |
|
assertHasWaiters(lock, c, t1, t2); |
998 |
|
c.signalAll(); |
1057 |
|
public void testAwaitLockCount(boolean fair) { |
1058 |
|
final PublicReentrantLock lock = new PublicReentrantLock(fair); |
1059 |
|
final Condition c = lock.newCondition(); |
1060 |
< |
final CountDownLatch locked = new CountDownLatch(2); |
1060 |
> |
final CountDownLatch pleaseSignal = new CountDownLatch(2); |
1061 |
|
Thread t1 = newStartedThread(new CheckedRunnable() { |
1062 |
|
public void realRun() throws InterruptedException { |
1063 |
|
lock.lock(); |
1064 |
|
assertLockedByMoi(lock); |
1065 |
|
assertEquals(1, lock.getHoldCount()); |
1066 |
< |
locked.countDown(); |
1066 |
> |
pleaseSignal.countDown(); |
1067 |
|
c.await(); |
1068 |
|
assertLockedByMoi(lock); |
1069 |
|
assertEquals(1, lock.getHoldCount()); |
1076 |
|
lock.lock(); |
1077 |
|
assertLockedByMoi(lock); |
1078 |
|
assertEquals(2, lock.getHoldCount()); |
1079 |
< |
locked.countDown(); |
1079 |
> |
pleaseSignal.countDown(); |
1080 |
|
c.await(); |
1081 |
|
assertLockedByMoi(lock); |
1082 |
|
assertEquals(2, lock.getHoldCount()); |
1084 |
|
lock.unlock(); |
1085 |
|
}}); |
1086 |
|
|
1087 |
< |
await(locked); |
1087 |
> |
await(pleaseSignal); |
1088 |
|
lock.lock(); |
1089 |
|
assertHasWaiters(lock, c, t1, t2); |
1090 |
|
assertEquals(1, lock.getHoldCount()); |