ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantLockTest.java
Revision: 1.10
Committed: Sat Dec 27 14:16:33 2003 UTC (20 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.9: +3 -3 lines
Log Message:
Adjust protected method test class

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by members of JCP JSR-166 Expert Group and released to the
3     * public domain. Use, modify, and redistribute this code in any way
4     * without acknowledgement. Other contributors include Andrew Wright,
5     * Jeffrey Hayes, Pat Fischer, Mike Judd.
6     */
7    
8     import junit.framework.*;
9     import java.util.concurrent.locks.*;
10     import java.util.concurrent.*;
11 dl 1.5 import java.util.*;
12 dl 1.3 import java.io.*;
13 dl 1.1
14 dl 1.4 public class ReentrantLockTest extends JSR166TestCase {
15 dl 1.1 public static void main(String[] args) {
16     junit.textui.TestRunner.run (suite());
17     }
18     public static Test suite() {
19     return new TestSuite(ReentrantLockTest.class);
20     }
21    
22 dl 1.6 /**
23     * A runnable calling lockInterruptibly
24     */
25 dl 1.5 class InterruptibleLockRunnable implements Runnable {
26     final ReentrantLock lock;
27     InterruptibleLockRunnable(ReentrantLock l) { lock = l; }
28 dl 1.6 public void run() {
29     try {
30 dl 1.5 lock.lockInterruptibly();
31     } catch(InterruptedException success){}
32     }
33     }
34    
35 dl 1.6
36     /**
37     * A runnable calling lockInterruptibly that expects to be
38     * interrupted
39     */
40 dl 1.5 class InterruptedLockRunnable implements Runnable {
41     final ReentrantLock lock;
42     InterruptedLockRunnable(ReentrantLock l) { lock = l; }
43 dl 1.6 public void run() {
44     try {
45 dl 1.5 lock.lockInterruptibly();
46 dl 1.6 threadShouldThrow();
47 dl 1.5 } catch(InterruptedException success){}
48     }
49     }
50    
51     /**
52 dl 1.6 * Subclass to expose protected methods
53 dl 1.5 */
54 dl 1.6 static class PublicReentrantLock extends ReentrantLock {
55     PublicReentrantLock() { super(); }
56 dl 1.5 public Collection<Thread> getQueuedThreads() {
57     return super.getQueuedThreads();
58     }
59     public ConditionObject newCondition() {
60 dl 1.10 return new PublicCondition();
61 dl 1.5 }
62    
63 dl 1.10 class PublicCondition extends ReentrantLock.ConditionObject {
64     PublicCondition() { }
65 dl 1.5 public Collection<Thread> getWaitingThreads() {
66     return super.getWaitingThreads();
67     }
68     }
69    
70     }
71    
72 dl 1.8 /**
73 dl 1.9 * Constructor sets given fairness
74     */
75     public void testConstructor() {
76     ReentrantLock rl = new ReentrantLock();
77     assertFalse(rl.isFair());
78     ReentrantLock r2 = new ReentrantLock(true);
79     assertTrue(r2.isFair());
80     }
81    
82     /**
83 dl 1.6 * locking an unlocked lock succeeds
84     */
85     public void testLock() {
86     ReentrantLock rl = new ReentrantLock();
87     rl.lock();
88     assertTrue(rl.isLocked());
89     rl.unlock();
90     }
91    
92 dl 1.8 /**
93 dl 1.6 * locking an unlocked fair lock succeeds
94     */
95     public void testFairLock() {
96     ReentrantLock rl = new ReentrantLock(true);
97     rl.lock();
98     assertTrue(rl.isLocked());
99     rl.unlock();
100     }
101    
102 dl 1.8 /**
103 dl 1.5 * Unlocking an unlocked lock throws IllegalMonitorStateException
104 dl 1.1 */
105 dl 1.6 public void testUnlock_IllegalMonitorStateException() {
106 dl 1.1 ReentrantLock rl = new ReentrantLock();
107 dl 1.6 try {
108 dl 1.1 rl.unlock();
109 dl 1.6 shouldThrow();
110 dl 1.1
111 dl 1.3 } catch(IllegalMonitorStateException success){}
112 dl 1.5 }
113 dl 1.1
114 dl 1.8 /**
115 dl 1.6 * trylock on an unlocked lock succeeds
116 dl 1.1 */
117 dl 1.6 public void testTryLock() {
118     ReentrantLock rl = new ReentrantLock();
119     assertTrue(rl.tryLock());
120     assertTrue(rl.isLocked());
121     rl.unlock();
122     }
123    
124 dl 1.1
125 dl 1.8 /**
126 dl 1.7 * getQueueLength reports number of waiting threads
127 dl 1.1 */
128 dl 1.7 public void testGetQueueLength() {
129 dl 1.5 final ReentrantLock lock = new ReentrantLock();
130     Thread t1 = new Thread(new InterruptedLockRunnable(lock));
131     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
132     try {
133 dl 1.7 assertEquals(0, lock.getQueueLength());
134 dl 1.5 lock.lock();
135     t1.start();
136     Thread.sleep(SHORT_DELAY_MS);
137 dl 1.7 assertEquals(1, lock.getQueueLength());
138 dl 1.5 t2.start();
139     Thread.sleep(SHORT_DELAY_MS);
140 dl 1.7 assertEquals(2, lock.getQueueLength());
141 dl 1.5 t1.interrupt();
142     Thread.sleep(SHORT_DELAY_MS);
143 dl 1.7 assertEquals(1, lock.getQueueLength());
144 dl 1.5 lock.unlock();
145     Thread.sleep(SHORT_DELAY_MS);
146 dl 1.7 assertEquals(0, lock.getQueueLength());
147 dl 1.5 t1.join();
148     t2.join();
149     } catch(Exception e){
150 dl 1.6 unexpectedException();
151 dl 1.5 }
152     }
153    
154 dl 1.8 /**
155 dl 1.5 * getQueuedThreads includes waiting threads
156     */
157 dl 1.6 public void testGetQueuedThreads() {
158     final PublicReentrantLock lock = new PublicReentrantLock();
159 dl 1.5 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
160     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
161     try {
162     assertTrue(lock.getQueuedThreads().isEmpty());
163     lock.lock();
164     assertTrue(lock.getQueuedThreads().isEmpty());
165     t1.start();
166     Thread.sleep(SHORT_DELAY_MS);
167     assertTrue(lock.getQueuedThreads().contains(t1));
168     t2.start();
169     Thread.sleep(SHORT_DELAY_MS);
170     assertTrue(lock.getQueuedThreads().contains(t1));
171     assertTrue(lock.getQueuedThreads().contains(t2));
172     t1.interrupt();
173     Thread.sleep(SHORT_DELAY_MS);
174     assertFalse(lock.getQueuedThreads().contains(t1));
175     assertTrue(lock.getQueuedThreads().contains(t2));
176     lock.unlock();
177     Thread.sleep(SHORT_DELAY_MS);
178     assertTrue(lock.getQueuedThreads().isEmpty());
179     t1.join();
180     t2.join();
181     } catch(Exception e){
182 dl 1.6 unexpectedException();
183 dl 1.5 }
184     }
185    
186 dl 1.1
187 dl 1.8 /**
188 dl 1.5 * timed trylock is interruptible.
189     */
190 dl 1.6 public void testInterruptedException2() {
191 dl 1.1 final ReentrantLock lock = new ReentrantLock();
192     lock.lock();
193     Thread t = new Thread(new Runnable() {
194 dl 1.6 public void run() {
195     try {
196 dl 1.4 lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS);
197 dl 1.6 threadShouldThrow();
198 dl 1.3 } catch(InterruptedException success){}
199 dl 1.1 }
200     });
201 dl 1.4 try {
202     t.start();
203     t.interrupt();
204     } catch(Exception e){
205 dl 1.6 unexpectedException();
206 dl 1.4 }
207 dl 1.1 }
208    
209 dl 1.3
210 dl 1.5 /**
211     * Trylock on a locked lock fails
212     */
213 dl 1.3 public void testTryLockWhenLocked() {
214     final ReentrantLock lock = new ReentrantLock();
215     lock.lock();
216     Thread t = new Thread(new Runnable() {
217 dl 1.6 public void run() {
218 dl 1.4 threadAssertFalse(lock.tryLock());
219 dl 1.3 }
220     });
221     try {
222     t.start();
223     t.join();
224     lock.unlock();
225     } catch(Exception e){
226 dl 1.6 unexpectedException();
227 dl 1.3 }
228     }
229    
230 dl 1.5 /**
231 dl 1.6 * Timed trylock on a locked lock times out
232 dl 1.5 */
233 dl 1.6 public void testTryLock_Timeout() {
234 dl 1.3 final ReentrantLock lock = new ReentrantLock();
235     lock.lock();
236     Thread t = new Thread(new Runnable() {
237 dl 1.6 public void run() {
238 dl 1.3 try {
239 dl 1.4 threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS));
240 dl 1.3 } catch (Exception ex) {
241 dl 1.6 threadUnexpectedException();
242 dl 1.3 }
243     }
244     });
245     try {
246     t.start();
247     t.join();
248     lock.unlock();
249     } catch(Exception e){
250 dl 1.6 unexpectedException();
251 dl 1.3 }
252     }
253 dl 1.1
254 dl 1.5 /**
255     * getHoldCount returns number of recursive holds
256     */
257 dl 1.1 public void testGetHoldCount() {
258     ReentrantLock lock = new ReentrantLock();
259 dl 1.6 for(int i = 1; i <= SIZE; i++) {
260 dl 1.1 lock.lock();
261     assertEquals(i,lock.getHoldCount());
262     }
263 dl 1.6 for(int i = SIZE; i > 0; i--) {
264 dl 1.1 lock.unlock();
265     assertEquals(i-1,lock.getHoldCount());
266     }
267     }
268    
269    
270 dl 1.5 /**
271     * isLocked is true when locked and false when not
272     */
273 dl 1.1 public void testIsLocked() {
274     final ReentrantLock lock = new ReentrantLock();
275     lock.lock();
276     assertTrue(lock.isLocked());
277     lock.unlock();
278     assertFalse(lock.isLocked());
279     Thread t = new Thread(new Runnable() {
280     public void run() {
281     lock.lock();
282     try {
283 dl 1.4 Thread.sleep(SMALL_DELAY_MS);
284 dl 1.1 }
285 dl 1.4 catch(Exception e) {
286 dl 1.6 threadUnexpectedException();
287 dl 1.4 }
288 dl 1.1 lock.unlock();
289     }
290     });
291 dl 1.6 try {
292 dl 1.1 t.start();
293     Thread.sleep(SHORT_DELAY_MS);
294     assertTrue(lock.isLocked());
295     t.join();
296     assertFalse(lock.isLocked());
297     } catch(Exception e){
298 dl 1.6 unexpectedException();
299 dl 1.1 }
300     }
301    
302    
303 dl 1.8 /**
304 dl 1.6 * lockInterruptibly is interruptible.
305     */
306     public void testLockInterruptibly1() {
307     final ReentrantLock lock = new ReentrantLock();
308     lock.lock();
309     Thread t = new Thread(new InterruptedLockRunnable(lock));
310     try {
311     t.start();
312     t.interrupt();
313     lock.unlock();
314     t.join();
315     } catch(Exception e){
316     unexpectedException();
317     }
318     }
319    
320 dl 1.5 /**
321     * lockInterruptibly succeeds when unlocked, else is interruptible
322     */
323 dl 1.6 public void testLockInterruptibly2() {
324 dl 1.1 final ReentrantLock lock = new ReentrantLock();
325 dl 1.3 try {
326     lock.lockInterruptibly();
327     } catch(Exception e) {
328 dl 1.6 unexpectedException();
329 dl 1.3 }
330 dl 1.5 Thread t = new Thread(new InterruptedLockRunnable(lock));
331 dl 1.3 try {
332     t.start();
333     t.interrupt();
334     assertTrue(lock.isLocked());
335     assertTrue(lock.isHeldByCurrentThread());
336     t.join();
337     } catch(Exception e){
338 dl 1.6 unexpectedException();
339 dl 1.3 }
340 dl 1.1 }
341 dl 1.2
342 dl 1.6 /**
343     * Calling await without holding lock throws IllegalMonitorStateException
344     */
345 dl 1.2 public void testAwait_IllegalMonitor() {
346     final ReentrantLock lock = new ReentrantLock();
347     final Condition c = lock.newCondition();
348     try {
349     c.await();
350 dl 1.6 shouldThrow();
351 dl 1.2 }
352     catch (IllegalMonitorStateException success) {
353     }
354     catch (Exception ex) {
355 dl 1.6 unexpectedException();
356 dl 1.2 }
357     }
358    
359 dl 1.6 /**
360     * Calling signal without holding lock throws IllegalMonitorStateException
361     */
362 dl 1.2 public void testSignal_IllegalMonitor() {
363     final ReentrantLock lock = new ReentrantLock();
364     final Condition c = lock.newCondition();
365     try {
366     c.signal();
367 dl 1.6 shouldThrow();
368 dl 1.2 }
369     catch (IllegalMonitorStateException success) {
370     }
371     catch (Exception ex) {
372 dl 1.6 unexpectedException();
373 dl 1.2 }
374     }
375    
376 dl 1.6 /**
377     * awaitNanos without a signal times out
378     */
379 dl 1.2 public void testAwaitNanos_Timeout() {
380     final ReentrantLock lock = new ReentrantLock();
381     final Condition c = lock.newCondition();
382     try {
383     lock.lock();
384     long t = c.awaitNanos(100);
385     assertTrue(t <= 0);
386     lock.unlock();
387     }
388     catch (Exception ex) {
389 dl 1.6 unexpectedException();
390 dl 1.2 }
391     }
392    
393 dl 1.6 /**
394     * timed await without a signal times out
395     */
396 dl 1.2 public void testAwait_Timeout() {
397     final ReentrantLock lock = new ReentrantLock();
398     final Condition c = lock.newCondition();
399     try {
400     lock.lock();
401 dl 1.4 assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
402 dl 1.2 lock.unlock();
403     }
404     catch (Exception ex) {
405 dl 1.6 unexpectedException();
406 dl 1.2 }
407     }
408    
409 dl 1.6 /**
410     * awaitUntil without a signal times out
411     */
412 dl 1.2 public void testAwaitUntil_Timeout() {
413     final ReentrantLock lock = new ReentrantLock();
414     final Condition c = lock.newCondition();
415     try {
416     lock.lock();
417     java.util.Date d = new java.util.Date();
418     assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
419     lock.unlock();
420     }
421     catch (Exception ex) {
422 dl 1.6 unexpectedException();
423 dl 1.2 }
424     }
425    
426 dl 1.6 /**
427     * await returns when signalled
428     */
429 dl 1.2 public void testAwait() {
430     final ReentrantLock lock = new ReentrantLock();
431 dl 1.5 final ReentrantLock.ConditionObject c = lock.newCondition();
432     Thread t = new Thread(new Runnable() {
433     public void run() {
434     try {
435     lock.lock();
436     c.await();
437     lock.unlock();
438     }
439     catch(InterruptedException e) {
440 dl 1.6 threadUnexpectedException();
441 dl 1.5 }
442     }
443     });
444    
445     try {
446     t.start();
447     Thread.sleep(SHORT_DELAY_MS);
448     lock.lock();
449     c.signal();
450     lock.unlock();
451     t.join(SHORT_DELAY_MS);
452     assertFalse(t.isAlive());
453     }
454     catch (Exception ex) {
455 dl 1.6 unexpectedException();
456 dl 1.5 }
457     }
458    
459 dl 1.6 /**
460     * hasWaiters returns true when a thread is waiting, else false
461     */
462 dl 1.5 public void testHasWaiters() {
463     final ReentrantLock lock = new ReentrantLock();
464     final ReentrantLock.ConditionObject c = lock.newCondition();
465 dl 1.2 Thread t = new Thread(new Runnable() {
466     public void run() {
467     try {
468     lock.lock();
469 dl 1.5 threadAssertFalse(c.hasWaiters());
470     threadAssertEquals(0, c.getWaitQueueLength());
471 dl 1.2 c.await();
472     lock.unlock();
473     }
474     catch(InterruptedException e) {
475 dl 1.6 threadUnexpectedException();
476 dl 1.2 }
477     }
478     });
479    
480     try {
481     t.start();
482     Thread.sleep(SHORT_DELAY_MS);
483     lock.lock();
484 dl 1.5 assertTrue(c.hasWaiters());
485     assertEquals(1, c.getWaitQueueLength());
486 dl 1.2 c.signal();
487     lock.unlock();
488 dl 1.5 Thread.sleep(SHORT_DELAY_MS);
489     lock.lock();
490     assertFalse(c.hasWaiters());
491     assertEquals(0, c.getWaitQueueLength());
492     lock.unlock();
493 dl 1.2 t.join(SHORT_DELAY_MS);
494     assertFalse(t.isAlive());
495 dl 1.5 }
496     catch (Exception ex) {
497 dl 1.6 unexpectedException();
498 dl 1.5 }
499     }
500    
501 dl 1.6 /**
502     * getWaitQueueLength returns number of waiting threads
503     */
504 dl 1.5 public void testGetWaitQueueLength() {
505     final ReentrantLock lock = new ReentrantLock();
506     final ReentrantLock.ConditionObject c = lock.newCondition();
507     Thread t1 = new Thread(new Runnable() {
508     public void run() {
509     try {
510     lock.lock();
511     threadAssertFalse(c.hasWaiters());
512     threadAssertEquals(0, c.getWaitQueueLength());
513     c.await();
514     lock.unlock();
515     }
516     catch(InterruptedException e) {
517 dl 1.6 threadUnexpectedException();
518 dl 1.5 }
519     }
520     });
521    
522     Thread t2 = new Thread(new Runnable() {
523     public void run() {
524     try {
525     lock.lock();
526     threadAssertTrue(c.hasWaiters());
527     threadAssertEquals(1, c.getWaitQueueLength());
528     c.await();
529     lock.unlock();
530     }
531     catch(InterruptedException e) {
532 dl 1.6 threadUnexpectedException();
533 dl 1.5 }
534     }
535     });
536    
537     try {
538     t1.start();
539     Thread.sleep(SHORT_DELAY_MS);
540     t2.start();
541     Thread.sleep(SHORT_DELAY_MS);
542     lock.lock();
543     assertTrue(c.hasWaiters());
544     assertEquals(2, c.getWaitQueueLength());
545     c.signalAll();
546     lock.unlock();
547     Thread.sleep(SHORT_DELAY_MS);
548     lock.lock();
549     assertFalse(c.hasWaiters());
550     assertEquals(0, c.getWaitQueueLength());
551     lock.unlock();
552     t1.join(SHORT_DELAY_MS);
553     t2.join(SHORT_DELAY_MS);
554     assertFalse(t1.isAlive());
555     assertFalse(t2.isAlive());
556     }
557     catch (Exception ex) {
558 dl 1.6 unexpectedException();
559 dl 1.5 }
560     }
561    
562 dl 1.6 /**
563     * getWaitingThreads returns only and all waiting threads
564     */
565 dl 1.5 public void testGetWaitingThreads() {
566 dl 1.6 final PublicReentrantLock lock = new PublicReentrantLock();
567     final PublicReentrantLock.PublicCondition c = (PublicReentrantLock.PublicCondition)lock.newCondition();
568 dl 1.5 Thread t1 = new Thread(new Runnable() {
569     public void run() {
570     try {
571     lock.lock();
572     threadAssertTrue(c.getWaitingThreads().isEmpty());
573     c.await();
574     lock.unlock();
575     }
576     catch(InterruptedException e) {
577 dl 1.6 threadUnexpectedException();
578 dl 1.5 }
579     }
580     });
581    
582     Thread t2 = new Thread(new Runnable() {
583     public void run() {
584     try {
585     lock.lock();
586     threadAssertFalse(c.getWaitingThreads().isEmpty());
587     c.await();
588     lock.unlock();
589     }
590     catch(InterruptedException e) {
591 dl 1.6 threadUnexpectedException();
592 dl 1.5 }
593     }
594     });
595    
596     try {
597     lock.lock();
598     assertTrue(c.getWaitingThreads().isEmpty());
599     lock.unlock();
600     t1.start();
601     Thread.sleep(SHORT_DELAY_MS);
602     t2.start();
603     Thread.sleep(SHORT_DELAY_MS);
604     lock.lock();
605     assertTrue(c.hasWaiters());
606     assertTrue(c.getWaitingThreads().contains(t1));
607     assertTrue(c.getWaitingThreads().contains(t2));
608     c.signalAll();
609     lock.unlock();
610     Thread.sleep(SHORT_DELAY_MS);
611     lock.lock();
612     assertFalse(c.hasWaiters());
613     assertTrue(c.getWaitingThreads().isEmpty());
614     lock.unlock();
615     t1.join(SHORT_DELAY_MS);
616     t2.join(SHORT_DELAY_MS);
617     assertFalse(t1.isAlive());
618     assertFalse(t2.isAlive());
619 dl 1.2 }
620     catch (Exception ex) {
621 dl 1.6 unexpectedException();
622 dl 1.2 }
623     }
624    
625 dl 1.6 /**
626     * awaitUninterruptibly doesn't abort on interrupt
627     */
628 dl 1.2 public void testAwaitUninterruptibly() {
629     final ReentrantLock lock = new ReentrantLock();
630     final Condition c = lock.newCondition();
631     Thread t = new Thread(new Runnable() {
632     public void run() {
633     lock.lock();
634     c.awaitUninterruptibly();
635     lock.unlock();
636     }
637     });
638    
639     try {
640     t.start();
641     Thread.sleep(SHORT_DELAY_MS);
642     t.interrupt();
643     lock.lock();
644     c.signal();
645     lock.unlock();
646 dl 1.6 assert(t.isInterrupted());
647 dl 1.2 t.join(SHORT_DELAY_MS);
648     assertFalse(t.isAlive());
649     }
650     catch (Exception ex) {
651 dl 1.6 unexpectedException();
652 dl 1.2 }
653     }
654    
655 dl 1.6 /**
656     * await is interruptible
657     */
658 dl 1.2 public void testAwait_Interrupt() {
659     final ReentrantLock lock = new ReentrantLock();
660     final Condition c = lock.newCondition();
661     Thread t = new Thread(new Runnable() {
662     public void run() {
663     try {
664     lock.lock();
665     c.await();
666     lock.unlock();
667 dl 1.6 threadShouldThrow();
668 dl 1.2 }
669     catch(InterruptedException success) {
670     }
671     }
672     });
673    
674     try {
675     t.start();
676     Thread.sleep(SHORT_DELAY_MS);
677     t.interrupt();
678     t.join(SHORT_DELAY_MS);
679     assertFalse(t.isAlive());
680     }
681     catch (Exception ex) {
682 dl 1.6 unexpectedException();
683 dl 1.2 }
684     }
685    
686 dl 1.6 /**
687     * awaitNanos is interruptible
688     */
689 dl 1.2 public void testAwaitNanos_Interrupt() {
690     final ReentrantLock lock = new ReentrantLock();
691     final Condition c = lock.newCondition();
692     Thread t = new Thread(new Runnable() {
693     public void run() {
694     try {
695     lock.lock();
696     c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
697     lock.unlock();
698 dl 1.6 threadShouldThrow();
699 dl 1.2 }
700     catch(InterruptedException success) {
701     }
702     }
703     });
704    
705     try {
706     t.start();
707     Thread.sleep(SHORT_DELAY_MS);
708     t.interrupt();
709     t.join(SHORT_DELAY_MS);
710     assertFalse(t.isAlive());
711     }
712     catch (Exception ex) {
713 dl 1.6 unexpectedException();
714 dl 1.2 }
715     }
716    
717 dl 1.6 /**
718     * awaitUntil is interruptible
719     */
720 dl 1.2 public void testAwaitUntil_Interrupt() {
721     final ReentrantLock lock = new ReentrantLock();
722     final Condition c = lock.newCondition();
723     Thread t = new Thread(new Runnable() {
724     public void run() {
725     try {
726     lock.lock();
727     java.util.Date d = new java.util.Date();
728     c.awaitUntil(new java.util.Date(d.getTime() + 10000));
729     lock.unlock();
730 dl 1.6 threadShouldThrow();
731 dl 1.2 }
732     catch(InterruptedException success) {
733     }
734     }
735     });
736    
737     try {
738     t.start();
739     Thread.sleep(SHORT_DELAY_MS);
740     t.interrupt();
741     t.join(SHORT_DELAY_MS);
742     assertFalse(t.isAlive());
743     }
744     catch (Exception ex) {
745 dl 1.6 unexpectedException();
746 dl 1.2 }
747     }
748    
749 dl 1.6 /**
750     * signalAll wakes up all threads
751     */
752 dl 1.2 public void testSignalAll() {
753     final ReentrantLock lock = new ReentrantLock();
754     final Condition c = lock.newCondition();
755     Thread t1 = new Thread(new Runnable() {
756     public void run() {
757     try {
758     lock.lock();
759     c.await();
760     lock.unlock();
761     }
762     catch(InterruptedException e) {
763 dl 1.6 threadUnexpectedException();
764 dl 1.2 }
765     }
766     });
767    
768     Thread t2 = new Thread(new Runnable() {
769     public void run() {
770     try {
771     lock.lock();
772     c.await();
773     lock.unlock();
774     }
775     catch(InterruptedException e) {
776 dl 1.6 threadUnexpectedException();
777 dl 1.2 }
778     }
779     });
780    
781     try {
782     t1.start();
783     t2.start();
784     Thread.sleep(SHORT_DELAY_MS);
785     lock.lock();
786     c.signalAll();
787     lock.unlock();
788     t1.join(SHORT_DELAY_MS);
789     t2.join(SHORT_DELAY_MS);
790     assertFalse(t1.isAlive());
791     assertFalse(t2.isAlive());
792     }
793     catch (Exception ex) {
794 dl 1.6 unexpectedException();
795 dl 1.3 }
796     }
797    
798 dl 1.6 /**
799     * A serialized lock deserializes as unlocked
800     */
801 dl 1.3 public void testSerialization() {
802     ReentrantLock l = new ReentrantLock();
803     l.lock();
804     l.unlock();
805    
806     try {
807     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
808     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
809     out.writeObject(l);
810     out.close();
811    
812     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
813     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
814     ReentrantLock r = (ReentrantLock) in.readObject();
815     r.lock();
816     r.unlock();
817     } catch(Exception e){
818     e.printStackTrace();
819 dl 1.6 unexpectedException();
820 dl 1.2 }
821     }
822 dl 1.1
823     }