ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantReadWriteLockTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ReentrantReadWriteLockTest.java (file contents):
Revision 1.2 by dl, Sun Sep 7 20:39:11 2003 UTC vs.
Revision 1.37 by jsr166, Sat Nov 21 10:25:05 2009 UTC

# Line 1 | Line 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.
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.
7   */
8  
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  
16 < public class ReentrantReadWriteLockTest extends TestCase {
14 <    static int HOLD_COUNT_TEST_LIMIT = 20;
15 <    
16 > public class ReentrantReadWriteLockTest extends JSR166TestCase {
17      public static void main(String[] args) {
18 <        junit.textui.TestRunner.run (suite());  
18 >        junit.textui.TestRunner.run (suite());
19      }
19    
20      public static Test suite() {
21 <        return new TestSuite(ReentrantReadWriteLockTest.class);
21 >        return new TestSuite(ReentrantReadWriteLockTest.class);
22      }
23  
24 +    /**
25 +     * A runnable calling lockInterruptibly
26 +     */
27 +    class InterruptibleLockRunnable extends CheckedRunnable {
28 +        final ReentrantReadWriteLock lock;
29 +        InterruptibleLockRunnable(ReentrantReadWriteLock l) { lock = l; }
30 +        public void realRun() throws InterruptedException {
31 +            lock.writeLock().lockInterruptibly();
32 +        }
33 +    }
34  
25    private static long SHORT_DELAY_MS = 100;
26    private static long MEDIUM_DELAY_MS = 1000;
27    private static long LONG_DELAY_MS = 10000;
35  
36 <    /*
37 <     * Unlocks an unlocked lock, throws Illegal Monitor State
38 <     *
36 >    /**
37 >     * A runnable calling lockInterruptibly that expects to be
38 >     * interrupted
39       */
40 <    
41 <    public void testIllegalMonitorStateException(){
42 <        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
43 <        try{
44 <            rl.writeLock().unlock();
45 <            fail("Should of thown Illegal Monitor State Exception");
40 >    class InterruptedLockRunnable extends CheckedInterruptedRunnable {
41 >        final ReentrantReadWriteLock lock;
42 >        InterruptedLockRunnable(ReentrantReadWriteLock l) { lock = l; }
43 >        public void realRun() throws InterruptedException {
44 >            lock.writeLock().lockInterruptibly();
45 >        }
46 >    }
47  
48 <        }catch(IllegalMonitorStateException success){}
48 >    /**
49 >     * Subclass to expose protected methods
50 >     */
51 >    static class PublicReentrantReadWriteLock extends ReentrantReadWriteLock {
52 >        PublicReentrantReadWriteLock() { super(); }
53 >        public Collection<Thread> getQueuedThreads() {
54 >            return super.getQueuedThreads();
55 >        }
56 >        public Collection<Thread> getWaitingThreads(Condition c) {
57 >            return super.getWaitingThreads(c);
58 >        }
59      }
60  
61 +    /**
62 +     * Constructor sets given fairness, and is in unlocked state
63 +     */
64 +    public void testConstructor() {
65 +        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
66 +        assertFalse(rl.isFair());
67 +        assertFalse(rl.isWriteLocked());
68 +        assertEquals(0, rl.getReadLockCount());
69 +        ReentrantReadWriteLock r2 = new ReentrantReadWriteLock(true);
70 +        assertTrue(r2.isFair());
71 +        assertFalse(r2.isWriteLocked());
72 +        assertEquals(0, r2.getReadLockCount());
73 +        ReentrantReadWriteLock r3 = new ReentrantReadWriteLock(false);
74 +        assertFalse(r3.isFair());
75 +        assertFalse(r3.isWriteLocked());
76 +        assertEquals(0, r3.getReadLockCount());
77 +    }
78  
79 +    /**
80 +     * write-locking and read-locking an unlocked lock succeed
81 +     */
82 +    public void testLock() {
83 +        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
84 +        rl.writeLock().lock();
85 +        assertTrue(rl.isWriteLocked());
86 +        assertTrue(rl.isWriteLockedByCurrentThread());
87 +        assertTrue(rl.writeLock().isHeldByCurrentThread());
88 +        assertEquals(0, rl.getReadLockCount());
89 +        rl.writeLock().unlock();
90 +        assertFalse(rl.isWriteLocked());
91 +        assertFalse(rl.isWriteLockedByCurrentThread());
92 +        assertFalse(rl.writeLock().isHeldByCurrentThread());
93 +        assertEquals(0, rl.getReadLockCount());
94 +        rl.readLock().lock();
95 +        assertFalse(rl.isWriteLocked());
96 +        assertFalse(rl.isWriteLockedByCurrentThread());
97 +        assertEquals(1, rl.getReadLockCount());
98 +        rl.readLock().unlock();
99 +        assertFalse(rl.isWriteLocked());
100 +        assertFalse(rl.isWriteLockedByCurrentThread());
101 +        assertEquals(0, rl.getReadLockCount());
102 +    }
103  
104 <    public void testInterruptedException(){
105 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
106 <        lock.writeLock().lock();
107 <        Thread t = new Thread(new Runnable() {
108 <                public void run(){
109 <                    try{
110 <                        lock.writeLock().lockInterruptibly();
111 <                        fail("should throw");
112 <                    }catch(InterruptedException success){}
113 <                }
114 <            });
115 <        try {
116 <            t.start();
117 <            t.interrupt();
118 <            lock.writeLock().unlock();
119 <            t.join();
120 <        } catch(Exception e){
121 <            fail("unexpected exception");
122 <        }
123 <    }
124 <
125 <    public void testInterruptedException2(){
126 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
127 <        lock.writeLock().lock();
69 <        Thread t = new Thread(new Runnable() {
70 <                public void run(){
71 <                    try{
72 <                        lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS);
73 <                        fail("should throw");
74 <                    }catch(InterruptedException success){}
75 <                }
76 <            });
77 <        try {
78 <            t.start();
79 <            t.interrupt();
80 <            lock.writeLock().unlock();
81 <            t.join();
82 <        } catch(Exception e){
83 <            fail("unexpected exception");
84 <        }
104 >
105 >    /**
106 >     * locking an unlocked fair lock succeeds
107 >     */
108 >    public void testFairLock() {
109 >        ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true);
110 >        rl.writeLock().lock();
111 >        assertTrue(rl.isWriteLocked());
112 >        assertTrue(rl.isWriteLockedByCurrentThread());
113 >        assertTrue(rl.writeLock().isHeldByCurrentThread());
114 >        assertEquals(0, rl.getReadLockCount());
115 >        rl.writeLock().unlock();
116 >        assertFalse(rl.isWriteLocked());
117 >        assertFalse(rl.isWriteLockedByCurrentThread());
118 >        assertFalse(rl.writeLock().isHeldByCurrentThread());
119 >        assertEquals(0, rl.getReadLockCount());
120 >        rl.readLock().lock();
121 >        assertFalse(rl.isWriteLocked());
122 >        assertFalse(rl.isWriteLockedByCurrentThread());
123 >        assertEquals(1, rl.getReadLockCount());
124 >        rl.readLock().unlock();
125 >        assertFalse(rl.isWriteLocked());
126 >        assertFalse(rl.isWriteLockedByCurrentThread());
127 >        assertEquals(0, rl.getReadLockCount());
128      }
129  
130 <    public void testInterruptedException3(){
131 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
132 <        lock.writeLock().lock();
133 <        Thread t = new Thread(new Runnable() {
134 <                public void run(){
135 <                    try{
136 <                        lock.readLock().lockInterruptibly();
137 <                        fail("should throw");
138 <                    }catch(InterruptedException success){}
139 <                }
97 <            });
98 <        try {
99 <            t.start();
100 <            t.interrupt();
130 >    /**
131 >     * getWriteHoldCount returns number of recursive holds
132 >     */
133 >    public void testGetWriteHoldCount() {
134 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
135 >        for (int i = 1; i <= SIZE; i++) {
136 >            lock.writeLock().lock();
137 >            assertEquals(i,lock.getWriteHoldCount());
138 >        }
139 >        for (int i = SIZE; i > 0; i--) {
140              lock.writeLock().unlock();
141 <            t.join();
103 <        } catch(Exception e){
104 <            fail("unexpected exception");
105 <        }
106 <    }
107 <
108 <    public void testInterruptedException4(){
109 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
110 <        lock.writeLock().lock();
111 <        Thread t = new Thread(new Runnable() {
112 <                public void run(){
113 <                    try{
114 <                        lock.readLock().tryLock(1000,TimeUnit.MILLISECONDS);
115 <                        fail("should throw");
116 <                    }catch(InterruptedException success){}
117 <                }
118 <            });
119 <        try {
120 <            t.start();
121 <            t.interrupt();
122 <            t.join();
123 <        } catch(Exception e){
124 <            fail("unexpected exception");
141 >            assertEquals(i-1,lock.getWriteHoldCount());
142          }
143      }
144  
145 <    
146 <    public void testTryLockWhenLocked() {
147 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
148 <        lock.writeLock().lock();
149 <        Thread t = new Thread(new Runnable() {
150 <                public void run(){
151 <                    assertFalse(lock.writeLock().tryLock());
152 <                }
136 <            });
137 <        try {
138 <            t.start();
139 <            t.join();
140 <            lock.writeLock().unlock();
141 <        } catch(Exception e){
142 <            fail("unexpected exception");
145 >    /**
146 >     * WriteLock.getHoldCount returns number of recursive holds
147 >     */
148 >    public void testGetHoldCount() {
149 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
150 >        for (int i = 1; i <= SIZE; i++) {
151 >            lock.writeLock().lock();
152 >            assertEquals(i,lock.writeLock().getHoldCount());
153          }
154 <    }
145 <
146 <    public void testTryLockWhenLocked2() {
147 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
148 <        lock.writeLock().lock();
149 <        Thread t = new Thread(new Runnable() {
150 <                public void run(){
151 <                    assertFalse(lock.readLock().tryLock());
152 <                }
153 <            });
154 <        try {
155 <            t.start();
156 <            t.join();
154 >        for (int i = SIZE; i > 0; i--) {
155              lock.writeLock().unlock();
156 <        } catch(Exception e){
159 <            fail("unexpected exception");
156 >            assertEquals(i-1,lock.writeLock().getHoldCount());
157          }
158 <    }
158 >    }
159  
160 <    public void testMultipleReadLocks() {
161 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
162 <        lock.readLock().lock();
163 <        Thread t = new Thread(new Runnable() {
164 <                public void run(){
165 <                    assertTrue(lock.readLock().tryLock());
166 <                    lock.readLock().unlock();
167 <                }
171 <            });
172 <        try {
173 <            t.start();
174 <            t.join();
175 <            lock.readLock().unlock();
176 <        } catch(Exception e){
177 <            fail("unexpected exception");
160 >    /**
161 >     * getReadHoldCount returns number of recursive holds
162 >     */
163 >    public void testGetReadHoldCount() {
164 >        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
165 >        for (int i = 1; i <= SIZE; i++) {
166 >            lock.readLock().lock();
167 >            assertEquals(i,lock.getReadHoldCount());
168          }
169 <    }
180 <
181 <    public void testWriteAfterMultipleReadLocks() {
182 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
183 <        lock.readLock().lock();
184 <        Thread t1 = new Thread(new Runnable() {
185 <                public void run(){
186 <                    lock.readLock().lock();
187 <                    lock.readLock().unlock();
188 <                }
189 <            });
190 <        Thread t2 = new Thread(new Runnable() {
191 <                public void run(){
192 <                    lock.writeLock().lock();
193 <                    lock.writeLock().unlock();
194 <                }
195 <            });
196 <
197 <        try {
198 <            t1.start();
199 <            t2.start();
200 <            Thread.sleep(SHORT_DELAY_MS);
169 >        for (int i = SIZE; i > 0; i--) {
170              lock.readLock().unlock();
171 <            t1.join(MEDIUM_DELAY_MS);
203 <            t2.join(MEDIUM_DELAY_MS);
204 <            assertTrue(!t1.isAlive());
205 <            assertTrue(!t2.isAlive());
206 <          
207 <        } catch(Exception e){
208 <            fail("unexpected exception");
209 <        }
210 <    }
211 <
212 <    public void testReadAfterWriteLock() {
213 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
214 <        lock.writeLock().lock();
215 <        Thread t1 = new Thread(new Runnable() {
216 <                public void run(){
217 <                    lock.readLock().lock();
218 <                    lock.readLock().unlock();
219 <                }
220 <            });
221 <        Thread t2 = new Thread(new Runnable() {
222 <                public void run(){
223 <                    lock.readLock().lock();
224 <                    lock.readLock().unlock();
225 <                }
226 <            });
227 <
228 <        try {
229 <            t1.start();
230 <            t2.start();
231 <            Thread.sleep(SHORT_DELAY_MS);
232 <            lock.writeLock().unlock();
233 <            t1.join(MEDIUM_DELAY_MS);
234 <            t2.join(MEDIUM_DELAY_MS);
235 <            assertTrue(!t1.isAlive());
236 <            assertTrue(!t2.isAlive());
237 <          
238 <        } catch(Exception e){
239 <            fail("unexpected exception");
240 <        }
241 <    }
242 <
243 <
244 <    public void testTryLockWhenReadLocked() {
245 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
246 <        lock.readLock().lock();
247 <        Thread t = new Thread(new Runnable() {
248 <                public void run(){
249 <                    assertTrue(lock.readLock().tryLock());
250 <                    lock.readLock().unlock();
251 <                }
252 <            });
253 <        try {
254 <            t.start();
255 <            t.join();
256 <            lock.readLock().unlock();
257 <        } catch(Exception e){
258 <            fail("unexpected exception");
171 >            assertEquals(i-1,lock.getReadHoldCount());
172          }
173 <    }
173 >    }
174  
262    
175  
176 <    public void testWriteTryLockWhenReadLocked() {
177 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
178 <        lock.readLock().lock();
179 <        Thread t = new Thread(new Runnable() {
180 <                public void run(){
269 <                    assertFalse(lock.writeLock().tryLock());
270 <                }
271 <            });
176 >    /**
177 >     * write-unlocking an unlocked lock throws IllegalMonitorStateException
178 >     */
179 >    public void testUnlock_IllegalMonitorStateException() {
180 >        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
181          try {
182 <            t.start();
183 <            t.join();
184 <            lock.readLock().unlock();
185 <        } catch(Exception e){
277 <            fail("unexpected exception");
278 <        }
279 <    }
182 >            rl.writeLock().unlock();
183 >            shouldThrow();
184 >        } catch (IllegalMonitorStateException success) {}
185 >    }
186  
281    
187  
188 <    public void testTryLock_Timeout(){
189 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
190 <        lock.writeLock().lock();
191 <        Thread t = new Thread(new Runnable() {
192 <                public void run(){
193 <                    try {
194 <                        assertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS));
195 <                    } catch (Exception ex) {
196 <                        fail("unexpected exception");
197 <                    }
198 <                }
199 <            });
200 <        try {
201 <            t.start();
202 <            t.join();
203 <            lock.writeLock().unlock();
204 <        } catch(Exception e){
205 <            fail("unexpected exception");
206 <        }
207 <    }
188 >    /**
189 >     * write-lockInterruptibly is interruptible
190 >     */
191 >    public void testWriteLockInterruptibly_Interrupted() throws Exception {
192 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
193 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
194 >            public void realRun() throws InterruptedException {
195 >                lock.writeLock().lockInterruptibly();
196 >                lock.writeLock().unlock();
197 >                lock.writeLock().lockInterruptibly();
198 >                lock.writeLock().unlock();
199 >            }});
200 >
201 >        lock.writeLock().lock();
202 >        t.start();
203 >        Thread.sleep(SHORT_DELAY_MS);
204 >        t.interrupt();
205 >        Thread.sleep(SHORT_DELAY_MS);
206 >        lock.writeLock().unlock();
207 >        t.join();
208 >    }
209  
210 <    public void testTryLock_Timeout2(){
211 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
212 <        lock.writeLock().lock();
213 <        Thread t = new Thread(new Runnable() {
214 <                public void run(){
215 <                    try {
216 <                        assertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS));
217 <                    } catch (Exception ex) {
218 <                        fail("unexpected exception");
219 <                    }
220 <                }
221 <            });
222 <        try {
223 <            t.start();
224 <            t.join();
225 <            lock.writeLock().unlock();
320 <        } catch(Exception e){
321 <            fail("unexpected exception");
322 <        }
323 <    }
210 >    /**
211 >     * timed write-tryLock is interruptible
212 >     */
213 >    public void testWriteTryLock_Interrupted() throws InterruptedException {
214 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
215 >        lock.writeLock().lock();
216 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
217 >            public void realRun() throws InterruptedException {
218 >                lock.writeLock().tryLock(1000,MILLISECONDS);
219 >            }});
220 >
221 >        t.start();
222 >        t.interrupt();
223 >        lock.writeLock().unlock();
224 >        t.join();
225 >    }
226  
227 +    /**
228 +     * read-lockInterruptibly is interruptible
229 +     */
230 +    public void testReadLockInterruptibly_Interrupted() throws InterruptedException {
231 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
232 +        lock.writeLock().lock();
233 +        Thread t = new Thread(new CheckedInterruptedRunnable() {
234 +            public void realRun() throws InterruptedException {
235 +                lock.readLock().lockInterruptibly();
236 +            }});
237 +
238 +        t.start();
239 +        Thread.sleep(SHORT_DELAY_MS);
240 +        t.interrupt();
241 +        Thread.sleep(SHORT_DELAY_MS);
242 +        lock.writeLock().unlock();
243 +        t.join();
244 +    }
245  
246 <    public void testLockInterruptibly() {
247 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
248 <        try {
249 <            lock.writeLock().lockInterruptibly();
250 <        } catch(Exception e) {
251 <            fail("unexpected exception");
252 <        }
253 <        Thread t = new Thread(new Runnable() {
254 <                public void run() {
255 <                    try {
256 <                        lock.writeLock().lockInterruptibly();
257 <                        fail("should throw");
258 <                    }
259 <                    catch(InterruptedException success) {
340 <                    }
341 <                }
342 <            });
343 <        try {
344 <            t.start();
345 <            t.interrupt();
346 <            t.join();
347 <            lock.writeLock().unlock();
348 <        } catch(Exception e){
349 <            fail("unexpected exception");
350 <        }
246 >    /**
247 >     * timed read-tryLock is interruptible
248 >     */
249 >    public void testReadTryLock_Interrupted() throws InterruptedException {
250 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
251 >        lock.writeLock().lock();
252 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
253 >            public void realRun() throws InterruptedException {
254 >                lock.readLock().tryLock(1000,MILLISECONDS);
255 >            }});
256 >
257 >        t.start();
258 >        t.interrupt();
259 >        t.join();
260      }
261  
262 <    public void testLockInterruptibly2() {
263 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
264 <        try {
265 <            lock.writeLock().lockInterruptibly();
266 <        } catch(Exception e) {
267 <            fail("unexpected exception");
268 <        }
269 <        Thread t = new Thread(new Runnable() {
270 <                public void run() {
271 <                    try {
272 <                        lock.readLock().lockInterruptibly();
273 <                        fail("should throw");
274 <                    }
275 <                    catch(InterruptedException success) {
276 <                    }
368 <                }
369 <            });
370 <        try {
371 <            t.start();
372 <            t.interrupt();
373 <            t.join();
374 <            lock.writeLock().unlock();
375 <        } catch(Exception e){
376 <            fail("unexpected exception");
377 <        }
262 >
263 >    /**
264 >     * write-tryLock fails if locked
265 >     */
266 >    public void testWriteTryLockWhenLocked() throws InterruptedException {
267 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
268 >        lock.writeLock().lock();
269 >        Thread t = new Thread(new CheckedRunnable() {
270 >            public void realRun() {
271 >                threadAssertFalse(lock.writeLock().tryLock());
272 >            }});
273 >
274 >        t.start();
275 >        t.join();
276 >        lock.writeLock().unlock();
277      }
278  
279 <    public void testAwait_IllegalMonitor() {
280 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
279 >    /**
280 >     * read-tryLock fails if locked
281 >     */
282 >    public void testReadTryLockWhenLocked() throws InterruptedException {
283 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
284 >        lock.writeLock().lock();
285 >        Thread t = new Thread(new CheckedRunnable() {
286 >            public void realRun() {
287 >                threadAssertFalse(lock.readLock().tryLock());
288 >            }});
289 >
290 >        t.start();
291 >        t.join();
292 >        lock.writeLock().unlock();
293 >    }
294 >
295 >    /**
296 >     * Multiple threads can hold a read lock when not write-locked
297 >     */
298 >    public void testMultipleReadLocks() throws InterruptedException {
299 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
300 >        lock.readLock().lock();
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();
309 >        lock.readLock().unlock();
310 >    }
311 >
312 >    /**
313 >     * A writelock succeeds after reading threads unlock
314 >     */
315 >    public void testWriteAfterMultipleReadLocks() throws InterruptedException {
316 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
317 >        lock.readLock().lock();
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();
331 >        Thread.sleep(SHORT_DELAY_MS);
332 >        lock.readLock().unlock();
333 >        t1.join(MEDIUM_DELAY_MS);
334 >        t2.join(MEDIUM_DELAY_MS);
335 >        assertTrue(!t1.isAlive());
336 >        assertTrue(!t2.isAlive());
337 >    }
338 >
339 >    /**
340 >     * Readlocks succeed after a writing thread unlocks
341 >     */
342 >    public void testReadAfterWriteLock() throws InterruptedException {
343 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
344 >        lock.writeLock().lock();
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();
358 >        Thread.sleep(SHORT_DELAY_MS);
359 >        lock.writeLock().unlock();
360 >        t1.join(MEDIUM_DELAY_MS);
361 >        t2.join(MEDIUM_DELAY_MS);
362 >        assertTrue(!t1.isAlive());
363 >        assertTrue(!t2.isAlive());
364 >    }
365 >
366 >    /**
367 >     * Read trylock succeeds if write locked by current thread
368 >     */
369 >    public void testReadHoldingWriteLock() {
370 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
371 >        lock.writeLock().lock();
372 >        assertTrue(lock.readLock().tryLock());
373 >        lock.readLock().unlock();
374 >        lock.writeLock().unlock();
375 >    }
376 >
377 >    /**
378 >     * Read lock succeeds if write locked by current thread even if
379 >     * other threads are waiting for readlock
380 >     */
381 >    public void testReadHoldingWriteLock2() throws InterruptedException {
382 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
383 >        lock.writeLock().lock();
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();
397 >        lock.readLock().lock();
398 >        lock.readLock().unlock();
399 >        Thread.sleep(SHORT_DELAY_MS);
400 >        lock.readLock().lock();
401 >        lock.readLock().unlock();
402 >        lock.writeLock().unlock();
403 >        t1.join(MEDIUM_DELAY_MS);
404 >        t2.join(MEDIUM_DELAY_MS);
405 >        assertTrue(!t1.isAlive());
406 >        assertTrue(!t2.isAlive());
407 >    }
408 >
409 >    /**
410 >     *  Read lock succeeds if write locked by current thread even if
411 >     * other threads are waiting for writelock
412 >     */
413 >    public void testReadHoldingWriteLock3() throws InterruptedException {
414 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
415 >        lock.writeLock().lock();
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();
429 >        lock.readLock().lock();
430 >        lock.readLock().unlock();
431 >        Thread.sleep(SHORT_DELAY_MS);
432 >        lock.readLock().lock();
433 >        lock.readLock().unlock();
434 >        lock.writeLock().unlock();
435 >        t1.join(MEDIUM_DELAY_MS);
436 >        t2.join(MEDIUM_DELAY_MS);
437 >        assertTrue(!t1.isAlive());
438 >        assertTrue(!t2.isAlive());
439 >    }
440 >
441 >
442 >    /**
443 >     *  Write lock succeeds if write locked by current thread even if
444 >     * other threads are waiting for writelock
445 >     */
446 >    public void testWriteHoldingWriteLock4() throws InterruptedException {
447 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
448 >        lock.writeLock().lock();
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();
462 >        lock.writeLock().lock();
463 >        lock.writeLock().unlock();
464 >        Thread.sleep(SHORT_DELAY_MS);
465 >        lock.writeLock().lock();
466 >        lock.writeLock().unlock();
467 >        lock.writeLock().unlock();
468 >        t1.join(MEDIUM_DELAY_MS);
469 >        t2.join(MEDIUM_DELAY_MS);
470 >        assertTrue(!t1.isAlive());
471 >        assertTrue(!t2.isAlive());
472 >    }
473 >
474 >
475 >    /**
476 >     * Fair Read trylock succeeds if write locked by current thread
477 >     */
478 >    public void testReadHoldingWriteLockFair() {
479 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
480 >        lock.writeLock().lock();
481 >        assertTrue(lock.readLock().tryLock());
482 >        lock.readLock().unlock();
483 >        lock.writeLock().unlock();
484 >    }
485 >
486 >    /**
487 >     * Fair Read lock succeeds if write locked by current thread even if
488 >     * other threads are waiting for readlock
489 >     */
490 >    public void testReadHoldingWriteLockFair2() throws InterruptedException {
491 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
492 >        lock.writeLock().lock();
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();
506 >        lock.readLock().lock();
507 >        lock.readLock().unlock();
508 >        Thread.sleep(SHORT_DELAY_MS);
509 >        lock.readLock().lock();
510 >        lock.readLock().unlock();
511 >        lock.writeLock().unlock();
512 >        t1.join(MEDIUM_DELAY_MS);
513 >        t2.join(MEDIUM_DELAY_MS);
514 >        assertTrue(!t1.isAlive());
515 >        assertTrue(!t2.isAlive());
516 >    }
517 >
518 >
519 >    /**
520 >     * Fair Read lock succeeds if write locked by current thread even if
521 >     * other threads are waiting for writelock
522 >     */
523 >    public void testReadHoldingWriteLockFair3() throws InterruptedException {
524 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
525 >        lock.writeLock().lock();
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();
539 >        lock.readLock().lock();
540 >        lock.readLock().unlock();
541 >        Thread.sleep(SHORT_DELAY_MS);
542 >        lock.readLock().lock();
543 >        lock.readLock().unlock();
544 >        lock.writeLock().unlock();
545 >        t1.join(MEDIUM_DELAY_MS);
546 >        t2.join(MEDIUM_DELAY_MS);
547 >        assertTrue(!t1.isAlive());
548 >        assertTrue(!t2.isAlive());
549 >    }
550 >
551 >
552 >    /**
553 >     * Fair Write lock succeeds if write locked by current thread even if
554 >     * other threads are waiting for writelock
555 >     */
556 >    public void testWriteHoldingWriteLockFair4() throws InterruptedException {
557 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
558 >        lock.writeLock().lock();
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();
572 >        Thread.sleep(SHORT_DELAY_MS);
573 >        assertTrue(lock.isWriteLockedByCurrentThread());
574 >        assertTrue(lock.getWriteHoldCount() == 1);
575 >        lock.writeLock().lock();
576 >        assertTrue(lock.getWriteHoldCount() == 2);
577 >        lock.writeLock().unlock();
578 >        lock.writeLock().lock();
579 >        lock.writeLock().unlock();
580 >        lock.writeLock().unlock();
581 >        t1.join(MEDIUM_DELAY_MS);
582 >        t2.join(MEDIUM_DELAY_MS);
583 >        assertTrue(!t1.isAlive());
584 >        assertTrue(!t2.isAlive());
585 >    }
586 >
587 >
588 >    /**
589 >     * Read tryLock succeeds if readlocked but not writelocked
590 >     */
591 >    public void testTryLockWhenReadLocked() throws InterruptedException {
592 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
593 >        lock.readLock().lock();
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();
602 >        lock.readLock().unlock();
603 >    }
604 >
605 >
606 >
607 >    /**
608 >     * write tryLock fails when readlocked
609 >     */
610 >    public void testWriteTryLockWhenReadLocked() throws InterruptedException {
611 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
612 >        lock.readLock().lock();
613 >        Thread t = new Thread(new CheckedRunnable() {
614 >            public void realRun() {
615 >                threadAssertFalse(lock.writeLock().tryLock());
616 >            }});
617 >
618 >        t.start();
619 >        t.join();
620 >        lock.readLock().unlock();
621 >    }
622 >
623 >
624 >    /**
625 >     * Fair Read tryLock succeeds if readlocked but not writelocked
626 >     */
627 >    public void testTryLockWhenReadLockedFair() throws InterruptedException {
628 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
629 >        lock.readLock().lock();
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();
638 >        lock.readLock().unlock();
639 >    }
640 >
641 >
642 >
643 >    /**
644 >     * Fair write tryLock fails when readlocked
645 >     */
646 >    public void testWriteTryLockWhenReadLockedFair() throws InterruptedException {
647 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
648 >        lock.readLock().lock();
649 >        Thread t = new Thread(new CheckedRunnable() {
650 >            public void realRun() {
651 >                threadAssertFalse(lock.writeLock().tryLock());
652 >            }});
653 >
654 >        t.start();
655 >        t.join();
656 >        lock.readLock().unlock();
657 >    }
658 >
659 >
660 >
661 >    /**
662 >     * write timed tryLock times out if locked
663 >     */
664 >    public void testWriteTryLock_Timeout() throws InterruptedException {
665 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
666 >        lock.writeLock().lock();
667 >        Thread t = new Thread(new CheckedRunnable() {
668 >            public void realRun() throws InterruptedException {
669 >                threadAssertFalse(lock.writeLock().tryLock(1, MILLISECONDS));
670 >            }});
671 >
672 >        t.start();
673 >        t.join();
674 >        assertTrue(lock.writeLock().isHeldByCurrentThread());
675 >        lock.writeLock().unlock();
676 >    }
677 >
678 >    /**
679 >     * read timed tryLock times out if write-locked
680 >     */
681 >    public void testReadTryLock_Timeout() throws InterruptedException {
682 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
683 >        lock.writeLock().lock();
684 >        Thread t = new Thread(new CheckedRunnable() {
685 >            public void realRun() throws InterruptedException {
686 >                threadAssertFalse(lock.readLock().tryLock(1, MILLISECONDS));
687 >            }});
688 >
689 >        t.start();
690 >        t.join();
691 >        assertTrue(lock.writeLock().isHeldByCurrentThread());
692 >        lock.writeLock().unlock();
693 >    }
694 >
695 >
696 >    /**
697 >     * write lockInterruptibly succeeds if lock free else is interruptible
698 >     */
699 >    public void testWriteLockInterruptibly() throws InterruptedException {
700 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
701 >        lock.writeLock().lockInterruptibly();
702 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
703 >            public void realRun() throws InterruptedException {
704 >                lock.writeLock().lockInterruptibly();
705 >            }});
706 >
707 >        t.start();
708 >        Thread.sleep(SHORT_DELAY_MS);
709 >        t.interrupt();
710 >        Thread.sleep(SHORT_DELAY_MS);
711 >        t.join();
712 >        lock.writeLock().unlock();
713 >    }
714 >
715 >    /**
716 >     *  read lockInterruptibly succeeds if lock free else is interruptible
717 >     */
718 >    public void testReadLockInterruptibly() throws InterruptedException {
719 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
720 >        lock.writeLock().lockInterruptibly();
721 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
722 >            public void realRun() throws InterruptedException {
723 >                lock.readLock().lockInterruptibly();
724 >            }});
725 >
726 >        t.start();
727 >        Thread.sleep(SHORT_DELAY_MS);
728 >        t.interrupt();
729 >        t.join();
730 >        lock.writeLock().unlock();
731 >    }
732 >
733 >    /**
734 >     * Calling await without holding lock throws IllegalMonitorStateException
735 >     */
736 >    public void testAwait_IllegalMonitor() throws InterruptedException {
737 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
738          final Condition c = lock.writeLock().newCondition();
739          try {
740              c.await();
741 <            fail("should throw");
742 <        }
387 <        catch (IllegalMonitorStateException success) {
388 <        }
389 <        catch (Exception ex) {
390 <            fail("should throw IMSE");
391 <        }
741 >            shouldThrow();
742 >        } catch (IllegalMonitorStateException success) {}
743      }
744  
745 +    /**
746 +     * Calling signal without holding lock throws IllegalMonitorStateException
747 +     */
748      public void testSignal_IllegalMonitor() {
749 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
749 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
750          final Condition c = lock.writeLock().newCondition();
751          try {
752              c.signal();
753 <            fail("should throw");
754 <        }
401 <        catch (IllegalMonitorStateException success) {
402 <        }
403 <        catch (Exception ex) {
404 <            fail("should throw IMSE");
405 <        }
753 >            shouldThrow();
754 >        } catch (IllegalMonitorStateException success) {}
755      }
756  
757 <    public void testAwaitNanos_Timeout() {
758 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
757 >    /**
758 >     * awaitNanos without a signal times out
759 >     */
760 >    public void testAwaitNanos_Timeout() throws InterruptedException {
761 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
762          final Condition c = lock.writeLock().newCondition();
763 <        try {
764 <            lock.writeLock().lock();
765 <            long t = c.awaitNanos(100);
766 <            assertTrue(t <= 0);
767 <            lock.writeLock().unlock();
416 <        }
417 <        catch (Exception ex) {
418 <            fail("unexpected exception");
419 <        }
763 >
764 >        lock.writeLock().lock();
765 >        long t = c.awaitNanos(100);
766 >        assertTrue(t <= 0);
767 >        lock.writeLock().unlock();
768      }
769  
770 <    public void testAwait_Timeout() {
771 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
770 >
771 >    /**
772 >     *  timed await without a signal times out
773 >     */
774 >    public void testAwait_Timeout() throws InterruptedException {
775 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
776          final Condition c = lock.writeLock().newCondition();
777 <        try {
778 <            lock.writeLock().lock();
779 <            assertFalse(c.await(10, TimeUnit.MILLISECONDS));
428 <            lock.writeLock().unlock();
429 <        }
430 <        catch (Exception ex) {
431 <            fail("unexpected exception");
432 <        }
777 >        lock.writeLock().lock();
778 >        assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS));
779 >        lock.writeLock().unlock();
780      }
781  
782 <    public void testAwaitUntil_Timeout() {
783 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
782 >    /**
783 >     * awaitUntil without a signal times out
784 >     */
785 >    public void testAwaitUntil_Timeout() throws InterruptedException {
786 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
787          final Condition c = lock.writeLock().newCondition();
788 <        try {
789 <            lock.writeLock().lock();
790 <            java.util.Date d = new java.util.Date();
791 <            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
442 <            lock.writeLock().unlock();
443 <        }
444 <        catch (Exception ex) {
445 <            fail("unexpected exception");
446 <        }
788 >        lock.writeLock().lock();
789 >        java.util.Date d = new java.util.Date();
790 >        assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
791 >        lock.writeLock().unlock();
792      }
793  
794 <    public void testAwait() {
795 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
794 >    /**
795 >     * await returns when signalled
796 >     */
797 >    public void testAwait() throws InterruptedException {
798 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
799          final Condition c = lock.writeLock().newCondition();
800 <        Thread t = new Thread(new Runnable() {
801 <                public void run() {
802 <                    try {
803 <                        lock.writeLock().lock();
804 <                        c.await();
805 <                        lock.writeLock().unlock();
806 <                    }
807 <                    catch(InterruptedException e) {
808 <                        fail("unexpected exception");
809 <                    }
810 <                }
811 <            });
800 >        Thread t = new Thread(new CheckedRunnable() {
801 >            public void realRun() throws InterruptedException {
802 >                lock.writeLock().lock();
803 >                c.await();
804 >                lock.writeLock().unlock();
805 >            }});
806 >
807 >        t.start();
808 >        Thread.sleep(SHORT_DELAY_MS);
809 >        lock.writeLock().lock();
810 >        c.signal();
811 >        lock.writeLock().unlock();
812 >        t.join(SHORT_DELAY_MS);
813 >        assertFalse(t.isAlive());
814 >    }
815  
816 <        try {
817 <            t.start();
818 <            Thread.sleep(SHORT_DELAY_MS);
819 <            lock.writeLock().lock();
820 <            c.signal();
821 <            lock.writeLock().unlock();
822 <            t.join(SHORT_DELAY_MS);
823 <            assertFalse(t.isAlive());
824 <        }
825 <        catch (Exception ex) {
826 <            fail("unexpected exception");
816 >    /** A helper class for uninterruptible wait tests */
817 >    class UninterruptableThread extends Thread {
818 >        private Lock lock;
819 >        private Condition c;
820 >
821 >        public volatile boolean canAwake = false;
822 >        public volatile boolean interrupted = false;
823 >        public volatile boolean lockStarted = false;
824 >
825 >        public UninterruptableThread(Lock lock, Condition c) {
826 >            this.lock = lock;
827 >            this.c = c;
828 >        }
829 >
830 >        public synchronized void run() {
831 >            lock.lock();
832 >            lockStarted = true;
833 >
834 >            while (!canAwake) {
835 >                c.awaitUninterruptibly();
836 >            }
837 >
838 >            interrupted = isInterrupted();
839 >            lock.unlock();
840          }
841      }
842  
843 <    public void testAwaitUninterruptibly() {
844 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
843 >    /**
844 >     * awaitUninterruptibly doesn't abort on interrupt
845 >     */
846 >    public void testAwaitUninterruptibly() throws InterruptedException {
847 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
848          final Condition c = lock.writeLock().newCondition();
849 <        Thread t = new Thread(new Runnable() {
850 <                public void run() {
851 <                    lock.writeLock().lock();
852 <                    c.awaitUninterruptibly();
853 <                    lock.writeLock().unlock();
854 <                }
855 <            });
849 >        UninterruptableThread thread = new UninterruptableThread(lock.writeLock(), c);
850 >
851 >        thread.start();
852 >
853 >        while (!thread.lockStarted) {
854 >            Thread.sleep(100);
855 >        }
856  
857 +        lock.writeLock().lock();
858          try {
859 <            t.start();
860 <            Thread.sleep(SHORT_DELAY_MS);
493 <            t.interrupt();
494 <            lock.writeLock().lock();
859 >            thread.interrupt();
860 >            thread.canAwake = true;
861              c.signal();
862 +        } finally {
863              lock.writeLock().unlock();
497            t.join(SHORT_DELAY_MS);
498            assertFalse(t.isAlive());
499        }
500        catch (Exception ex) {
501            fail("unexpected exception");
864          }
865 +
866 +        thread.join();
867 +        assertTrue(thread.interrupted);
868 +        assertFalse(thread.isAlive());
869      }
870  
871 <    public void testAwait_Interrupt() {
872 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
873 <        final Condition c = lock.writeLock().newCondition();
874 <        Thread t = new Thread(new Runnable() {
875 <                public void run() {
876 <                    try {
877 <                        lock.writeLock().lock();
878 <                        c.await();
879 <                        lock.writeLock().unlock();
880 <                        fail("should throw");
881 <                    }
882 <                    catch(InterruptedException success) {
883 <                    }
884 <                }
885 <            });
886 <
887 <        try {
888 <            t.start();
889 <            Thread.sleep(SHORT_DELAY_MS);
890 <            t.interrupt();
891 <            t.join(SHORT_DELAY_MS);
892 <            assertFalse(t.isAlive());
893 <        }
894 <        catch (Exception ex) {
895 <            fail("unexpected exception");
896 <        }
897 <    }
898 <
899 <    public void testAwaitNanos_Interrupt() {
900 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
901 <        final Condition c = lock.writeLock().newCondition();
902 <        Thread t = new Thread(new Runnable() {
903 <                public void run() {
904 <                    try {
905 <                        lock.writeLock().lock();
906 <                        c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
907 <                        lock.writeLock().unlock();
908 <                        fail("should throw");
909 <                    }
910 <                    catch(InterruptedException success) {
911 <                    }
912 <                }
913 <            });
914 <
915 <        try {
916 <            t.start();
917 <            Thread.sleep(SHORT_DELAY_MS);
918 <            t.interrupt();
919 <            t.join(SHORT_DELAY_MS);
920 <            assertFalse(t.isAlive());
921 <        }
922 <        catch (Exception ex) {
923 <            fail("unexpected exception");
924 <        }
925 <    }
926 <
927 <    public void testAwaitUntil_Interrupt() {
928 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
929 <        final Condition c = lock.writeLock().newCondition();
564 <        Thread t = new Thread(new Runnable() {
565 <                public void run() {
566 <                    try {
567 <                        lock.writeLock().lock();
568 <                        java.util.Date d = new java.util.Date();
569 <                        c.awaitUntil(new java.util.Date(d.getTime() + 10000));
570 <                        lock.writeLock().unlock();
571 <                        fail("should throw");
572 <                    }
573 <                    catch(InterruptedException success) {
574 <                    }
575 <                }
576 <            });
577 <
578 <        try {
579 <            t.start();
580 <            Thread.sleep(SHORT_DELAY_MS);
581 <            t.interrupt();
582 <            t.join(SHORT_DELAY_MS);
583 <            assertFalse(t.isAlive());
584 <        }
585 <        catch (Exception ex) {
586 <            fail("unexpected exception");
587 <        }
588 <    }
589 <
590 <    public void testSignalAll() {
591 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
592 <        final Condition c = lock.writeLock().newCondition();
593 <        Thread t1 = new Thread(new Runnable() {
594 <                public void run() {
595 <                    try {
596 <                        lock.writeLock().lock();
597 <                        c.await();
598 <                        lock.writeLock().unlock();
599 <                    }
600 <                    catch(InterruptedException e) {
601 <                        fail("unexpected exception");
602 <                    }
603 <                }
604 <            });
605 <
606 <        Thread t2 = new Thread(new Runnable() {
607 <                public void run() {
608 <                    try {
609 <                        lock.writeLock().lock();
610 <                        c.await();
611 <                        lock.writeLock().unlock();
612 <                    }
613 <                    catch(InterruptedException e) {
614 <                        fail("unexpected exception");
615 <                    }
616 <                }
617 <            });
618 <
619 <        try {
620 <            t1.start();
621 <            t2.start();
622 <            Thread.sleep(SHORT_DELAY_MS);
623 <            lock.writeLock().lock();
624 <            c.signalAll();
625 <            lock.writeLock().unlock();
626 <            t1.join(SHORT_DELAY_MS);
627 <            t2.join(SHORT_DELAY_MS);
628 <            assertFalse(t1.isAlive());
629 <            assertFalse(t2.isAlive());
630 <        }
631 <        catch (Exception ex) {
632 <            fail("unexpected exception");
633 <        }
871 >    /**
872 >     * await is interruptible
873 >     */
874 >    public void testAwait_Interrupt() throws InterruptedException {
875 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
876 >        final Condition c = lock.writeLock().newCondition();
877 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
878 >            public void realRun() throws InterruptedException {
879 >                lock.writeLock().lock();
880 >                c.await();
881 >                lock.writeLock().unlock();
882 >            }});
883 >
884 >        t.start();
885 >        Thread.sleep(SHORT_DELAY_MS);
886 >        t.interrupt();
887 >        t.join(SHORT_DELAY_MS);
888 >        assertFalse(t.isAlive());
889 >    }
890 >
891 >    /**
892 >     * awaitNanos is interruptible
893 >     */
894 >    public void testAwaitNanos_Interrupt() throws InterruptedException {
895 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
896 >        final Condition c = lock.writeLock().newCondition();
897 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
898 >            public void realRun() throws InterruptedException {
899 >                lock.writeLock().lock();
900 >                c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
901 >                lock.writeLock().unlock();
902 >            }});
903 >
904 >        t.start();
905 >        Thread.sleep(SHORT_DELAY_MS);
906 >        t.interrupt();
907 >        t.join(SHORT_DELAY_MS);
908 >        assertFalse(t.isAlive());
909 >    }
910 >
911 >    /**
912 >     * awaitUntil is interruptible
913 >     */
914 >    public void testAwaitUntil_Interrupt() throws InterruptedException {
915 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
916 >        final Condition c = lock.writeLock().newCondition();
917 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
918 >            public void realRun() throws InterruptedException {
919 >                lock.writeLock().lock();
920 >                java.util.Date d = new java.util.Date();
921 >                c.awaitUntil(new java.util.Date(d.getTime() + 10000));
922 >                lock.writeLock().unlock();
923 >            }});
924 >
925 >        t.start();
926 >        Thread.sleep(SHORT_DELAY_MS);
927 >        t.interrupt();
928 >        t.join(SHORT_DELAY_MS);
929 >        assertFalse(t.isAlive());
930      }
931  
932 <    public void testSerialization() {
932 >    /**
933 >     * signalAll wakes up all threads
934 >     */
935 >    public void testSignalAll() throws InterruptedException {
936 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
937 >        final Condition c = lock.writeLock().newCondition();
938 >        Thread t1 = new Thread(new CheckedRunnable() {
939 >            public void realRun() throws InterruptedException {
940 >                lock.writeLock().lock();
941 >                c.await();
942 >                lock.writeLock().unlock();
943 >            }});
944 >
945 >        Thread t2 = new Thread(new CheckedRunnable() {
946 >            public void realRun() throws InterruptedException {
947 >                lock.writeLock().lock();
948 >                c.await();
949 >                lock.writeLock().unlock();
950 >            }});
951 >
952 >        t1.start();
953 >        t2.start();
954 >        Thread.sleep(SHORT_DELAY_MS);
955 >        lock.writeLock().lock();
956 >        c.signalAll();
957 >        lock.writeLock().unlock();
958 >        t1.join(SHORT_DELAY_MS);
959 >        t2.join(SHORT_DELAY_MS);
960 >        assertFalse(t1.isAlive());
961 >        assertFalse(t2.isAlive());
962 >    }
963 >
964 >    /**
965 >     * A serialized lock deserializes as unlocked
966 >     */
967 >    public void testSerialization() throws Exception {
968          ReentrantReadWriteLock l = new ReentrantReadWriteLock();
969          l.readLock().lock();
970          l.readLock().unlock();
971  
972 +        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
973 +        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
974 +        out.writeObject(l);
975 +        out.close();
976 +
977 +        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
978 +        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
979 +        ReentrantReadWriteLock r = (ReentrantReadWriteLock) in.readObject();
980 +        r.readLock().lock();
981 +        r.readLock().unlock();
982 +    }
983 +
984 +    /**
985 +     * hasQueuedThreads reports whether there are waiting threads
986 +     */
987 +    public void testhasQueuedThreads() throws InterruptedException {
988 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
989 +        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
990 +        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
991 +        assertFalse(lock.hasQueuedThreads());
992 +        lock.writeLock().lock();
993 +        t1.start();
994 +        Thread.sleep(SHORT_DELAY_MS);
995 +        assertTrue(lock.hasQueuedThreads());
996 +        t2.start();
997 +        Thread.sleep(SHORT_DELAY_MS);
998 +        assertTrue(lock.hasQueuedThreads());
999 +        t1.interrupt();
1000 +        Thread.sleep(SHORT_DELAY_MS);
1001 +        assertTrue(lock.hasQueuedThreads());
1002 +        lock.writeLock().unlock();
1003 +        Thread.sleep(SHORT_DELAY_MS);
1004 +        assertFalse(lock.hasQueuedThreads());
1005 +        t1.join();
1006 +        t2.join();
1007 +    }
1008 +
1009 +    /**
1010 +     * hasQueuedThread(null) throws NPE
1011 +     */
1012 +    public void testHasQueuedThreadNPE() {
1013 +        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1014          try {
1015 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1016 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1017 <            out.writeObject(l);
645 <            out.close();
646 <
647 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
648 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
649 <            ReentrantReadWriteLock r = (ReentrantReadWriteLock) in.readObject();
650 <            r.readLock().lock();
651 <            r.readLock().unlock();
652 <        } catch(Exception e){
653 <            e.printStackTrace();
654 <            fail("unexpected exception");
655 <        }
1015 >            sync.hasQueuedThread(null);
1016 >            shouldThrow();
1017 >        } catch (NullPointerException success) {}
1018      }
1019  
1020 +    /**
1021 +     * hasQueuedThread reports whether a thread is queued.
1022 +     */
1023 +    public void testHasQueuedThread() throws InterruptedException {
1024 +        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1025 +        Thread t1 = new Thread(new InterruptedLockRunnable(sync));
1026 +        Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
1027 +        assertFalse(sync.hasQueuedThread(t1));
1028 +        assertFalse(sync.hasQueuedThread(t2));
1029 +        sync.writeLock().lock();
1030 +        t1.start();
1031 +        Thread.sleep(SHORT_DELAY_MS);
1032 +        assertTrue(sync.hasQueuedThread(t1));
1033 +        t2.start();
1034 +        Thread.sleep(SHORT_DELAY_MS);
1035 +        assertTrue(sync.hasQueuedThread(t1));
1036 +        assertTrue(sync.hasQueuedThread(t2));
1037 +        t1.interrupt();
1038 +        Thread.sleep(SHORT_DELAY_MS);
1039 +        assertFalse(sync.hasQueuedThread(t1));
1040 +        assertTrue(sync.hasQueuedThread(t2));
1041 +        sync.writeLock().unlock();
1042 +        Thread.sleep(SHORT_DELAY_MS);
1043 +        assertFalse(sync.hasQueuedThread(t1));
1044 +        Thread.sleep(SHORT_DELAY_MS);
1045 +        assertFalse(sync.hasQueuedThread(t2));
1046 +        t1.join();
1047 +        t2.join();
1048 +    }
1049 +
1050 +
1051 +    /**
1052 +     * getQueueLength reports number of waiting threads
1053 +     */
1054 +    public void testGetQueueLength() throws InterruptedException {
1055 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1056 +        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1057 +        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1058 +        assertEquals(0, lock.getQueueLength());
1059 +        lock.writeLock().lock();
1060 +        t1.start();
1061 +        Thread.sleep(SHORT_DELAY_MS);
1062 +        assertEquals(1, lock.getQueueLength());
1063 +        t2.start();
1064 +        Thread.sleep(SHORT_DELAY_MS);
1065 +        assertEquals(2, lock.getQueueLength());
1066 +        t1.interrupt();
1067 +        Thread.sleep(SHORT_DELAY_MS);
1068 +        assertEquals(1, lock.getQueueLength());
1069 +        lock.writeLock().unlock();
1070 +        Thread.sleep(SHORT_DELAY_MS);
1071 +        assertEquals(0, lock.getQueueLength());
1072 +        t1.join();
1073 +        t2.join();
1074 +    }
1075 +
1076 +    /**
1077 +     * getQueuedThreads includes waiting threads
1078 +     */
1079 +    public void testGetQueuedThreads() throws InterruptedException {
1080 +        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1081 +        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
1082 +        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
1083 +        assertTrue(lock.getQueuedThreads().isEmpty());
1084 +        lock.writeLock().lock();
1085 +        assertTrue(lock.getQueuedThreads().isEmpty());
1086 +        t1.start();
1087 +        Thread.sleep(SHORT_DELAY_MS);
1088 +        assertTrue(lock.getQueuedThreads().contains(t1));
1089 +        t2.start();
1090 +        Thread.sleep(SHORT_DELAY_MS);
1091 +        assertTrue(lock.getQueuedThreads().contains(t1));
1092 +        assertTrue(lock.getQueuedThreads().contains(t2));
1093 +        t1.interrupt();
1094 +        Thread.sleep(SHORT_DELAY_MS);
1095 +        assertFalse(lock.getQueuedThreads().contains(t1));
1096 +        assertTrue(lock.getQueuedThreads().contains(t2));
1097 +        lock.writeLock().unlock();
1098 +        Thread.sleep(SHORT_DELAY_MS);
1099 +        assertTrue(lock.getQueuedThreads().isEmpty());
1100 +        t1.join();
1101 +        t2.join();
1102 +    }
1103 +
1104 +    /**
1105 +     * hasWaiters throws NPE if null
1106 +     */
1107 +    public void testHasWaitersNPE() {
1108 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1109 +        try {
1110 +            lock.hasWaiters(null);
1111 +            shouldThrow();
1112 +        } catch (NullPointerException success) {}
1113 +    }
1114 +
1115 +    /**
1116 +     * getWaitQueueLength throws NPE if null
1117 +     */
1118 +    public void testGetWaitQueueLengthNPE() {
1119 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1120 +        try {
1121 +            lock.getWaitQueueLength(null);
1122 +            shouldThrow();
1123 +        } catch (NullPointerException success) {}
1124 +    }
1125 +
1126 +
1127 +    /**
1128 +     * getWaitingThreads throws NPE if null
1129 +     */
1130 +    public void testGetWaitingThreadsNPE() {
1131 +        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1132 +        try {
1133 +            lock.getWaitingThreads(null);
1134 +            shouldThrow();
1135 +        } catch (NullPointerException success) {}
1136 +    }
1137 +
1138 +    /**
1139 +     * hasWaiters throws IAE if not owned
1140 +     */
1141 +    public void testHasWaitersIAE() {
1142 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1143 +        final Condition c = lock.writeLock().newCondition();
1144 +        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1145 +        try {
1146 +            lock2.hasWaiters(c);
1147 +            shouldThrow();
1148 +        } catch (IllegalArgumentException success) {}
1149 +    }
1150 +
1151 +    /**
1152 +     * hasWaiters throws IMSE if not locked
1153 +     */
1154 +    public void testHasWaitersIMSE() {
1155 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1156 +        final Condition c = lock.writeLock().newCondition();
1157 +        try {
1158 +            lock.hasWaiters(c);
1159 +            shouldThrow();
1160 +        } catch (IllegalMonitorStateException success) {}
1161 +    }
1162 +
1163 +
1164 +    /**
1165 +     * getWaitQueueLength throws IAE if not owned
1166 +     */
1167 +    public void testGetWaitQueueLengthIAE() {
1168 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1169 +        final Condition c = lock.writeLock().newCondition();
1170 +        final ReentrantReadWriteLock lock2 = new ReentrantReadWriteLock();
1171 +        try {
1172 +            lock2.getWaitQueueLength(c);
1173 +            shouldThrow();
1174 +        } catch (IllegalArgumentException success) {}
1175 +    }
1176 +
1177 +    /**
1178 +     * getWaitQueueLength throws IMSE if not locked
1179 +     */
1180 +    public void testGetWaitQueueLengthIMSE() {
1181 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1182 +        final Condition c = lock.writeLock().newCondition();
1183 +        try {
1184 +            lock.getWaitQueueLength(c);
1185 +            shouldThrow();
1186 +        } catch (IllegalMonitorStateException success) {}
1187 +    }
1188 +
1189 +
1190 +    /**
1191 +     * getWaitingThreads throws IAE if not owned
1192 +     */
1193 +    public void testGetWaitingThreadsIAE() {
1194 +        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1195 +        final Condition c = lock.writeLock().newCondition();
1196 +        final PublicReentrantReadWriteLock lock2 = new PublicReentrantReadWriteLock();
1197 +        try {
1198 +            lock2.getWaitingThreads(c);
1199 +            shouldThrow();
1200 +        } catch (IllegalArgumentException success) {}
1201 +    }
1202 +
1203 +    /**
1204 +     * getWaitingThreads throws IMSE if not locked
1205 +     */
1206 +    public void testGetWaitingThreadsIMSE() {
1207 +        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1208 +        final Condition c = lock.writeLock().newCondition();
1209 +        try {
1210 +            lock.getWaitingThreads(c);
1211 +            shouldThrow();
1212 +        } catch (IllegalMonitorStateException success) {}
1213 +    }
1214 +
1215 +
1216 +    /**
1217 +     * hasWaiters returns true when a thread is waiting, else false
1218 +     */
1219 +    public void testHasWaiters() throws InterruptedException {
1220 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1221 +        final Condition c = lock.writeLock().newCondition();
1222 +        Thread t = new Thread(new CheckedRunnable() {
1223 +            public void realRun() throws InterruptedException {
1224 +                lock.writeLock().lock();
1225 +                threadAssertFalse(lock.hasWaiters(c));
1226 +                threadAssertEquals(0, lock.getWaitQueueLength(c));
1227 +                c.await();
1228 +                lock.writeLock().unlock();
1229 +            }});
1230 +
1231 +        t.start();
1232 +        Thread.sleep(SHORT_DELAY_MS);
1233 +        lock.writeLock().lock();
1234 +        assertTrue(lock.hasWaiters(c));
1235 +        assertEquals(1, lock.getWaitQueueLength(c));
1236 +        c.signal();
1237 +        lock.writeLock().unlock();
1238 +        Thread.sleep(SHORT_DELAY_MS);
1239 +        lock.writeLock().lock();
1240 +        assertFalse(lock.hasWaiters(c));
1241 +        assertEquals(0, lock.getWaitQueueLength(c));
1242 +        lock.writeLock().unlock();
1243 +        t.join(SHORT_DELAY_MS);
1244 +        assertFalse(t.isAlive());
1245 +    }
1246 +
1247 +    /**
1248 +     * getWaitQueueLength returns number of waiting threads
1249 +     */
1250 +    public void testGetWaitQueueLength() throws InterruptedException {
1251 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1252 +        final Condition c = lock.writeLock().newCondition();
1253 +        Thread t = new Thread(new CheckedRunnable() {
1254 +            public void realRun() throws InterruptedException {
1255 +                lock.writeLock().lock();
1256 +                threadAssertFalse(lock.hasWaiters(c));
1257 +                threadAssertEquals(0, lock.getWaitQueueLength(c));
1258 +                c.await();
1259 +                lock.writeLock().unlock();
1260 +            }});
1261 +
1262 +        t.start();
1263 +        Thread.sleep(SHORT_DELAY_MS);
1264 +        lock.writeLock().lock();
1265 +        assertTrue(lock.hasWaiters(c));
1266 +        assertEquals(1, lock.getWaitQueueLength(c));
1267 +        c.signal();
1268 +        lock.writeLock().unlock();
1269 +        Thread.sleep(SHORT_DELAY_MS);
1270 +        lock.writeLock().lock();
1271 +        assertFalse(lock.hasWaiters(c));
1272 +        assertEquals(0, lock.getWaitQueueLength(c));
1273 +        lock.writeLock().unlock();
1274 +        t.join(SHORT_DELAY_MS);
1275 +        assertFalse(t.isAlive());
1276 +    }
1277 +
1278 +
1279 +    /**
1280 +     * getWaitingThreads returns only and all waiting threads
1281 +     */
1282 +    public void testGetWaitingThreads() throws InterruptedException {
1283 +        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1284 +        final Condition c = lock.writeLock().newCondition();
1285 +        Thread t1 = new Thread(new CheckedRunnable() {
1286 +            public void realRun() throws InterruptedException {
1287 +                lock.writeLock().lock();
1288 +                threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
1289 +                c.await();
1290 +                lock.writeLock().unlock();
1291 +            }});
1292 +
1293 +        Thread t2 = new Thread(new CheckedRunnable() {
1294 +            public void realRun() throws InterruptedException {
1295 +                lock.writeLock().lock();
1296 +                threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
1297 +                c.await();
1298 +                lock.writeLock().unlock();
1299 +            }});
1300 +
1301 +        lock.writeLock().lock();
1302 +        assertTrue(lock.getWaitingThreads(c).isEmpty());
1303 +        lock.writeLock().unlock();
1304 +        t1.start();
1305 +        Thread.sleep(SHORT_DELAY_MS);
1306 +        t2.start();
1307 +        Thread.sleep(SHORT_DELAY_MS);
1308 +        lock.writeLock().lock();
1309 +        assertTrue(lock.hasWaiters(c));
1310 +        assertTrue(lock.getWaitingThreads(c).contains(t1));
1311 +        assertTrue(lock.getWaitingThreads(c).contains(t2));
1312 +        c.signalAll();
1313 +        lock.writeLock().unlock();
1314 +        Thread.sleep(SHORT_DELAY_MS);
1315 +        lock.writeLock().lock();
1316 +        assertFalse(lock.hasWaiters(c));
1317 +        assertTrue(lock.getWaitingThreads(c).isEmpty());
1318 +        lock.writeLock().unlock();
1319 +        t1.join(SHORT_DELAY_MS);
1320 +        t2.join(SHORT_DELAY_MS);
1321 +        assertFalse(t1.isAlive());
1322 +        assertFalse(t2.isAlive());
1323 +    }
1324 +
1325 +    /**
1326 +     * toString indicates current lock state
1327 +     */
1328 +    public void testToString() {
1329 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1330 +        String us = lock.toString();
1331 +        assertTrue(us.indexOf("Write locks = 0") >= 0);
1332 +        assertTrue(us.indexOf("Read locks = 0") >= 0);
1333 +        lock.writeLock().lock();
1334 +        String ws = lock.toString();
1335 +        assertTrue(ws.indexOf("Write locks = 1") >= 0);
1336 +        assertTrue(ws.indexOf("Read locks = 0") >= 0);
1337 +        lock.writeLock().unlock();
1338 +        lock.readLock().lock();
1339 +        lock.readLock().lock();
1340 +        String rs = lock.toString();
1341 +        assertTrue(rs.indexOf("Write locks = 0") >= 0);
1342 +        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1343 +    }
1344 +
1345 +    /**
1346 +     * readLock.toString indicates current lock state
1347 +     */
1348 +    public void testReadLockToString() {
1349 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1350 +        String us = lock.readLock().toString();
1351 +        assertTrue(us.indexOf("Read locks = 0") >= 0);
1352 +        lock.readLock().lock();
1353 +        lock.readLock().lock();
1354 +        String rs = lock.readLock().toString();
1355 +        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1356 +    }
1357 +
1358 +    /**
1359 +     * writeLock.toString indicates current lock state
1360 +     */
1361 +    public void testWriteLockToString() {
1362 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1363 +        String us = lock.writeLock().toString();
1364 +        assertTrue(us.indexOf("Unlocked") >= 0);
1365 +        lock.writeLock().lock();
1366 +        String ls = lock.writeLock().toString();
1367 +        assertTrue(ls.indexOf("Locked") >= 0);
1368 +    }
1369  
1370   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines