189 |
|
*/ |
190 |
|
public void testWriteLockInterruptibly_Interrupted() throws Exception { |
191 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
192 |
< |
Thread t = new Thread(new Runnable() { |
193 |
< |
public void run() { |
194 |
< |
try { |
195 |
< |
lock.writeLock().lockInterruptibly(); |
196 |
< |
lock.writeLock().unlock(); |
197 |
< |
lock.writeLock().lockInterruptibly(); |
198 |
< |
lock.writeLock().unlock(); |
199 |
< |
} catch (InterruptedException success) {} |
200 |
< |
} |
201 |
< |
}); |
192 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
193 |
> |
public void realRun() throws InterruptedException { |
194 |
> |
lock.writeLock().lockInterruptibly(); |
195 |
> |
lock.writeLock().unlock(); |
196 |
> |
lock.writeLock().lockInterruptibly(); |
197 |
> |
lock.writeLock().unlock(); |
198 |
> |
}}); |
199 |
|
|
200 |
|
lock.writeLock().lock(); |
201 |
|
t.start(); |
212 |
|
public void testWriteTryLock_Interrupted() throws InterruptedException { |
213 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
214 |
|
lock.writeLock().lock(); |
215 |
< |
Thread t = new Thread(new Runnable() { |
216 |
< |
public void run() { |
217 |
< |
try { |
218 |
< |
lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS); |
222 |
< |
} catch (InterruptedException success) {} |
223 |
< |
} |
224 |
< |
}); |
215 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
216 |
> |
public void realRun() throws InterruptedException { |
217 |
> |
lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS); |
218 |
> |
}}); |
219 |
|
|
220 |
|
t.start(); |
221 |
|
t.interrupt(); |
229 |
|
public void testReadLockInterruptibly_Interrupted() throws InterruptedException { |
230 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
231 |
|
lock.writeLock().lock(); |
232 |
< |
Thread t = new Thread(new Runnable() { |
233 |
< |
public void run() { |
234 |
< |
try { |
235 |
< |
lock.readLock().lockInterruptibly(); |
242 |
< |
} catch (InterruptedException success) {} |
243 |
< |
} |
244 |
< |
}); |
232 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
233 |
> |
public void realRun() throws InterruptedException { |
234 |
> |
lock.readLock().lockInterruptibly(); |
235 |
> |
}}); |
236 |
|
|
237 |
|
t.start(); |
238 |
|
Thread.sleep(SHORT_DELAY_MS); |
248 |
|
public void testReadTryLock_Interrupted() throws InterruptedException { |
249 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
250 |
|
lock.writeLock().lock(); |
251 |
< |
Thread t = new Thread(new Runnable() { |
252 |
< |
public void run() { |
253 |
< |
try { |
254 |
< |
lock.readLock().tryLock(1000,TimeUnit.MILLISECONDS); |
264 |
< |
threadShouldThrow(); |
265 |
< |
} catch (InterruptedException success) {} |
266 |
< |
} |
267 |
< |
}); |
251 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
252 |
> |
public void realRun() throws InterruptedException { |
253 |
> |
lock.readLock().tryLock(1000,TimeUnit.MILLISECONDS); |
254 |
> |
}}); |
255 |
|
|
256 |
|
t.start(); |
257 |
|
t.interrupt(); |
686 |
|
public void testWriteTryLock_Timeout() throws InterruptedException { |
687 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
688 |
|
lock.writeLock().lock(); |
689 |
< |
Thread t = new Thread(new Runnable() { |
690 |
< |
public void run() { |
691 |
< |
try { |
692 |
< |
threadAssertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS)); |
706 |
< |
} catch (Exception ex) { |
707 |
< |
threadUnexpectedException(); |
708 |
< |
} |
709 |
< |
} |
710 |
< |
}); |
689 |
> |
Thread t = new Thread(new CheckedRunnable() { |
690 |
> |
public void realRun() throws InterruptedException { |
691 |
> |
threadAssertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS)); |
692 |
> |
}}); |
693 |
|
|
694 |
|
t.start(); |
695 |
|
t.join(); |
696 |
+ |
assertTrue(lock.writeLock().isHeldByCurrentThread()); |
697 |
|
lock.writeLock().unlock(); |
698 |
|
} |
699 |
|
|
703 |
|
public void testReadTryLock_Timeout() throws InterruptedException { |
704 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
705 |
|
lock.writeLock().lock(); |
706 |
< |
Thread t = new Thread(new Runnable() { |
707 |
< |
public void run() { |
708 |
< |
try { |
709 |
< |
threadAssertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS)); |
727 |
< |
} catch (Exception ex) { |
728 |
< |
threadUnexpectedException(); |
729 |
< |
} |
730 |
< |
} |
731 |
< |
}); |
706 |
> |
Thread t = new Thread(new CheckedRunnable() { |
707 |
> |
public void realRun() throws InterruptedException { |
708 |
> |
threadAssertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS)); |
709 |
> |
}}); |
710 |
|
|
711 |
|
t.start(); |
712 |
|
t.join(); |
713 |
+ |
assertTrue(lock.writeLock().isHeldByCurrentThread()); |
714 |
|
lock.writeLock().unlock(); |
715 |
|
} |
716 |
|
|
721 |
|
public void testWriteLockInterruptibly() throws InterruptedException { |
722 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
723 |
|
lock.writeLock().lockInterruptibly(); |
724 |
< |
Thread t = new Thread(new Runnable() { |
725 |
< |
public void run() { |
726 |
< |
try { |
727 |
< |
lock.writeLock().lockInterruptibly(); |
749 |
< |
threadShouldThrow(); |
750 |
< |
} |
751 |
< |
catch (InterruptedException success) { |
752 |
< |
} |
753 |
< |
} |
754 |
< |
}); |
724 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
725 |
> |
public void realRun() throws InterruptedException { |
726 |
> |
lock.writeLock().lockInterruptibly(); |
727 |
> |
}}); |
728 |
|
|
729 |
|
t.start(); |
730 |
|
Thread.sleep(SHORT_DELAY_MS); |
740 |
|
public void testReadLockInterruptibly() throws InterruptedException { |
741 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
742 |
|
lock.writeLock().lockInterruptibly(); |
743 |
< |
Thread t = new Thread(new Runnable() { |
744 |
< |
public void run() { |
745 |
< |
try { |
746 |
< |
lock.readLock().lockInterruptibly(); |
774 |
< |
threadShouldThrow(); |
775 |
< |
} |
776 |
< |
catch (InterruptedException success) { |
777 |
< |
} |
778 |
< |
} |
779 |
< |
}); |
743 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
744 |
> |
public void realRun() throws InterruptedException { |
745 |
> |
lock.readLock().lockInterruptibly(); |
746 |
> |
}}); |
747 |
|
|
748 |
|
t.start(); |
749 |
|
Thread.sleep(SHORT_DELAY_MS); |
793 |
|
/** |
794 |
|
* timed await without a signal times out |
795 |
|
*/ |
796 |
< |
public void testAwait_Timeout() { |
796 |
> |
public void testAwait_Timeout() throws InterruptedException { |
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)); |
801 |
|
lock.writeLock().unlock(); |
802 |
|
} |
803 |
|
|
804 |
|
/** |
805 |
|
* awaitUntil without a signal times out |
806 |
|
*/ |
807 |
< |
public void testAwaitUntil_Timeout() { |
807 |
> |
public void testAwaitUntil_Timeout() throws InterruptedException { |
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(); |
812 |
+ |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
813 |
|
lock.writeLock().unlock(); |
814 |
|
} |
815 |
|
|
819 |
|
public void testAwait() throws InterruptedException { |
820 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
821 |
|
final Condition c = lock.writeLock().newCondition(); |
822 |
< |
Thread t = new Thread(new Runnable() { |
823 |
< |
public void run() { |
824 |
< |
try { |
825 |
< |
lock.writeLock().lock(); |
826 |
< |
c.await(); |
827 |
< |
lock.writeLock().unlock(); |
859 |
< |
} |
860 |
< |
catch (InterruptedException e) { |
861 |
< |
threadUnexpectedException(); |
862 |
< |
} |
863 |
< |
} |
864 |
< |
}); |
822 |
> |
Thread t = new Thread(new CheckedRunnable() { |
823 |
> |
public void realRun() throws InterruptedException { |
824 |
> |
lock.writeLock().lock(); |
825 |
> |
c.await(); |
826 |
> |
lock.writeLock().unlock(); |
827 |
> |
}}); |
828 |
|
|
829 |
|
t.start(); |
830 |
|
Thread.sleep(SHORT_DELAY_MS); |
896 |
|
public void testAwait_Interrupt() throws InterruptedException { |
897 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
898 |
|
final Condition c = lock.writeLock().newCondition(); |
899 |
< |
Thread t = new Thread(new Runnable() { |
900 |
< |
public void run() { |
901 |
< |
try { |
902 |
< |
lock.writeLock().lock(); |
903 |
< |
c.await(); |
904 |
< |
lock.writeLock().unlock(); |
942 |
< |
threadShouldThrow(); |
943 |
< |
} |
944 |
< |
catch (InterruptedException success) { |
945 |
< |
} |
946 |
< |
} |
947 |
< |
}); |
899 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
900 |
> |
public void realRun() throws InterruptedException { |
901 |
> |
lock.writeLock().lock(); |
902 |
> |
c.await(); |
903 |
> |
lock.writeLock().unlock(); |
904 |
> |
}}); |
905 |
|
|
906 |
|
t.start(); |
907 |
|
Thread.sleep(SHORT_DELAY_MS); |
916 |
|
public void testAwaitNanos_Interrupt() throws InterruptedException { |
917 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
918 |
|
final Condition c = lock.writeLock().newCondition(); |
919 |
< |
Thread t = new Thread(new Runnable() { |
920 |
< |
public void run() { |
921 |
< |
try { |
922 |
< |
lock.writeLock().lock(); |
923 |
< |
c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000); |
924 |
< |
lock.writeLock().unlock(); |
968 |
< |
threadShouldThrow(); |
969 |
< |
} |
970 |
< |
catch (InterruptedException success) { |
971 |
< |
} |
972 |
< |
} |
973 |
< |
}); |
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); |
923 |
> |
lock.writeLock().unlock(); |
924 |
> |
}}); |
925 |
|
|
926 |
|
t.start(); |
927 |
|
Thread.sleep(SHORT_DELAY_MS); |
936 |
|
public void testAwaitUntil_Interrupt() throws InterruptedException { |
937 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
938 |
|
final Condition c = lock.writeLock().newCondition(); |
939 |
< |
Thread t = new Thread(new Runnable() { |
940 |
< |
public void run() { |
941 |
< |
try { |
942 |
< |
lock.writeLock().lock(); |
943 |
< |
java.util.Date d = new java.util.Date(); |
944 |
< |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
945 |
< |
lock.writeLock().unlock(); |
995 |
< |
threadShouldThrow(); |
996 |
< |
} |
997 |
< |
catch (InterruptedException success) { |
998 |
< |
} |
999 |
< |
} |
1000 |
< |
}); |
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(); |
943 |
> |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
944 |
> |
lock.writeLock().unlock(); |
945 |
> |
}}); |
946 |
|
|
947 |
|
t.start(); |
948 |
|
Thread.sleep(SHORT_DELAY_MS); |
957 |
|
public void testSignalAll() throws InterruptedException { |
958 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
959 |
|
final Condition c = lock.writeLock().newCondition(); |
960 |
< |
Thread t1 = new Thread(new Runnable() { |
961 |
< |
public void run() { |
962 |
< |
try { |
963 |
< |
lock.writeLock().lock(); |
964 |
< |
c.await(); |
965 |
< |
lock.writeLock().unlock(); |
966 |
< |
} |
967 |
< |
catch (InterruptedException e) { |
968 |
< |
threadUnexpectedException(); |
969 |
< |
} |
970 |
< |
} |
971 |
< |
}); |
972 |
< |
|
1028 |
< |
Thread t2 = new Thread(new Runnable() { |
1029 |
< |
public void run() { |
1030 |
< |
try { |
1031 |
< |
lock.writeLock().lock(); |
1032 |
< |
c.await(); |
1033 |
< |
lock.writeLock().unlock(); |
1034 |
< |
} |
1035 |
< |
catch (InterruptedException e) { |
1036 |
< |
threadUnexpectedException(); |
1037 |
< |
} |
1038 |
< |
} |
1039 |
< |
}); |
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() { |
968 |
> |
public void realRun() throws InterruptedException { |
969 |
> |
lock.writeLock().lock(); |
970 |
> |
c.await(); |
971 |
> |
lock.writeLock().unlock(); |
972 |
> |
}}); |
973 |
|
|
974 |
|
t1.start(); |
975 |
|
t2.start(); |
1036 |
|
try { |
1037 |
|
sync.hasQueuedThread(null); |
1038 |
|
shouldThrow(); |
1039 |
< |
} catch (NullPointerException success) { |
1107 |
< |
} |
1039 |
> |
} catch (NullPointerException success) {} |
1040 |
|
} |
1041 |
|
|
1042 |
|
/** |
1241 |
|
public void testHasWaiters() throws InterruptedException { |
1242 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1243 |
|
final Condition c = lock.writeLock().newCondition(); |
1244 |
< |
Thread t = new Thread(new Runnable() { |
1245 |
< |
public void run() { |
1246 |
< |
try { |
1247 |
< |
lock.writeLock().lock(); |
1248 |
< |
threadAssertFalse(lock.hasWaiters(c)); |
1249 |
< |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
1250 |
< |
c.await(); |
1251 |
< |
lock.writeLock().unlock(); |
1320 |
< |
} |
1321 |
< |
catch (InterruptedException e) { |
1322 |
< |
threadUnexpectedException(); |
1323 |
< |
} |
1324 |
< |
} |
1325 |
< |
}); |
1244 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1245 |
> |
public void realRun() throws InterruptedException { |
1246 |
> |
lock.writeLock().lock(); |
1247 |
> |
threadAssertFalse(lock.hasWaiters(c)); |
1248 |
> |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
1249 |
> |
c.await(); |
1250 |
> |
lock.writeLock().unlock(); |
1251 |
> |
}}); |
1252 |
|
|
1253 |
|
t.start(); |
1254 |
|
Thread.sleep(SHORT_DELAY_MS); |
1272 |
|
public void testGetWaitQueueLength() throws InterruptedException { |
1273 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
1274 |
|
final Condition c = lock.writeLock().newCondition(); |
1275 |
< |
Thread t = new Thread(new Runnable() { |
1276 |
< |
public void run() { |
1277 |
< |
try { |
1278 |
< |
lock.writeLock().lock(); |
1279 |
< |
threadAssertFalse(lock.hasWaiters(c)); |
1280 |
< |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
1281 |
< |
c.await(); |
1282 |
< |
lock.writeLock().unlock(); |
1357 |
< |
} |
1358 |
< |
catch (InterruptedException e) { |
1359 |
< |
threadUnexpectedException(); |
1360 |
< |
} |
1361 |
< |
} |
1362 |
< |
}); |
1275 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1276 |
> |
public void realRun() throws InterruptedException { |
1277 |
> |
lock.writeLock().lock(); |
1278 |
> |
threadAssertFalse(lock.hasWaiters(c)); |
1279 |
> |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
1280 |
> |
c.await(); |
1281 |
> |
lock.writeLock().unlock(); |
1282 |
> |
}}); |
1283 |
|
|
1284 |
|
t.start(); |
1285 |
|
Thread.sleep(SHORT_DELAY_MS); |
1304 |
|
public void testGetWaitingThreads() throws InterruptedException { |
1305 |
|
final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock(); |
1306 |
|
final Condition c = lock.writeLock().newCondition(); |
1307 |
< |
Thread t1 = new Thread(new Runnable() { |
1308 |
< |
public void run() { |
1309 |
< |
try { |
1310 |
< |
lock.writeLock().lock(); |
1311 |
< |
threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); |
1312 |
< |
c.await(); |
1313 |
< |
lock.writeLock().unlock(); |
1314 |
< |
} |
1315 |
< |
catch (InterruptedException e) { |
1316 |
< |
threadUnexpectedException(); |
1317 |
< |
} |
1318 |
< |
} |
1319 |
< |
}); |
1320 |
< |
|
1321 |
< |
Thread t2 = new Thread(new Runnable() { |
1402 |
< |
public void run() { |
1403 |
< |
try { |
1404 |
< |
lock.writeLock().lock(); |
1405 |
< |
threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); |
1406 |
< |
c.await(); |
1407 |
< |
lock.writeLock().unlock(); |
1408 |
< |
} |
1409 |
< |
catch (InterruptedException e) { |
1410 |
< |
threadUnexpectedException(); |
1411 |
< |
} |
1412 |
< |
} |
1413 |
< |
}); |
1307 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
1308 |
> |
public void realRun() throws InterruptedException { |
1309 |
> |
lock.writeLock().lock(); |
1310 |
> |
threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); |
1311 |
> |
c.await(); |
1312 |
> |
lock.writeLock().unlock(); |
1313 |
> |
}}); |
1314 |
> |
|
1315 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
1316 |
> |
public void realRun() throws InterruptedException { |
1317 |
> |
lock.writeLock().lock(); |
1318 |
> |
threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); |
1319 |
> |
c.await(); |
1320 |
> |
lock.writeLock().unlock(); |
1321 |
> |
}}); |
1322 |
|
|
1323 |
|
lock.writeLock().lock(); |
1324 |
|
assertTrue(lock.getWaitingThreads(c).isEmpty()); |