9 |
|
import junit.framework.*; |
10 |
|
import java.util.concurrent.locks.*; |
11 |
|
import java.util.concurrent.*; |
12 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
|
import java.io.*; |
14 |
|
import java.util.*; |
15 |
|
|
215 |
|
lock.writeLock().lock(); |
216 |
|
Thread t = new Thread(new CheckedInterruptedRunnable() { |
217 |
|
public void realRun() throws InterruptedException { |
218 |
< |
lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS); |
218 |
> |
lock.writeLock().tryLock(SMALL_DELAY_MS, MILLISECONDS); |
219 |
|
}}); |
220 |
|
|
221 |
|
t.start(); |
222 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
223 |
|
t.interrupt(); |
224 |
|
lock.writeLock().unlock(); |
225 |
|
t.join(); |
252 |
|
lock.writeLock().lock(); |
253 |
|
Thread t = new Thread(new CheckedInterruptedRunnable() { |
254 |
|
public void realRun() throws InterruptedException { |
255 |
< |
lock.readLock().tryLock(1000,TimeUnit.MILLISECONDS); |
255 |
> |
lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS); |
256 |
|
}}); |
257 |
|
|
258 |
|
t.start(); |
259 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
260 |
|
t.interrupt(); |
261 |
|
t.join(); |
262 |
|
} |
268 |
|
public void testWriteTryLockWhenLocked() throws InterruptedException { |
269 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
270 |
|
lock.writeLock().lock(); |
271 |
< |
Thread t = new Thread(new Runnable() { |
272 |
< |
public void run() { |
273 |
< |
threadAssertFalse(lock.writeLock().tryLock()); |
274 |
< |
} |
272 |
< |
}); |
271 |
> |
Thread t = new Thread(new CheckedRunnable() { |
272 |
> |
public void realRun() { |
273 |
> |
threadAssertFalse(lock.writeLock().tryLock()); |
274 |
> |
}}); |
275 |
|
|
276 |
|
t.start(); |
277 |
|
t.join(); |
284 |
|
public void testReadTryLockWhenLocked() throws InterruptedException { |
285 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
286 |
|
lock.writeLock().lock(); |
287 |
< |
Thread t = new Thread(new Runnable() { |
288 |
< |
public void run() { |
289 |
< |
threadAssertFalse(lock.readLock().tryLock()); |
290 |
< |
} |
289 |
< |
}); |
287 |
> |
Thread t = new Thread(new CheckedRunnable() { |
288 |
> |
public void realRun() { |
289 |
> |
threadAssertFalse(lock.readLock().tryLock()); |
290 |
> |
}}); |
291 |
|
|
292 |
|
t.start(); |
293 |
|
t.join(); |
300 |
|
public void testMultipleReadLocks() throws InterruptedException { |
301 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
302 |
|
lock.readLock().lock(); |
303 |
< |
Thread t = new Thread(new Runnable() { |
304 |
< |
public void run() { |
305 |
< |
threadAssertTrue(lock.readLock().tryLock()); |
306 |
< |
lock.readLock().unlock(); |
307 |
< |
} |
307 |
< |
}); |
303 |
> |
Thread t = new Thread(new CheckedRunnable() { |
304 |
> |
public void realRun() { |
305 |
> |
threadAssertTrue(lock.readLock().tryLock()); |
306 |
> |
lock.readLock().unlock(); |
307 |
> |
}}); |
308 |
|
|
309 |
|
t.start(); |
310 |
|
t.join(); |
317 |
|
public void testWriteAfterMultipleReadLocks() throws InterruptedException { |
318 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
319 |
|
lock.readLock().lock(); |
320 |
< |
Thread t1 = new Thread(new Runnable() { |
321 |
< |
public void run() { |
322 |
< |
lock.readLock().lock(); |
323 |
< |
lock.readLock().unlock(); |
324 |
< |
} |
325 |
< |
}); |
326 |
< |
Thread t2 = new Thread(new Runnable() { |
327 |
< |
public void run() { |
328 |
< |
lock.writeLock().lock(); |
329 |
< |
lock.writeLock().unlock(); |
330 |
< |
} |
331 |
< |
}); |
320 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
321 |
> |
public void realRun() { |
322 |
> |
lock.readLock().lock(); |
323 |
> |
lock.readLock().unlock(); |
324 |
> |
}}); |
325 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
326 |
> |
public void realRun() { |
327 |
> |
lock.writeLock().lock(); |
328 |
> |
lock.writeLock().unlock(); |
329 |
> |
}}); |
330 |
|
|
331 |
|
t1.start(); |
332 |
|
t2.start(); |
344 |
|
public void testReadAfterWriteLock() throws InterruptedException { |
345 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
346 |
|
lock.writeLock().lock(); |
347 |
< |
Thread t1 = new Thread(new Runnable() { |
348 |
< |
public void run() { |
349 |
< |
lock.readLock().lock(); |
350 |
< |
lock.readLock().unlock(); |
351 |
< |
} |
352 |
< |
}); |
353 |
< |
Thread t2 = new Thread(new Runnable() { |
354 |
< |
public void run() { |
355 |
< |
lock.readLock().lock(); |
356 |
< |
lock.readLock().unlock(); |
359 |
< |
} |
360 |
< |
}); |
347 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
348 |
> |
public void realRun() { |
349 |
> |
lock.readLock().lock(); |
350 |
> |
lock.readLock().unlock(); |
351 |
> |
}}); |
352 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
353 |
> |
public void realRun() { |
354 |
> |
lock.readLock().lock(); |
355 |
> |
lock.readLock().unlock(); |
356 |
> |
}}); |
357 |
|
|
358 |
|
t1.start(); |
359 |
|
t2.start(); |
383 |
|
public void testReadHoldingWriteLock2() throws InterruptedException { |
384 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
385 |
|
lock.writeLock().lock(); |
386 |
< |
Thread t1 = new Thread(new Runnable() { |
387 |
< |
public void run() { |
388 |
< |
lock.readLock().lock(); |
389 |
< |
lock.readLock().unlock(); |
390 |
< |
} |
391 |
< |
}); |
392 |
< |
Thread t2 = new Thread(new Runnable() { |
393 |
< |
public void run() { |
394 |
< |
lock.readLock().lock(); |
395 |
< |
lock.readLock().unlock(); |
400 |
< |
} |
401 |
< |
}); |
386 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
387 |
> |
public void realRun() { |
388 |
> |
lock.readLock().lock(); |
389 |
> |
lock.readLock().unlock(); |
390 |
> |
}}); |
391 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
392 |
> |
public void realRun() { |
393 |
> |
lock.readLock().lock(); |
394 |
> |
lock.readLock().unlock(); |
395 |
> |
}}); |
396 |
|
|
397 |
|
t1.start(); |
398 |
|
t2.start(); |
415 |
|
public void testReadHoldingWriteLock3() throws InterruptedException { |
416 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
417 |
|
lock.writeLock().lock(); |
418 |
< |
Thread t1 = new Thread(new Runnable() { |
419 |
< |
public void run() { |
420 |
< |
lock.writeLock().lock(); |
421 |
< |
lock.writeLock().unlock(); |
422 |
< |
} |
423 |
< |
}); |
424 |
< |
Thread t2 = new Thread(new Runnable() { |
425 |
< |
public void run() { |
426 |
< |
lock.writeLock().lock(); |
427 |
< |
lock.writeLock().unlock(); |
434 |
< |
} |
435 |
< |
}); |
418 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
419 |
> |
public void realRun() { |
420 |
> |
lock.writeLock().lock(); |
421 |
> |
lock.writeLock().unlock(); |
422 |
> |
}}); |
423 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
424 |
> |
public void realRun() { |
425 |
> |
lock.writeLock().lock(); |
426 |
> |
lock.writeLock().unlock(); |
427 |
> |
}}); |
428 |
|
|
429 |
|
t1.start(); |
430 |
|
t2.start(); |
448 |
|
public void testWriteHoldingWriteLock4() throws InterruptedException { |
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(); |
469 |
< |
} |
470 |
< |
}); |
451 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
452 |
> |
public void realRun() { |
453 |
> |
lock.writeLock().lock(); |
454 |
> |
lock.writeLock().unlock(); |
455 |
> |
}}); |
456 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
457 |
> |
public void realRun() { |
458 |
> |
lock.writeLock().lock(); |
459 |
> |
lock.writeLock().unlock(); |
460 |
> |
}}); |
461 |
|
|
462 |
|
t1.start(); |
463 |
|
t2.start(); |
492 |
|
public void testReadHoldingWriteLockFair2() throws InterruptedException { |
493 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
494 |
|
lock.writeLock().lock(); |
495 |
< |
Thread t1 = new Thread(new Runnable() { |
496 |
< |
public void run() { |
497 |
< |
lock.readLock().lock(); |
498 |
< |
lock.readLock().unlock(); |
499 |
< |
} |
500 |
< |
}); |
501 |
< |
Thread t2 = new Thread(new Runnable() { |
502 |
< |
public void run() { |
503 |
< |
lock.readLock().lock(); |
504 |
< |
lock.readLock().unlock(); |
515 |
< |
} |
516 |
< |
}); |
495 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
496 |
> |
public void realRun() { |
497 |
> |
lock.readLock().lock(); |
498 |
> |
lock.readLock().unlock(); |
499 |
> |
}}); |
500 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
501 |
> |
public void realRun() { |
502 |
> |
lock.readLock().lock(); |
503 |
> |
lock.readLock().unlock(); |
504 |
> |
}}); |
505 |
|
|
506 |
|
t1.start(); |
507 |
|
t2.start(); |
525 |
|
public void testReadHoldingWriteLockFair3() throws InterruptedException { |
526 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
527 |
|
lock.writeLock().lock(); |
528 |
< |
Thread t1 = new Thread(new Runnable() { |
529 |
< |
public void run() { |
530 |
< |
lock.writeLock().lock(); |
531 |
< |
lock.writeLock().unlock(); |
532 |
< |
} |
533 |
< |
}); |
534 |
< |
Thread t2 = new Thread(new Runnable() { |
535 |
< |
public void run() { |
536 |
< |
lock.writeLock().lock(); |
537 |
< |
lock.writeLock().unlock(); |
550 |
< |
} |
551 |
< |
}); |
528 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
529 |
> |
public void realRun() { |
530 |
> |
lock.writeLock().lock(); |
531 |
> |
lock.writeLock().unlock(); |
532 |
> |
}}); |
533 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
534 |
> |
public void realRun() { |
535 |
> |
lock.writeLock().lock(); |
536 |
> |
lock.writeLock().unlock(); |
537 |
> |
}}); |
538 |
|
|
539 |
|
t1.start(); |
540 |
|
t2.start(); |
558 |
|
public void testWriteHoldingWriteLockFair4() throws InterruptedException { |
559 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
560 |
|
lock.writeLock().lock(); |
561 |
< |
Thread t1 = new Thread(new Runnable() { |
562 |
< |
public void run() { |
563 |
< |
lock.writeLock().lock(); |
564 |
< |
lock.writeLock().unlock(); |
565 |
< |
} |
566 |
< |
}); |
567 |
< |
Thread t2 = new Thread(new Runnable() { |
568 |
< |
public void run() { |
569 |
< |
lock.writeLock().lock(); |
570 |
< |
lock.writeLock().unlock(); |
585 |
< |
} |
586 |
< |
}); |
561 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
562 |
> |
public void realRun() { |
563 |
> |
lock.writeLock().lock(); |
564 |
> |
lock.writeLock().unlock(); |
565 |
> |
}}); |
566 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
567 |
> |
public void realRun() { |
568 |
> |
lock.writeLock().lock(); |
569 |
> |
lock.writeLock().unlock(); |
570 |
> |
}}); |
571 |
|
|
572 |
|
t1.start(); |
573 |
|
t2.start(); |
593 |
|
public void testTryLockWhenReadLocked() throws InterruptedException { |
594 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
595 |
|
lock.readLock().lock(); |
596 |
< |
Thread t = new Thread(new Runnable() { |
597 |
< |
public void run() { |
598 |
< |
threadAssertTrue(lock.readLock().tryLock()); |
599 |
< |
lock.readLock().unlock(); |
600 |
< |
} |
617 |
< |
}); |
596 |
> |
Thread t = new Thread(new CheckedRunnable() { |
597 |
> |
public void realRun() { |
598 |
> |
threadAssertTrue(lock.readLock().tryLock()); |
599 |
> |
lock.readLock().unlock(); |
600 |
> |
}}); |
601 |
|
|
602 |
|
t.start(); |
603 |
|
t.join(); |
612 |
|
public void testWriteTryLockWhenReadLocked() throws InterruptedException { |
613 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
614 |
|
lock.readLock().lock(); |
615 |
< |
Thread t = new Thread(new Runnable() { |
616 |
< |
public void run() { |
617 |
< |
threadAssertFalse(lock.writeLock().tryLock()); |
618 |
< |
} |
636 |
< |
}); |
615 |
> |
Thread t = new Thread(new CheckedRunnable() { |
616 |
> |
public void realRun() { |
617 |
> |
threadAssertFalse(lock.writeLock().tryLock()); |
618 |
> |
}}); |
619 |
|
|
620 |
|
t.start(); |
621 |
|
t.join(); |
629 |
|
public void testTryLockWhenReadLockedFair() throws InterruptedException { |
630 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
631 |
|
lock.readLock().lock(); |
632 |
< |
Thread t = new Thread(new Runnable() { |
633 |
< |
public void run() { |
634 |
< |
threadAssertTrue(lock.readLock().tryLock()); |
635 |
< |
lock.readLock().unlock(); |
636 |
< |
} |
655 |
< |
}); |
632 |
> |
Thread t = new Thread(new CheckedRunnable() { |
633 |
> |
public void realRun() { |
634 |
> |
threadAssertTrue(lock.readLock().tryLock()); |
635 |
> |
lock.readLock().unlock(); |
636 |
> |
}}); |
637 |
|
|
638 |
|
t.start(); |
639 |
|
t.join(); |
648 |
|
public void testWriteTryLockWhenReadLockedFair() throws InterruptedException { |
649 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
650 |
|
lock.readLock().lock(); |
651 |
< |
Thread t = new Thread(new Runnable() { |
652 |
< |
public void run() { |
653 |
< |
threadAssertFalse(lock.writeLock().tryLock()); |
654 |
< |
} |
674 |
< |
}); |
651 |
> |
Thread t = new Thread(new CheckedRunnable() { |
652 |
> |
public void realRun() { |
653 |
> |
threadAssertFalse(lock.writeLock().tryLock()); |
654 |
> |
}}); |
655 |
|
|
656 |
|
t.start(); |
657 |
|
t.join(); |
668 |
|
lock.writeLock().lock(); |
669 |
|
Thread t = new Thread(new CheckedRunnable() { |
670 |
|
public void realRun() throws InterruptedException { |
671 |
< |
threadAssertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS)); |
671 |
> |
threadAssertFalse(lock.writeLock().tryLock(1, MILLISECONDS)); |
672 |
|
}}); |
673 |
|
|
674 |
|
t.start(); |
685 |
|
lock.writeLock().lock(); |
686 |
|
Thread t = new Thread(new CheckedRunnable() { |
687 |
|
public void realRun() throws InterruptedException { |
688 |
< |
threadAssertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS)); |
688 |
> |
threadAssertFalse(lock.readLock().tryLock(1, MILLISECONDS)); |
689 |
|
}}); |
690 |
|
|
691 |
|
t.start(); |
777 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
778 |
|
final Condition c = lock.writeLock().newCondition(); |
779 |
|
lock.writeLock().lock(); |
780 |
< |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
780 |
> |
assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS)); |
781 |
|
lock.writeLock().unlock(); |
782 |
|
} |
783 |
|
|
899 |
|
Thread t = new Thread(new CheckedInterruptedRunnable() { |
900 |
|
public void realRun() throws InterruptedException { |
901 |
|
lock.writeLock().lock(); |
902 |
< |
c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000); |
902 |
> |
c.awaitNanos(LONG_DELAY_MS * 1000L * 1000L); |
903 |
|
lock.writeLock().unlock(); |
904 |
|
}}); |
905 |
|
|