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