ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantLockTest.java
Revision: 1.6
Committed: Sat Sep 20 18:20:08 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.5: +168 -86 lines
Log Message:
Documentation scaffolding

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