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(1000,MILLISECONDS); |
256 |
|
}}); |
257 |
|
|
258 |
|
t.start(); |
267 |
|
public void testWriteTryLockWhenLocked() throws InterruptedException { |
268 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
269 |
|
lock.writeLock().lock(); |
270 |
< |
Thread t = new Thread(new Runnable() { |
271 |
< |
public void run() { |
272 |
< |
threadAssertFalse(lock.writeLock().tryLock()); |
273 |
< |
} |
272 |
< |
}); |
270 |
> |
Thread t = new Thread(new CheckedRunnable() { |
271 |
> |
public void realRun() { |
272 |
> |
threadAssertFalse(lock.writeLock().tryLock()); |
273 |
> |
}}); |
274 |
|
|
275 |
|
t.start(); |
276 |
|
t.join(); |
283 |
|
public void testReadTryLockWhenLocked() throws InterruptedException { |
284 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
285 |
|
lock.writeLock().lock(); |
286 |
< |
Thread t = new Thread(new Runnable() { |
287 |
< |
public void run() { |
288 |
< |
threadAssertFalse(lock.readLock().tryLock()); |
289 |
< |
} |
289 |
< |
}); |
286 |
> |
Thread t = new Thread(new CheckedRunnable() { |
287 |
> |
public void realRun() { |
288 |
> |
threadAssertFalse(lock.readLock().tryLock()); |
289 |
> |
}}); |
290 |
|
|
291 |
|
t.start(); |
292 |
|
t.join(); |
299 |
|
public void testMultipleReadLocks() throws InterruptedException { |
300 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
301 |
|
lock.readLock().lock(); |
302 |
< |
Thread t = new Thread(new Runnable() { |
303 |
< |
public void run() { |
304 |
< |
threadAssertTrue(lock.readLock().tryLock()); |
305 |
< |
lock.readLock().unlock(); |
306 |
< |
} |
307 |
< |
}); |
302 |
> |
Thread t = new Thread(new CheckedRunnable() { |
303 |
> |
public void realRun() { |
304 |
> |
threadAssertTrue(lock.readLock().tryLock()); |
305 |
> |
lock.readLock().unlock(); |
306 |
> |
}}); |
307 |
|
|
308 |
|
t.start(); |
309 |
|
t.join(); |
316 |
|
public void testWriteAfterMultipleReadLocks() throws InterruptedException { |
317 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
318 |
|
lock.readLock().lock(); |
319 |
< |
Thread t1 = new Thread(new Runnable() { |
320 |
< |
public void run() { |
321 |
< |
lock.readLock().lock(); |
322 |
< |
lock.readLock().unlock(); |
323 |
< |
} |
324 |
< |
}); |
325 |
< |
Thread t2 = new Thread(new Runnable() { |
326 |
< |
public void run() { |
327 |
< |
lock.writeLock().lock(); |
328 |
< |
lock.writeLock().unlock(); |
330 |
< |
} |
331 |
< |
}); |
319 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
320 |
> |
public void realRun() { |
321 |
> |
lock.readLock().lock(); |
322 |
> |
lock.readLock().unlock(); |
323 |
> |
}}); |
324 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
325 |
> |
public void realRun() { |
326 |
> |
lock.writeLock().lock(); |
327 |
> |
lock.writeLock().unlock(); |
328 |
> |
}}); |
329 |
|
|
330 |
|
t1.start(); |
331 |
|
t2.start(); |
343 |
|
public void testReadAfterWriteLock() throws InterruptedException { |
344 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
345 |
|
lock.writeLock().lock(); |
346 |
< |
Thread t1 = new Thread(new Runnable() { |
347 |
< |
public void run() { |
348 |
< |
lock.readLock().lock(); |
349 |
< |
lock.readLock().unlock(); |
350 |
< |
} |
351 |
< |
}); |
352 |
< |
Thread t2 = new Thread(new Runnable() { |
353 |
< |
public void run() { |
354 |
< |
lock.readLock().lock(); |
355 |
< |
lock.readLock().unlock(); |
359 |
< |
} |
360 |
< |
}); |
346 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
347 |
> |
public void realRun() { |
348 |
> |
lock.readLock().lock(); |
349 |
> |
lock.readLock().unlock(); |
350 |
> |
}}); |
351 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
352 |
> |
public void realRun() { |
353 |
> |
lock.readLock().lock(); |
354 |
> |
lock.readLock().unlock(); |
355 |
> |
}}); |
356 |
|
|
357 |
|
t1.start(); |
358 |
|
t2.start(); |
382 |
|
public void testReadHoldingWriteLock2() throws InterruptedException { |
383 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
384 |
|
lock.writeLock().lock(); |
385 |
< |
Thread t1 = new Thread(new Runnable() { |
386 |
< |
public void run() { |
387 |
< |
lock.readLock().lock(); |
388 |
< |
lock.readLock().unlock(); |
389 |
< |
} |
390 |
< |
}); |
391 |
< |
Thread t2 = new Thread(new Runnable() { |
392 |
< |
public void run() { |
393 |
< |
lock.readLock().lock(); |
394 |
< |
lock.readLock().unlock(); |
400 |
< |
} |
401 |
< |
}); |
385 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
386 |
> |
public void realRun() { |
387 |
> |
lock.readLock().lock(); |
388 |
> |
lock.readLock().unlock(); |
389 |
> |
}}); |
390 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
391 |
> |
public void realRun() { |
392 |
> |
lock.readLock().lock(); |
393 |
> |
lock.readLock().unlock(); |
394 |
> |
}}); |
395 |
|
|
396 |
|
t1.start(); |
397 |
|
t2.start(); |
414 |
|
public void testReadHoldingWriteLock3() throws InterruptedException { |
415 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
416 |
|
lock.writeLock().lock(); |
417 |
< |
Thread t1 = new Thread(new Runnable() { |
418 |
< |
public void run() { |
419 |
< |
lock.writeLock().lock(); |
420 |
< |
lock.writeLock().unlock(); |
421 |
< |
} |
422 |
< |
}); |
423 |
< |
Thread t2 = new Thread(new Runnable() { |
424 |
< |
public void run() { |
425 |
< |
lock.writeLock().lock(); |
426 |
< |
lock.writeLock().unlock(); |
434 |
< |
} |
435 |
< |
}); |
417 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
418 |
> |
public void realRun() { |
419 |
> |
lock.writeLock().lock(); |
420 |
> |
lock.writeLock().unlock(); |
421 |
> |
}}); |
422 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
423 |
> |
public void realRun() { |
424 |
> |
lock.writeLock().lock(); |
425 |
> |
lock.writeLock().unlock(); |
426 |
> |
}}); |
427 |
|
|
428 |
|
t1.start(); |
429 |
|
t2.start(); |
447 |
|
public void testWriteHoldingWriteLock4() throws InterruptedException { |
448 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
449 |
|
lock.writeLock().lock(); |
450 |
< |
Thread t1 = new Thread(new Runnable() { |
451 |
< |
public void run() { |
452 |
< |
lock.writeLock().lock(); |
453 |
< |
lock.writeLock().unlock(); |
454 |
< |
} |
455 |
< |
}); |
456 |
< |
Thread t2 = new Thread(new Runnable() { |
457 |
< |
public void run() { |
458 |
< |
lock.writeLock().lock(); |
459 |
< |
lock.writeLock().unlock(); |
469 |
< |
} |
470 |
< |
}); |
450 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
451 |
> |
public void realRun() { |
452 |
> |
lock.writeLock().lock(); |
453 |
> |
lock.writeLock().unlock(); |
454 |
> |
}}); |
455 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
456 |
> |
public void realRun() { |
457 |
> |
lock.writeLock().lock(); |
458 |
> |
lock.writeLock().unlock(); |
459 |
> |
}}); |
460 |
|
|
461 |
|
t1.start(); |
462 |
|
t2.start(); |
491 |
|
public void testReadHoldingWriteLockFair2() throws InterruptedException { |
492 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
493 |
|
lock.writeLock().lock(); |
494 |
< |
Thread t1 = new Thread(new Runnable() { |
495 |
< |
public void run() { |
496 |
< |
lock.readLock().lock(); |
497 |
< |
lock.readLock().unlock(); |
498 |
< |
} |
499 |
< |
}); |
500 |
< |
Thread t2 = new Thread(new Runnable() { |
501 |
< |
public void run() { |
502 |
< |
lock.readLock().lock(); |
503 |
< |
lock.readLock().unlock(); |
515 |
< |
} |
516 |
< |
}); |
494 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
495 |
> |
public void realRun() { |
496 |
> |
lock.readLock().lock(); |
497 |
> |
lock.readLock().unlock(); |
498 |
> |
}}); |
499 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
500 |
> |
public void realRun() { |
501 |
> |
lock.readLock().lock(); |
502 |
> |
lock.readLock().unlock(); |
503 |
> |
}}); |
504 |
|
|
505 |
|
t1.start(); |
506 |
|
t2.start(); |
524 |
|
public void testReadHoldingWriteLockFair3() throws InterruptedException { |
525 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
526 |
|
lock.writeLock().lock(); |
527 |
< |
Thread t1 = new Thread(new Runnable() { |
528 |
< |
public void run() { |
529 |
< |
lock.writeLock().lock(); |
530 |
< |
lock.writeLock().unlock(); |
531 |
< |
} |
532 |
< |
}); |
533 |
< |
Thread t2 = new Thread(new Runnable() { |
534 |
< |
public void run() { |
535 |
< |
lock.writeLock().lock(); |
536 |
< |
lock.writeLock().unlock(); |
550 |
< |
} |
551 |
< |
}); |
527 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
528 |
> |
public void realRun() { |
529 |
> |
lock.writeLock().lock(); |
530 |
> |
lock.writeLock().unlock(); |
531 |
> |
}}); |
532 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
533 |
> |
public void realRun() { |
534 |
> |
lock.writeLock().lock(); |
535 |
> |
lock.writeLock().unlock(); |
536 |
> |
}}); |
537 |
|
|
538 |
|
t1.start(); |
539 |
|
t2.start(); |
557 |
|
public void testWriteHoldingWriteLockFair4() throws InterruptedException { |
558 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
559 |
|
lock.writeLock().lock(); |
560 |
< |
Thread t1 = new Thread(new Runnable() { |
561 |
< |
public void run() { |
562 |
< |
lock.writeLock().lock(); |
563 |
< |
lock.writeLock().unlock(); |
564 |
< |
} |
565 |
< |
}); |
566 |
< |
Thread t2 = new Thread(new Runnable() { |
567 |
< |
public void run() { |
568 |
< |
lock.writeLock().lock(); |
569 |
< |
lock.writeLock().unlock(); |
585 |
< |
} |
586 |
< |
}); |
560 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
561 |
> |
public void realRun() { |
562 |
> |
lock.writeLock().lock(); |
563 |
> |
lock.writeLock().unlock(); |
564 |
> |
}}); |
565 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
566 |
> |
public void realRun() { |
567 |
> |
lock.writeLock().lock(); |
568 |
> |
lock.writeLock().unlock(); |
569 |
> |
}}); |
570 |
|
|
571 |
|
t1.start(); |
572 |
|
t2.start(); |
592 |
|
public void testTryLockWhenReadLocked() throws InterruptedException { |
593 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
594 |
|
lock.readLock().lock(); |
595 |
< |
Thread t = new Thread(new Runnable() { |
596 |
< |
public void run() { |
597 |
< |
threadAssertTrue(lock.readLock().tryLock()); |
598 |
< |
lock.readLock().unlock(); |
599 |
< |
} |
617 |
< |
}); |
595 |
> |
Thread t = new Thread(new CheckedRunnable() { |
596 |
> |
public void realRun() { |
597 |
> |
threadAssertTrue(lock.readLock().tryLock()); |
598 |
> |
lock.readLock().unlock(); |
599 |
> |
}}); |
600 |
|
|
601 |
|
t.start(); |
602 |
|
t.join(); |
611 |
|
public void testWriteTryLockWhenReadLocked() throws InterruptedException { |
612 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
613 |
|
lock.readLock().lock(); |
614 |
< |
Thread t = new Thread(new Runnable() { |
615 |
< |
public void run() { |
616 |
< |
threadAssertFalse(lock.writeLock().tryLock()); |
617 |
< |
} |
636 |
< |
}); |
614 |
> |
Thread t = new Thread(new CheckedRunnable() { |
615 |
> |
public void realRun() { |
616 |
> |
threadAssertFalse(lock.writeLock().tryLock()); |
617 |
> |
}}); |
618 |
|
|
619 |
|
t.start(); |
620 |
|
t.join(); |
628 |
|
public void testTryLockWhenReadLockedFair() throws InterruptedException { |
629 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
630 |
|
lock.readLock().lock(); |
631 |
< |
Thread t = new Thread(new Runnable() { |
632 |
< |
public void run() { |
633 |
< |
threadAssertTrue(lock.readLock().tryLock()); |
634 |
< |
lock.readLock().unlock(); |
635 |
< |
} |
655 |
< |
}); |
631 |
> |
Thread t = new Thread(new CheckedRunnable() { |
632 |
> |
public void realRun() { |
633 |
> |
threadAssertTrue(lock.readLock().tryLock()); |
634 |
> |
lock.readLock().unlock(); |
635 |
> |
}}); |
636 |
|
|
637 |
|
t.start(); |
638 |
|
t.join(); |
647 |
|
public void testWriteTryLockWhenReadLockedFair() throws InterruptedException { |
648 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); |
649 |
|
lock.readLock().lock(); |
650 |
< |
Thread t = new Thread(new Runnable() { |
651 |
< |
public void run() { |
652 |
< |
threadAssertFalse(lock.writeLock().tryLock()); |
653 |
< |
} |
674 |
< |
}); |
650 |
> |
Thread t = new Thread(new CheckedRunnable() { |
651 |
> |
public void realRun() { |
652 |
> |
threadAssertFalse(lock.writeLock().tryLock()); |
653 |
> |
}}); |
654 |
|
|
655 |
|
t.start(); |
656 |
|
t.join(); |
667 |
|
lock.writeLock().lock(); |
668 |
|
Thread t = new Thread(new CheckedRunnable() { |
669 |
|
public void realRun() throws InterruptedException { |
670 |
< |
threadAssertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS)); |
670 |
> |
threadAssertFalse(lock.writeLock().tryLock(1, MILLISECONDS)); |
671 |
|
}}); |
672 |
|
|
673 |
|
t.start(); |
684 |
|
lock.writeLock().lock(); |
685 |
|
Thread t = new Thread(new CheckedRunnable() { |
686 |
|
public void realRun() throws InterruptedException { |
687 |
< |
threadAssertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS)); |
687 |
> |
threadAssertFalse(lock.readLock().tryLock(1, MILLISECONDS)); |
688 |
|
}}); |
689 |
|
|
690 |
|
t.start(); |
776 |
|
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
777 |
|
final Condition c = lock.writeLock().newCondition(); |
778 |
|
lock.writeLock().lock(); |
779 |
< |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
779 |
> |
assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS)); |
780 |
|
lock.writeLock().unlock(); |
781 |
|
} |
782 |
|
|