2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
|
* http://creativecommons.org/licenses/publicdomain |
5 |
< |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
< |
* Pat Fisher, Mike Judd. |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
14 |
|
|
15 |
|
public class ReentrantLockTest extends JSR166TestCase { |
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run (suite()); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
|
return new TestSuite(ReentrantLockTest.class); |
29 |
|
public void run() { |
30 |
|
try { |
31 |
|
lock.lockInterruptibly(); |
32 |
< |
} catch(InterruptedException success){} |
32 |
> |
} catch (InterruptedException success){} |
33 |
|
} |
34 |
|
} |
35 |
|
|
45 |
|
try { |
46 |
|
lock.lockInterruptibly(); |
47 |
|
threadShouldThrow(); |
48 |
< |
} catch(InterruptedException success){} |
48 |
> |
} catch (InterruptedException success){} |
49 |
|
} |
50 |
|
} |
51 |
|
|
54 |
|
*/ |
55 |
|
static class PublicReentrantLock extends ReentrantLock { |
56 |
|
PublicReentrantLock() { super(); } |
57 |
< |
public Collection<Thread> getQueuedThreads() { |
58 |
< |
return super.getQueuedThreads(); |
57 |
> |
public Collection<Thread> getQueuedThreads() { |
58 |
> |
return super.getQueuedThreads(); |
59 |
|
} |
60 |
< |
public Collection<Thread> getWaitingThreads(Condition c) { |
61 |
< |
return super.getWaitingThreads(c); |
60 |
> |
public Collection<Thread> getWaitingThreads(Condition c) { |
61 |
> |
return super.getWaitingThreads(c); |
62 |
|
} |
63 |
|
|
64 |
|
|
67 |
|
/** |
68 |
|
* Constructor sets given fairness |
69 |
|
*/ |
70 |
< |
public void testConstructor() { |
70 |
> |
public void testConstructor() { |
71 |
|
ReentrantLock rl = new ReentrantLock(); |
72 |
|
assertFalse(rl.isFair()); |
73 |
|
ReentrantLock r2 = new ReentrantLock(true); |
77 |
|
/** |
78 |
|
* locking an unlocked lock succeeds |
79 |
|
*/ |
80 |
< |
public void testLock() { |
80 |
> |
public void testLock() { |
81 |
|
ReentrantLock rl = new ReentrantLock(); |
82 |
|
rl.lock(); |
83 |
|
assertTrue(rl.isLocked()); |
87 |
|
/** |
88 |
|
* locking an unlocked fair lock succeeds |
89 |
|
*/ |
90 |
< |
public void testFairLock() { |
90 |
> |
public void testFairLock() { |
91 |
|
ReentrantLock rl = new ReentrantLock(true); |
92 |
|
rl.lock(); |
93 |
|
assertTrue(rl.isLocked()); |
97 |
|
/** |
98 |
|
* Unlocking an unlocked lock throws IllegalMonitorStateException |
99 |
|
*/ |
100 |
< |
public void testUnlock_IllegalMonitorStateException() { |
100 |
> |
public void testUnlock_IllegalMonitorStateException() { |
101 |
|
ReentrantLock rl = new ReentrantLock(); |
102 |
|
try { |
103 |
|
rl.unlock(); |
104 |
|
shouldThrow(); |
105 |
|
|
106 |
< |
} catch(IllegalMonitorStateException success){} |
106 |
> |
} catch (IllegalMonitorStateException success){} |
107 |
|
} |
108 |
|
|
109 |
|
/** |
110 |
|
* tryLock on an unlocked lock succeeds |
111 |
|
*/ |
112 |
< |
public void testTryLock() { |
112 |
> |
public void testTryLock() { |
113 |
|
ReentrantLock rl = new ReentrantLock(); |
114 |
|
assertTrue(rl.tryLock()); |
115 |
|
assertTrue(rl.isLocked()); |
120 |
|
/** |
121 |
|
* hasQueuedThreads reports whether there are waiting threads |
122 |
|
*/ |
123 |
< |
public void testhasQueuedThreads() { |
123 |
> |
public void testhasQueuedThreads() { |
124 |
|
final ReentrantLock lock = new ReentrantLock(); |
125 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
126 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
141 |
|
assertFalse(lock.hasQueuedThreads()); |
142 |
|
t1.join(); |
143 |
|
t2.join(); |
144 |
< |
} catch(Exception e){ |
144 |
> |
} catch (Exception e){ |
145 |
|
unexpectedException(); |
146 |
|
} |
147 |
< |
} |
147 |
> |
} |
148 |
|
|
149 |
|
/** |
150 |
|
* getQueueLength reports number of waiting threads |
151 |
|
*/ |
152 |
< |
public void testGetQueueLength() { |
152 |
> |
public void testGetQueueLength() { |
153 |
|
final ReentrantLock lock = new ReentrantLock(); |
154 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
155 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
170 |
|
assertEquals(0, lock.getQueueLength()); |
171 |
|
t1.join(); |
172 |
|
t2.join(); |
173 |
< |
} catch(Exception e){ |
173 |
> |
} catch (Exception e){ |
174 |
|
unexpectedException(); |
175 |
|
} |
176 |
< |
} |
176 |
> |
} |
177 |
|
|
178 |
|
/** |
179 |
|
* getQueueLength reports number of waiting threads |
180 |
|
*/ |
181 |
< |
public void testGetQueueLength_fair() { |
181 |
> |
public void testGetQueueLength_fair() { |
182 |
|
final ReentrantLock lock = new ReentrantLock(true); |
183 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
184 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
199 |
|
assertEquals(0, lock.getQueueLength()); |
200 |
|
t1.join(); |
201 |
|
t2.join(); |
202 |
< |
} catch(Exception e){ |
202 |
> |
} catch (Exception e){ |
203 |
|
unexpectedException(); |
204 |
|
} |
205 |
< |
} |
205 |
> |
} |
206 |
|
|
207 |
|
/** |
208 |
|
* hasQueuedThread(null) throws NPE |
209 |
|
*/ |
210 |
< |
public void testHasQueuedThreadNPE() { |
210 |
> |
public void testHasQueuedThreadNPE() { |
211 |
|
final ReentrantLock sync = new ReentrantLock(); |
212 |
|
try { |
213 |
|
sync.hasQueuedThread(null); |
219 |
|
/** |
220 |
|
* hasQueuedThread reports whether a thread is queued. |
221 |
|
*/ |
222 |
< |
public void testHasQueuedThread() { |
222 |
> |
public void testHasQueuedThread() { |
223 |
|
final ReentrantLock sync = new ReentrantLock(); |
224 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(sync)); |
225 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(sync)); |
245 |
|
assertFalse(sync.hasQueuedThread(t2)); |
246 |
|
t1.join(); |
247 |
|
t2.join(); |
248 |
< |
} catch(Exception e){ |
248 |
> |
} catch (Exception e){ |
249 |
|
unexpectedException(); |
250 |
|
} |
251 |
< |
} |
251 |
> |
} |
252 |
|
|
253 |
|
|
254 |
|
/** |
255 |
|
* getQueuedThreads includes waiting threads |
256 |
|
*/ |
257 |
< |
public void testGetQueuedThreads() { |
257 |
> |
public void testGetQueuedThreads() { |
258 |
|
final PublicReentrantLock lock = new PublicReentrantLock(); |
259 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
260 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
278 |
|
assertTrue(lock.getQueuedThreads().isEmpty()); |
279 |
|
t1.join(); |
280 |
|
t2.join(); |
281 |
< |
} catch(Exception e){ |
281 |
> |
} catch (Exception e){ |
282 |
|
unexpectedException(); |
283 |
|
} |
284 |
< |
} |
284 |
> |
} |
285 |
|
|
286 |
|
|
287 |
|
/** |
288 |
|
* timed tryLock is interruptible. |
289 |
|
*/ |
290 |
< |
public void testInterruptedException2() { |
290 |
> |
public void testInterruptedException2() { |
291 |
|
final ReentrantLock lock = new ReentrantLock(); |
292 |
|
lock.lock(); |
293 |
|
Thread t = new Thread(new Runnable() { |
295 |
|
try { |
296 |
|
lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS); |
297 |
|
threadShouldThrow(); |
298 |
< |
} catch(InterruptedException success){} |
298 |
> |
} catch (InterruptedException success){} |
299 |
|
} |
300 |
|
}); |
301 |
|
try { |
302 |
|
t.start(); |
303 |
|
t.interrupt(); |
304 |
< |
} catch(Exception e){ |
304 |
> |
} catch (Exception e){ |
305 |
|
unexpectedException(); |
306 |
|
} |
307 |
|
} |
310 |
|
/** |
311 |
|
* TryLock on a locked lock fails |
312 |
|
*/ |
313 |
< |
public void testTryLockWhenLocked() { |
313 |
> |
public void testTryLockWhenLocked() { |
314 |
|
final ReentrantLock lock = new ReentrantLock(); |
315 |
|
lock.lock(); |
316 |
|
Thread t = new Thread(new Runnable() { |
322 |
|
t.start(); |
323 |
|
t.join(); |
324 |
|
lock.unlock(); |
325 |
< |
} catch(Exception e){ |
325 |
> |
} catch (Exception e){ |
326 |
|
unexpectedException(); |
327 |
|
} |
328 |
< |
} |
328 |
> |
} |
329 |
|
|
330 |
|
/** |
331 |
|
* Timed tryLock on a locked lock times out |
332 |
|
*/ |
333 |
< |
public void testTryLock_Timeout() { |
333 |
> |
public void testTryLock_Timeout() { |
334 |
|
final ReentrantLock lock = new ReentrantLock(); |
335 |
|
lock.lock(); |
336 |
|
Thread t = new Thread(new Runnable() { |
346 |
|
t.start(); |
347 |
|
t.join(); |
348 |
|
lock.unlock(); |
349 |
< |
} catch(Exception e){ |
349 |
> |
} catch (Exception e){ |
350 |
|
unexpectedException(); |
351 |
|
} |
352 |
< |
} |
353 |
< |
|
352 |
> |
} |
353 |
> |
|
354 |
|
/** |
355 |
|
* getHoldCount returns number of recursive holds |
356 |
|
*/ |
357 |
|
public void testGetHoldCount() { |
358 |
|
ReentrantLock lock = new ReentrantLock(); |
359 |
< |
for(int i = 1; i <= SIZE; i++) { |
359 |
> |
for (int i = 1; i <= SIZE; i++) { |
360 |
|
lock.lock(); |
361 |
|
assertEquals(i,lock.getHoldCount()); |
362 |
|
} |
363 |
< |
for(int i = SIZE; i > 0; i--) { |
363 |
> |
for (int i = SIZE; i > 0; i--) { |
364 |
|
lock.unlock(); |
365 |
|
assertEquals(i-1,lock.getHoldCount()); |
366 |
|
} |
367 |
|
} |
368 |
< |
|
369 |
< |
|
368 |
> |
|
369 |
> |
|
370 |
|
/** |
371 |
|
* isLocked is true when locked and false when not |
372 |
|
*/ |
376 |
|
assertTrue(lock.isLocked()); |
377 |
|
lock.unlock(); |
378 |
|
assertFalse(lock.isLocked()); |
379 |
< |
Thread t = new Thread(new Runnable() { |
379 |
> |
Thread t = new Thread(new Runnable() { |
380 |
|
public void run() { |
381 |
|
lock.lock(); |
382 |
|
try { |
383 |
|
Thread.sleep(SMALL_DELAY_MS); |
384 |
|
} |
385 |
< |
catch(Exception e) { |
385 |
> |
catch (Exception e) { |
386 |
|
threadUnexpectedException(); |
387 |
|
} |
388 |
|
lock.unlock(); |
394 |
|
assertTrue(lock.isLocked()); |
395 |
|
t.join(); |
396 |
|
assertFalse(lock.isLocked()); |
397 |
< |
} catch(Exception e){ |
397 |
> |
} catch (Exception e){ |
398 |
|
unexpectedException(); |
399 |
|
} |
400 |
|
} |
403 |
|
/** |
404 |
|
* lockInterruptibly is interruptible. |
405 |
|
*/ |
406 |
< |
public void testLockInterruptibly1() { |
406 |
> |
public void testLockInterruptibly1() { |
407 |
|
final ReentrantLock lock = new ReentrantLock(); |
408 |
|
lock.lock(); |
409 |
|
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
414 |
|
Thread.sleep(SHORT_DELAY_MS); |
415 |
|
lock.unlock(); |
416 |
|
t.join(); |
417 |
< |
} catch(Exception e){ |
417 |
> |
} catch (Exception e){ |
418 |
|
unexpectedException(); |
419 |
|
} |
420 |
< |
} |
420 |
> |
} |
421 |
|
|
422 |
|
/** |
423 |
|
* lockInterruptibly succeeds when unlocked, else is interruptible |
424 |
|
*/ |
425 |
|
public void testLockInterruptibly2() { |
426 |
< |
final ReentrantLock lock = new ReentrantLock(); |
426 |
> |
final ReentrantLock lock = new ReentrantLock(); |
427 |
|
try { |
428 |
|
lock.lockInterruptibly(); |
429 |
< |
} catch(Exception e) { |
429 |
> |
} catch (Exception e) { |
430 |
|
unexpectedException(); |
431 |
|
} |
432 |
|
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
436 |
|
assertTrue(lock.isLocked()); |
437 |
|
assertTrue(lock.isHeldByCurrentThread()); |
438 |
|
t.join(); |
439 |
< |
} catch(Exception e){ |
439 |
> |
} catch (Exception e){ |
440 |
|
unexpectedException(); |
441 |
|
} |
442 |
|
} |
445 |
|
* Calling await without holding lock throws IllegalMonitorStateException |
446 |
|
*/ |
447 |
|
public void testAwait_IllegalMonitor() { |
448 |
< |
final ReentrantLock lock = new ReentrantLock(); |
448 |
> |
final ReentrantLock lock = new ReentrantLock(); |
449 |
|
final Condition c = lock.newCondition(); |
450 |
|
try { |
451 |
|
c.await(); |
462 |
|
* Calling signal without holding lock throws IllegalMonitorStateException |
463 |
|
*/ |
464 |
|
public void testSignal_IllegalMonitor() { |
465 |
< |
final ReentrantLock lock = new ReentrantLock(); |
465 |
> |
final ReentrantLock lock = new ReentrantLock(); |
466 |
|
final Condition c = lock.newCondition(); |
467 |
|
try { |
468 |
|
c.signal(); |
479 |
|
* awaitNanos without a signal times out |
480 |
|
*/ |
481 |
|
public void testAwaitNanos_Timeout() { |
482 |
< |
final ReentrantLock lock = new ReentrantLock(); |
482 |
> |
final ReentrantLock lock = new ReentrantLock(); |
483 |
|
final Condition c = lock.newCondition(); |
484 |
|
try { |
485 |
|
lock.lock(); |
496 |
|
* timed await without a signal times out |
497 |
|
*/ |
498 |
|
public void testAwait_Timeout() { |
499 |
< |
final ReentrantLock lock = new ReentrantLock(); |
499 |
> |
final ReentrantLock lock = new ReentrantLock(); |
500 |
|
final Condition c = lock.newCondition(); |
501 |
|
try { |
502 |
|
lock.lock(); |
512 |
|
* awaitUntil without a signal times out |
513 |
|
*/ |
514 |
|
public void testAwaitUntil_Timeout() { |
515 |
< |
final ReentrantLock lock = new ReentrantLock(); |
515 |
> |
final ReentrantLock lock = new ReentrantLock(); |
516 |
|
final Condition c = lock.newCondition(); |
517 |
|
try { |
518 |
|
lock.lock(); |
529 |
|
* await returns when signalled |
530 |
|
*/ |
531 |
|
public void testAwait() { |
532 |
< |
final ReentrantLock lock = new ReentrantLock(); |
532 |
> |
final ReentrantLock lock = new ReentrantLock(); |
533 |
|
final Condition c = lock.newCondition(); |
534 |
< |
Thread t = new Thread(new Runnable() { |
534 |
> |
Thread t = new Thread(new Runnable() { |
535 |
|
public void run() { |
536 |
|
try { |
537 |
|
lock.lock(); |
538 |
|
c.await(); |
539 |
|
lock.unlock(); |
540 |
|
} |
541 |
< |
catch(InterruptedException e) { |
541 |
> |
catch (InterruptedException e) { |
542 |
|
threadUnexpectedException(); |
543 |
|
} |
544 |
|
} |
670 |
|
* getWaitingThreads throws IAE if not owned |
671 |
|
*/ |
672 |
|
public void testGetWaitingThreadsIAE() { |
673 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
673 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(); |
674 |
|
final Condition c = (lock.newCondition()); |
675 |
< |
final PublicReentrantLock lock2 = new PublicReentrantLock(); |
675 |
> |
final PublicReentrantLock lock2 = new PublicReentrantLock(); |
676 |
|
try { |
677 |
|
lock2.getWaitingThreads(c); |
678 |
|
shouldThrow(); |
686 |
|
* getWaitingThreads throws IMSE if not locked |
687 |
|
*/ |
688 |
|
public void testGetWaitingThreadsIMSE() { |
689 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
689 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(); |
690 |
|
final Condition c = (lock.newCondition()); |
691 |
|
try { |
692 |
|
lock.getWaitingThreads(c); |
703 |
|
* hasWaiters returns true when a thread is waiting, else false |
704 |
|
*/ |
705 |
|
public void testHasWaiters() { |
706 |
< |
final ReentrantLock lock = new ReentrantLock(); |
706 |
> |
final ReentrantLock lock = new ReentrantLock(); |
707 |
|
final Condition c = lock.newCondition(); |
708 |
< |
Thread t = new Thread(new Runnable() { |
708 |
> |
Thread t = new Thread(new Runnable() { |
709 |
|
public void run() { |
710 |
|
try { |
711 |
|
lock.lock(); |
714 |
|
c.await(); |
715 |
|
lock.unlock(); |
716 |
|
} |
717 |
< |
catch(InterruptedException e) { |
717 |
> |
catch (InterruptedException e) { |
718 |
|
threadUnexpectedException(); |
719 |
|
} |
720 |
|
} |
745 |
|
* getWaitQueueLength returns number of waiting threads |
746 |
|
*/ |
747 |
|
public void testGetWaitQueueLength() { |
748 |
< |
final ReentrantLock lock = new ReentrantLock(); |
748 |
> |
final ReentrantLock lock = new ReentrantLock(); |
749 |
|
final Condition c = lock.newCondition(); |
750 |
< |
Thread t1 = new Thread(new Runnable() { |
750 |
> |
Thread t1 = new Thread(new Runnable() { |
751 |
|
public void run() { |
752 |
|
try { |
753 |
|
lock.lock(); |
756 |
|
c.await(); |
757 |
|
lock.unlock(); |
758 |
|
} |
759 |
< |
catch(InterruptedException e) { |
759 |
> |
catch (InterruptedException e) { |
760 |
|
threadUnexpectedException(); |
761 |
|
} |
762 |
|
} |
763 |
|
}); |
764 |
|
|
765 |
< |
Thread t2 = new Thread(new Runnable() { |
765 |
> |
Thread t2 = new Thread(new Runnable() { |
766 |
|
public void run() { |
767 |
|
try { |
768 |
|
lock.lock(); |
771 |
|
c.await(); |
772 |
|
lock.unlock(); |
773 |
|
} |
774 |
< |
catch(InterruptedException e) { |
774 |
> |
catch (InterruptedException e) { |
775 |
|
threadUnexpectedException(); |
776 |
|
} |
777 |
|
} |
806 |
|
* getWaitingThreads returns only and all waiting threads |
807 |
|
*/ |
808 |
|
public void testGetWaitingThreads() { |
809 |
< |
final PublicReentrantLock lock = new PublicReentrantLock(); |
809 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(); |
810 |
|
final Condition c = lock.newCondition(); |
811 |
< |
Thread t1 = new Thread(new Runnable() { |
811 |
> |
Thread t1 = new Thread(new Runnable() { |
812 |
|
public void run() { |
813 |
|
try { |
814 |
|
lock.lock(); |
816 |
|
c.await(); |
817 |
|
lock.unlock(); |
818 |
|
} |
819 |
< |
catch(InterruptedException e) { |
819 |
> |
catch (InterruptedException e) { |
820 |
|
threadUnexpectedException(); |
821 |
|
} |
822 |
|
} |
823 |
|
}); |
824 |
|
|
825 |
< |
Thread t2 = new Thread(new Runnable() { |
825 |
> |
Thread t2 = new Thread(new Runnable() { |
826 |
|
public void run() { |
827 |
|
try { |
828 |
|
lock.lock(); |
830 |
|
c.await(); |
831 |
|
lock.unlock(); |
832 |
|
} |
833 |
< |
catch(InterruptedException e) { |
833 |
> |
catch (InterruptedException e) { |
834 |
|
threadUnexpectedException(); |
835 |
|
} |
836 |
|
} |
869 |
|
class UninterruptableThread extends Thread { |
870 |
|
private ReentrantLock lock; |
871 |
|
private Condition c; |
872 |
< |
|
872 |
> |
|
873 |
|
public volatile boolean canAwake = false; |
874 |
|
public volatile boolean interrupted = false; |
875 |
|
public volatile boolean lockStarted = false; |
876 |
< |
|
876 |
> |
|
877 |
|
public UninterruptableThread(ReentrantLock lock, Condition c) { |
878 |
|
this.lock = lock; |
879 |
|
this.c = c; |
880 |
|
} |
881 |
< |
|
881 |
> |
|
882 |
|
public synchronized void run() { |
883 |
|
lock.lock(); |
884 |
|
lockStarted = true; |
885 |
< |
|
885 |
> |
|
886 |
|
while (!canAwake) { |
887 |
|
c.awaitUninterruptibly(); |
888 |
|
} |
889 |
< |
|
889 |
> |
|
890 |
|
interrupted = isInterrupted(); |
891 |
|
lock.unlock(); |
892 |
|
} |
928 |
|
* await is interruptible |
929 |
|
*/ |
930 |
|
public void testAwait_Interrupt() { |
931 |
< |
final ReentrantLock lock = new ReentrantLock(); |
931 |
> |
final ReentrantLock lock = new ReentrantLock(); |
932 |
|
final Condition c = lock.newCondition(); |
933 |
< |
Thread t = new Thread(new Runnable() { |
933 |
> |
Thread t = new Thread(new Runnable() { |
934 |
|
public void run() { |
935 |
|
try { |
936 |
|
lock.lock(); |
938 |
|
lock.unlock(); |
939 |
|
threadShouldThrow(); |
940 |
|
} |
941 |
< |
catch(InterruptedException success) { |
941 |
> |
catch (InterruptedException success) { |
942 |
|
} |
943 |
|
} |
944 |
|
}); |
959 |
|
* awaitNanos is interruptible |
960 |
|
*/ |
961 |
|
public void testAwaitNanos_Interrupt() { |
962 |
< |
final ReentrantLock lock = new ReentrantLock(); |
962 |
> |
final ReentrantLock lock = new ReentrantLock(); |
963 |
|
final Condition c = lock.newCondition(); |
964 |
< |
Thread t = new Thread(new Runnable() { |
964 |
> |
Thread t = new Thread(new Runnable() { |
965 |
|
public void run() { |
966 |
|
try { |
967 |
|
lock.lock(); |
969 |
|
lock.unlock(); |
970 |
|
threadShouldThrow(); |
971 |
|
} |
972 |
< |
catch(InterruptedException success) { |
972 |
> |
catch (InterruptedException success) { |
973 |
|
} |
974 |
|
} |
975 |
|
}); |
990 |
|
* awaitUntil is interruptible |
991 |
|
*/ |
992 |
|
public void testAwaitUntil_Interrupt() { |
993 |
< |
final ReentrantLock lock = new ReentrantLock(); |
993 |
> |
final ReentrantLock lock = new ReentrantLock(); |
994 |
|
final Condition c = lock.newCondition(); |
995 |
< |
Thread t = new Thread(new Runnable() { |
995 |
> |
Thread t = new Thread(new Runnable() { |
996 |
|
public void run() { |
997 |
|
try { |
998 |
|
lock.lock(); |
1001 |
|
lock.unlock(); |
1002 |
|
threadShouldThrow(); |
1003 |
|
} |
1004 |
< |
catch(InterruptedException success) { |
1004 |
> |
catch (InterruptedException success) { |
1005 |
|
} |
1006 |
|
} |
1007 |
|
}); |
1022 |
|
* signalAll wakes up all threads |
1023 |
|
*/ |
1024 |
|
public void testSignalAll() { |
1025 |
< |
final ReentrantLock lock = new ReentrantLock(); |
1025 |
> |
final ReentrantLock lock = new ReentrantLock(); |
1026 |
|
final Condition c = lock.newCondition(); |
1027 |
< |
Thread t1 = new Thread(new Runnable() { |
1027 |
> |
Thread t1 = new Thread(new Runnable() { |
1028 |
|
public void run() { |
1029 |
|
try { |
1030 |
|
lock.lock(); |
1031 |
|
c.await(); |
1032 |
|
lock.unlock(); |
1033 |
|
} |
1034 |
< |
catch(InterruptedException e) { |
1034 |
> |
catch (InterruptedException e) { |
1035 |
|
threadUnexpectedException(); |
1036 |
|
} |
1037 |
|
} |
1038 |
|
}); |
1039 |
|
|
1040 |
< |
Thread t2 = new Thread(new Runnable() { |
1040 |
> |
Thread t2 = new Thread(new Runnable() { |
1041 |
|
public void run() { |
1042 |
|
try { |
1043 |
|
lock.lock(); |
1044 |
|
c.await(); |
1045 |
|
lock.unlock(); |
1046 |
|
} |
1047 |
< |
catch(InterruptedException e) { |
1047 |
> |
catch (InterruptedException e) { |
1048 |
|
threadUnexpectedException(); |
1049 |
|
} |
1050 |
|
} |
1071 |
|
* await after multiple reentrant locking preserves lock count |
1072 |
|
*/ |
1073 |
|
public void testAwaitLockCount() { |
1074 |
< |
final ReentrantLock lock = new ReentrantLock(); |
1074 |
> |
final ReentrantLock lock = new ReentrantLock(); |
1075 |
|
final Condition c = lock.newCondition(); |
1076 |
< |
Thread t1 = new Thread(new Runnable() { |
1076 |
> |
Thread t1 = new Thread(new Runnable() { |
1077 |
|
public void run() { |
1078 |
|
try { |
1079 |
|
lock.lock(); |
1082 |
|
threadAssertEquals(1, lock.getHoldCount()); |
1083 |
|
lock.unlock(); |
1084 |
|
} |
1085 |
< |
catch(InterruptedException e) { |
1085 |
> |
catch (InterruptedException e) { |
1086 |
|
threadUnexpectedException(); |
1087 |
|
} |
1088 |
|
} |
1089 |
|
}); |
1090 |
|
|
1091 |
< |
Thread t2 = new Thread(new Runnable() { |
1091 |
> |
Thread t2 = new Thread(new Runnable() { |
1092 |
|
public void run() { |
1093 |
|
try { |
1094 |
|
lock.lock(); |
1099 |
|
lock.unlock(); |
1100 |
|
lock.unlock(); |
1101 |
|
} |
1102 |
< |
catch(InterruptedException e) { |
1102 |
> |
catch (InterruptedException e) { |
1103 |
|
threadUnexpectedException(); |
1104 |
|
} |
1105 |
|
} |
1141 |
|
ReentrantLock r = (ReentrantLock) in.readObject(); |
1142 |
|
r.lock(); |
1143 |
|
r.unlock(); |
1144 |
< |
} catch(Exception e){ |
1144 |
> |
} catch (Exception e){ |
1145 |
|
e.printStackTrace(); |
1146 |
|
unexpectedException(); |
1147 |
|
} |