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

Comparing jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java (file contents):
Revision 1.3 by dl, Mon Dec 29 14:19:27 2003 UTC vs.
Revision 1.30 by jsr166, Sat Nov 21 02:33:20 2009 UTC

# Line 2 | Line 2
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4   * http://creativecommons.org/licenses/publicdomain
5 < * Other contributors include Andrew Wright, Jeffrey Hayes,
6 < * Pat Fisher, Mike Judd.
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9  
10   import junit.framework.*;
11   import java.util.*;
12   import java.util.concurrent.*;
13 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
14   import java.util.concurrent.locks.*;
15   import java.io.*;
16  
# Line 22 | Line 23 | public class AbstractQueuedSynchronizerT
23      }
24  
25      /**
26 <     * A simple mutex class, from the AbstractQueuedSynchronizer
27 <     * javadoc.  All tests exercise this as a sample user extension.
28 <     * Other methods/features of AbstractQueuedSynchronizerTest are
29 <     * tested implicitly in ReentrantLock, ReentrantReadWriteLock, and
30 <     * Semaphore test classes
31 <     */
32 <    static class Mutex implements Lock, java.io.Serializable {
33 <        private static class Sync extends AbstractQueuedSynchronizer {
34 <            public int acquireExclusiveState(boolean isQueued, int acquires) {
35 <                assert acquires == 1; // Does not use multiple acquires
36 <                return state().compareAndSet(0, 1)? 0 : -1;
37 <            }
38 <            
38 <            public boolean releaseExclusiveState(int releases) {
39 <                state().set(0);
40 <                return true;
41 <            }
42 <            
43 <            public int acquireSharedState(boolean isQueued, int acquires) {
44 <                throw new UnsupportedOperationException();
45 <            }
46 <            
47 <            public boolean releaseSharedState(int releases) {
48 <                throw new UnsupportedOperationException();
49 <            }
50 <            
51 <            public void checkConditionAccess(Thread thread, boolean waiting) {
52 <                if (state().get() == 0) throw new IllegalMonitorStateException();
53 <            }
54 <            
55 <            Condition newCondition() { return new ConditionObject(); }
56 <            
57 <            private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
58 <                s.defaultReadObject();
59 <                state().set(0); // reset to unlocked state
60 <            }
61 <        }
62 <        
63 <        private final Sync sync = new Sync();
64 <        public boolean tryLock() {
65 <            return sync.acquireExclusiveState(false, 1) >= 0;
26 >     * A simple mutex class, adapted from the
27 >     * AbstractQueuedSynchronizer javadoc.  Exclusive acquire tests
28 >     * exercise this as a sample user extension.  Other
29 >     * methods/features of AbstractQueuedSynchronizerTest are tested
30 >     * via other test classes, including those for ReentrantLock,
31 >     * ReentrantReadWriteLock, and Semaphore
32 >     */
33 >    static class Mutex extends AbstractQueuedSynchronizer {
34 >        public boolean isHeldExclusively() { return getState() == 1; }
35 >
36 >        public boolean tryAcquire(int acquires) {
37 >            assertTrue(acquires == 1);
38 >            return compareAndSetState(0, 1);
39          }
40 <        public void lock() {
41 <            sync.acquireExclusiveUninterruptibly(1);
40 >
41 >        public boolean tryRelease(int releases) {
42 >            if (getState() == 0) throw new IllegalMonitorStateException();
43 >            setState(0);
44 >            return true;
45          }
46 <        public void lockInterruptibly() throws InterruptedException {
47 <            sync.acquireExclusiveInterruptibly(1);
46 >
47 >        public AbstractQueuedSynchronizer.ConditionObject newCondition() { return new AbstractQueuedSynchronizer.ConditionObject(); }
48 >
49 >    }
50 >
51 >
52 >    /**
53 >     * A simple latch class, to test shared mode.
54 >     */
55 >    static class BooleanLatch extends AbstractQueuedSynchronizer {
56 >        public boolean isSignalled() { return getState() != 0; }
57 >
58 >        public int tryAcquireShared(int ignore) {
59 >            return isSignalled()? 1 : -1;
60          }
61 <        public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException {
62 <            return sync.acquireExclusiveTimed(1, unit.toNanos(timeout));
61 >
62 >        public boolean tryReleaseShared(int ignore) {
63 >            setState(1);
64 >            return true;
65          }
76        public void unlock() { sync.releaseExclusive(1); }
77        public Condition newCondition() { return sync.newCondition(); }
78        public boolean isLocked() { return sync.state().get() != 0; }
79        public boolean hasQueuedThreads() { return sync.hasQueuedThreads(); }
66      }
67  
68      /**
69 <     * A runnable calling lockInterruptibly
70 <     */
71 <    class InterruptibleLockRunnable implements Runnable {
72 <        final Mutex lock;
73 <        InterruptibleLockRunnable(Mutex l) { lock = l; }
74 <        public void run() {
75 <            try {
76 <                lock.lockInterruptibly();
91 <            } catch(InterruptedException success){}
69 >     * A runnable calling acquireInterruptibly that does not expect to
70 >     * be interrupted.
71 >     */
72 >    class InterruptibleSyncRunnable extends CheckedRunnable {
73 >        final Mutex sync;
74 >        InterruptibleSyncRunnable(Mutex l) { sync = l; }
75 >        public void realRun() throws InterruptedException {
76 >            sync.acquireInterruptibly(1);
77          }
78      }
79  
80  
81      /**
82 <     * A runnable calling lockInterruptibly that expects to be
83 <     * interrupted
82 >     * A runnable calling acquireInterruptibly that expects to be
83 >     * interrupted.
84       */
85 <    class InterruptedLockRunnable implements Runnable {
86 <        final Mutex lock;
87 <        InterruptedLockRunnable(Mutex l) { lock = l; }
88 <        public void run() {
89 <            try {
105 <                lock.lockInterruptibly();
106 <                threadShouldThrow();
107 <            } catch(InterruptedException success){}
85 >    class InterruptedSyncRunnable extends CheckedInterruptedRunnable {
86 >        final Mutex sync;
87 >        InterruptedSyncRunnable(Mutex l) { sync = l; }
88 >        public void realRun() throws InterruptedException {
89 >            sync.acquireInterruptibly(1);
90          }
91      }
92 <    
92 >
93      /**
94 <     * locking an unlocked lock succeeds
94 >     * isHeldExclusively is false upon construction
95       */
96 <    public void testLock() {
97 <        Mutex rl = new Mutex();
98 <        rl.lock();
117 <        assertTrue(rl.isLocked());
118 <        rl.unlock();
96 >    public void testIsHeldExclusively() {
97 >        Mutex rl = new Mutex();
98 >        assertFalse(rl.isHeldExclusively());
99      }
100  
101      /**
102 <     * trylock on an unlocked lock succeeds
102 >     * acquiring released sync succeeds
103       */
104 <    public void testTryLock() {
105 <        Mutex rl = new Mutex();
106 <        assertTrue(rl.tryLock());
107 <        assertTrue(rl.isLocked());
108 <        rl.unlock();
104 >    public void testAcquire() {
105 >        Mutex rl = new Mutex();
106 >        rl.acquire(1);
107 >        assertTrue(rl.isHeldExclusively());
108 >        rl.release(1);
109 >        assertFalse(rl.isHeldExclusively());
110 >    }
111 >
112 >    /**
113 >     * tryAcquire on an released sync succeeds
114 >     */
115 >    public void testTryAcquire() {
116 >        Mutex rl = new Mutex();
117 >        assertTrue(rl.tryAcquire(1));
118 >        assertTrue(rl.isHeldExclusively());
119 >        rl.release(1);
120      }
121  
122      /**
123       * hasQueuedThreads reports whether there are waiting threads
124       */
125 <    public void testhasQueuedThreads() {
126 <        final Mutex lock = new Mutex();
127 <        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
128 <        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
129 <        try {
130 <            assertFalse(lock.hasQueuedThreads());
131 <            lock.lock();
132 <            t1.start();
133 <            Thread.sleep(SHORT_DELAY_MS);
134 <            assertTrue(lock.hasQueuedThreads());
135 <            t2.start();
136 <            Thread.sleep(SHORT_DELAY_MS);
137 <            assertTrue(lock.hasQueuedThreads());
138 <            t1.interrupt();
139 <            Thread.sleep(SHORT_DELAY_MS);
140 <            assertTrue(lock.hasQueuedThreads());
141 <            lock.unlock();
142 <            Thread.sleep(SHORT_DELAY_MS);
143 <            assertFalse(lock.hasQueuedThreads());
144 <            t1.join();
145 <            t2.join();
155 <        } catch(Exception e){
156 <            unexpectedException();
157 <        }
158 <    }
125 >    public void testhasQueuedThreads() throws InterruptedException {
126 >        final Mutex sync = new Mutex();
127 >        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
128 >        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
129 >        assertFalse(sync.hasQueuedThreads());
130 >        sync.acquire(1);
131 >        t1.start();
132 >        Thread.sleep(SHORT_DELAY_MS);
133 >        assertTrue(sync.hasQueuedThreads());
134 >        t2.start();
135 >        Thread.sleep(SHORT_DELAY_MS);
136 >        assertTrue(sync.hasQueuedThreads());
137 >        t1.interrupt();
138 >        Thread.sleep(SHORT_DELAY_MS);
139 >        assertTrue(sync.hasQueuedThreads());
140 >        sync.release(1);
141 >        Thread.sleep(SHORT_DELAY_MS);
142 >        assertFalse(sync.hasQueuedThreads());
143 >        t1.join();
144 >        t2.join();
145 >    }
146  
147      /**
148 <     * timed trylock is interruptible.
148 >     * isQueued(null) throws NPE
149       */
150 <    public void testInterruptedException2() {
151 <        final Mutex lock = new Mutex();
152 <        lock.lock();
153 <        Thread t = new Thread(new Runnable() {
154 <                public void run() {
155 <                    try {
169 <                        lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS);
170 <                        threadShouldThrow();
171 <                    } catch(InterruptedException success){}
172 <                }
173 <            });
174 <        try {
175 <            t.start();
176 <            t.interrupt();
177 <        } catch(Exception e){
178 <            unexpectedException();
179 <        }
150 >    public void testIsQueuedNPE() {
151 >        final Mutex sync = new Mutex();
152 >        try {
153 >            sync.isQueued(null);
154 >            shouldThrow();
155 >        } catch (NullPointerException success) {}
156      }
157  
158 +    /**
159 +     * isQueued reports whether a thread is queued.
160 +     */
161 +    public void testIsQueued() throws InterruptedException {
162 +        final Mutex sync = new Mutex();
163 +        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
164 +        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
165 +        assertFalse(sync.isQueued(t1));
166 +        assertFalse(sync.isQueued(t2));
167 +        sync.acquire(1);
168 +        t1.start();
169 +        Thread.sleep(SHORT_DELAY_MS);
170 +        assertTrue(sync.isQueued(t1));
171 +        t2.start();
172 +        Thread.sleep(SHORT_DELAY_MS);
173 +        assertTrue(sync.isQueued(t1));
174 +        assertTrue(sync.isQueued(t2));
175 +        t1.interrupt();
176 +        Thread.sleep(SHORT_DELAY_MS);
177 +        assertFalse(sync.isQueued(t1));
178 +        assertTrue(sync.isQueued(t2));
179 +        sync.release(1);
180 +        Thread.sleep(SHORT_DELAY_MS);
181 +        assertFalse(sync.isQueued(t1));
182 +        Thread.sleep(SHORT_DELAY_MS);
183 +        assertFalse(sync.isQueued(t2));
184 +        t1.join();
185 +        t2.join();
186 +    }
187 +
188 +    /**
189 +     * getFirstQueuedThread returns first waiting thread or null if none
190 +     */
191 +    public void testGetFirstQueuedThread() throws InterruptedException {
192 +        final Mutex sync = new Mutex();
193 +        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
194 +        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
195 +        assertNull(sync.getFirstQueuedThread());
196 +        sync.acquire(1);
197 +        t1.start();
198 +        Thread.sleep(SHORT_DELAY_MS);
199 +        assertEquals(t1, sync.getFirstQueuedThread());
200 +        t2.start();
201 +        Thread.sleep(SHORT_DELAY_MS);
202 +        assertEquals(t1, sync.getFirstQueuedThread());
203 +        t1.interrupt();
204 +        Thread.sleep(SHORT_DELAY_MS);
205 +        Thread.sleep(SHORT_DELAY_MS);
206 +        assertEquals(t2, sync.getFirstQueuedThread());
207 +        sync.release(1);
208 +        Thread.sleep(SHORT_DELAY_MS);
209 +        assertNull(sync.getFirstQueuedThread());
210 +        t1.join();
211 +        t2.join();
212 +    }
213 +
214 +
215 +    /**
216 +     * hasContended reports false if no thread has ever blocked, else true
217 +     */
218 +    public void testHasContended() throws InterruptedException {
219 +        final Mutex sync = new Mutex();
220 +        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
221 +        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
222 +        assertFalse(sync.hasContended());
223 +        sync.acquire(1);
224 +        t1.start();
225 +        Thread.sleep(SHORT_DELAY_MS);
226 +        assertTrue(sync.hasContended());
227 +        t2.start();
228 +        Thread.sleep(SHORT_DELAY_MS);
229 +        assertTrue(sync.hasContended());
230 +        t1.interrupt();
231 +        Thread.sleep(SHORT_DELAY_MS);
232 +        assertTrue(sync.hasContended());
233 +        sync.release(1);
234 +        Thread.sleep(SHORT_DELAY_MS);
235 +        assertTrue(sync.hasContended());
236 +        t1.join();
237 +        t2.join();
238 +    }
239 +
240 +    /**
241 +     * getQueuedThreads includes waiting threads
242 +     */
243 +    public void testGetQueuedThreads() throws InterruptedException {
244 +        final Mutex sync = new Mutex();
245 +        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
246 +        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
247 +        assertTrue(sync.getQueuedThreads().isEmpty());
248 +        sync.acquire(1);
249 +        assertTrue(sync.getQueuedThreads().isEmpty());
250 +        t1.start();
251 +        Thread.sleep(SHORT_DELAY_MS);
252 +        assertTrue(sync.getQueuedThreads().contains(t1));
253 +        t2.start();
254 +        Thread.sleep(SHORT_DELAY_MS);
255 +        assertTrue(sync.getQueuedThreads().contains(t1));
256 +        assertTrue(sync.getQueuedThreads().contains(t2));
257 +        t1.interrupt();
258 +        Thread.sleep(SHORT_DELAY_MS);
259 +        assertFalse(sync.getQueuedThreads().contains(t1));
260 +        assertTrue(sync.getQueuedThreads().contains(t2));
261 +        sync.release(1);
262 +        Thread.sleep(SHORT_DELAY_MS);
263 +        assertTrue(sync.getQueuedThreads().isEmpty());
264 +        t1.join();
265 +        t2.join();
266 +    }
267 +
268 +    /**
269 +     * getExclusiveQueuedThreads includes waiting threads
270 +     */
271 +    public void testGetExclusiveQueuedThreads() throws InterruptedException {
272 +        final Mutex sync = new Mutex();
273 +        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
274 +        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
275 +        assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
276 +        sync.acquire(1);
277 +        assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
278 +        t1.start();
279 +        Thread.sleep(SHORT_DELAY_MS);
280 +        assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
281 +        t2.start();
282 +        Thread.sleep(SHORT_DELAY_MS);
283 +        assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
284 +        assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
285 +        t1.interrupt();
286 +        Thread.sleep(SHORT_DELAY_MS);
287 +        assertFalse(sync.getExclusiveQueuedThreads().contains(t1));
288 +        assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
289 +        sync.release(1);
290 +        Thread.sleep(SHORT_DELAY_MS);
291 +        assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
292 +        t1.join();
293 +        t2.join();
294 +    }
295 +
296 +    /**
297 +     * getSharedQueuedThreads does not include exclusively waiting threads
298 +     */
299 +    public void testGetSharedQueuedThreads() throws InterruptedException {
300 +        final Mutex sync = new Mutex();
301 +        Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
302 +        Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
303 +        assertTrue(sync.getSharedQueuedThreads().isEmpty());
304 +        sync.acquire(1);
305 +        assertTrue(sync.getSharedQueuedThreads().isEmpty());
306 +        t1.start();
307 +        Thread.sleep(SHORT_DELAY_MS);
308 +        assertTrue(sync.getSharedQueuedThreads().isEmpty());
309 +        t2.start();
310 +        Thread.sleep(SHORT_DELAY_MS);
311 +        assertTrue(sync.getSharedQueuedThreads().isEmpty());
312 +        t1.interrupt();
313 +        Thread.sleep(SHORT_DELAY_MS);
314 +        assertTrue(sync.getSharedQueuedThreads().isEmpty());
315 +        sync.release(1);
316 +        Thread.sleep(SHORT_DELAY_MS);
317 +        assertTrue(sync.getSharedQueuedThreads().isEmpty());
318 +        t1.join();
319 +        t2.join();
320 +    }
321 +
322 +    /**
323 +     * tryAcquireNanos is interruptible.
324 +     */
325 +    public void testInterruptedException2() throws InterruptedException {
326 +        final Mutex sync = new Mutex();
327 +        sync.acquire(1);
328 +        Thread t = new Thread(new CheckedInterruptedRunnable() {
329 +            public void realRun() throws InterruptedException {
330 +                sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000);
331 +            }});
332 +
333 +        t.start();
334 +        t.interrupt();
335 +        t.join();
336 +    }
337 +
338 +
339 +    /**
340 +     * TryAcquire on exclusively held sync fails
341 +     */
342 +    public void testTryAcquireWhenSynced() throws InterruptedException {
343 +        final Mutex sync = new Mutex();
344 +        sync.acquire(1);
345 +        Thread t = new Thread(new CheckedRunnable() {
346 +            public void realRun() {
347 +                threadAssertFalse(sync.tryAcquire(1));
348 +            }});
349 +
350 +        t.start();
351 +        t.join();
352 +        sync.release(1);
353 +    }
354 +
355 +    /**
356 +     * tryAcquireNanos on an exclusively held sync times out
357 +     */
358 +    public void testAcquireNanos_Timeout() throws InterruptedException {
359 +        final Mutex sync = new Mutex();
360 +        sync.acquire(1);
361 +        Thread t = new Thread(new CheckedRunnable() {
362 +            public void realRun() throws InterruptedException {
363 +                threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000));
364 +            }});
365 +
366 +        t.start();
367 +        t.join();
368 +        sync.release(1);
369 +    }
370 +
371 +
372 +    /**
373 +     * getState is true when acquired and false when not
374 +     */
375 +    public void testGetState() throws InterruptedException {
376 +        final Mutex sync = new Mutex();
377 +        sync.acquire(1);
378 +        assertTrue(sync.isHeldExclusively());
379 +        sync.release(1);
380 +        assertFalse(sync.isHeldExclusively());
381 +        Thread t = new Thread(new CheckedRunnable() {
382 +            public void realRun() throws InterruptedException {
383 +                sync.acquire(1);
384 +                Thread.sleep(SMALL_DELAY_MS);
385 +                sync.release(1);
386 +            }});
387 +
388 +        t.start();
389 +        Thread.sleep(SHORT_DELAY_MS);
390 +        assertTrue(sync.isHeldExclusively());
391 +        t.join();
392 +        assertFalse(sync.isHeldExclusively());
393 +    }
394 +
395 +
396 +    /**
397 +     * acquireInterruptibly is interruptible.
398 +     */
399 +    public void testAcquireInterruptibly1() throws InterruptedException {
400 +        final Mutex sync = new Mutex();
401 +        sync.acquire(1);
402 +        Thread t = new Thread(new InterruptedSyncRunnable(sync));
403 +
404 +        t.start();
405 +        Thread.sleep(SHORT_DELAY_MS);
406 +        t.interrupt();
407 +        Thread.sleep(SHORT_DELAY_MS);
408 +        sync.release(1);
409 +        t.join();
410 +    }
411 +
412 +    /**
413 +     * acquireInterruptibly succeeds when released, else is interruptible
414 +     */
415 +    public void testAcquireInterruptibly2() throws InterruptedException {
416 +        final Mutex sync = new Mutex();
417 +        sync.acquireInterruptibly(1);
418 +        Thread t = new Thread(new InterruptedSyncRunnable(sync));
419 +        t.start();
420 +        t.interrupt();
421 +        assertTrue(sync.isHeldExclusively());
422 +        t.join();
423 +    }
424 +
425 +    /**
426 +     * owns is true for a condition created by sync else false
427 +     */
428 +    public void testOwns() {
429 +        final Mutex sync = new Mutex();
430 +        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
431 +        final Mutex sync2 = new Mutex();
432 +        assertTrue(sync.owns(c));
433 +        assertFalse(sync2.owns(c));
434 +    }
435 +
436 +    /**
437 +     * Calling await without holding sync throws IllegalMonitorStateException
438 +     */
439 +    public void testAwait_IllegalMonitor() throws InterruptedException {
440 +        final Mutex sync = new Mutex();
441 +        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
442 +        try {
443 +            c.await();
444 +            shouldThrow();
445 +        } catch (IllegalMonitorStateException success) {}
446 +    }
447  
448      /**
449 <     * Trylock on a locked lock fails
449 >     * Calling signal without holding sync throws IllegalMonitorStateException
450       */
451 <    public void testTryLockWhenLocked() {
452 <        final Mutex lock = new Mutex();
453 <        lock.lock();
454 <        Thread t = new Thread(new Runnable() {
455 <                public void run() {
456 <                    threadAssertFalse(lock.tryLock());
457 <                }
458 <            });
194 <        try {
195 <            t.start();
196 <            t.join();
197 <            lock.unlock();
198 <        } catch(Exception e){
199 <            unexpectedException();
200 <        }
201 <    }
451 >    public void testSignal_IllegalMonitor() {
452 >        final Mutex sync = new Mutex();
453 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
454 >        try {
455 >            c.signal();
456 >            shouldThrow();
457 >        } catch (IllegalMonitorStateException success) {}
458 >    }
459  
460      /**
461 <     * Timed trylock on a locked lock times out
461 >     * awaitNanos without a signal times out
462       */
463 <    public void testTryLock_Timeout() {
464 <        final Mutex lock = new Mutex();
465 <        lock.lock();
466 <        Thread t = new Thread(new Runnable() {
467 <                public void run() {
468 <                    try {
469 <                        threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS));
213 <                    } catch (Exception ex) {
214 <                        threadUnexpectedException();
215 <                    }
216 <                }
217 <            });
218 <        try {
219 <            t.start();
220 <            t.join();
221 <            lock.unlock();
222 <        } catch(Exception e){
223 <            unexpectedException();
224 <        }
225 <    }
226 <    
227 <  
228 <    /**
229 <     * isLocked is true when locked and false when not
230 <     */
231 <    public void testIsLocked() {
232 <        final Mutex lock = new Mutex();
233 <        lock.lock();
234 <        assertTrue(lock.isLocked());
235 <        lock.unlock();
236 <        assertFalse(lock.isLocked());
237 <        Thread t = new Thread(new Runnable() {
238 <                public void run() {
239 <                    lock.lock();
240 <                    try {
241 <                        Thread.sleep(SMALL_DELAY_MS);
242 <                    }
243 <                    catch(Exception e) {
244 <                        threadUnexpectedException();
245 <                    }
246 <                    lock.unlock();
247 <                }
248 <            });
249 <        try {
250 <            t.start();
251 <            Thread.sleep(SHORT_DELAY_MS);
252 <            assertTrue(lock.isLocked());
253 <            t.join();
254 <            assertFalse(lock.isLocked());
255 <        } catch(Exception e){
256 <            unexpectedException();
257 <        }
463 >    public void testAwaitNanos_Timeout() throws InterruptedException {
464 >        final Mutex sync = new Mutex();
465 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
466 >        sync.acquire(1);
467 >        long t = c.awaitNanos(100);
468 >        assertTrue(t <= 0);
469 >        sync.release(1);
470      }
471  
472 +    /**
473 +     *  Timed await without a signal times out
474 +     */
475 +    public void testAwait_Timeout() throws InterruptedException {
476 +        final Mutex sync = new Mutex();
477 +        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
478 +        sync.acquire(1);
479 +        assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS));
480 +        sync.release(1);
481 +    }
482  
483      /**
484 <     * lockInterruptibly is interruptible.
484 >     * awaitUntil without a signal times out
485       */
486 <    public void testLockInterruptibly1() {
487 <        final Mutex lock = new Mutex();
488 <        lock.lock();
489 <        Thread t = new Thread(new InterruptedLockRunnable(lock));
490 <        try {
491 <            t.start();
492 <            t.interrupt();
493 <            lock.unlock();
272 <            t.join();
273 <        } catch(Exception e){
274 <            unexpectedException();
275 <        }
276 <    }
486 >    public void testAwaitUntil_Timeout() throws InterruptedException {
487 >        final Mutex sync = new Mutex();
488 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
489 >        sync.acquire(1);
490 >        java.util.Date d = new java.util.Date();
491 >        assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
492 >        sync.release(1);
493 >    }
494  
495      /**
496 <     * lockInterruptibly succeeds when unlocked, else is interruptible
496 >     * await returns when signalled
497       */
498 <    public void testLockInterruptibly2() {
499 <        final Mutex lock = new Mutex();
500 <        try {
501 <            lock.lockInterruptibly();
502 <        } catch(Exception e) {
503 <            unexpectedException();
504 <        }
505 <        Thread t = new Thread(new InterruptedLockRunnable(lock));
498 >    public void testAwait() throws InterruptedException {
499 >        final Mutex sync = new Mutex();
500 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
501 >        Thread t = new Thread(new CheckedRunnable() {
502 >            public void realRun() throws InterruptedException {
503 >                sync.acquire(1);
504 >                c.await();
505 >                sync.release(1);
506 >            }});
507 >
508 >        t.start();
509 >        Thread.sleep(SHORT_DELAY_MS);
510 >        sync.acquire(1);
511 >        c.signal();
512 >        sync.release(1);
513 >        t.join(SHORT_DELAY_MS);
514 >        assertFalse(t.isAlive());
515 >    }
516 >
517 >
518 >
519 >    /**
520 >     * hasWaiters throws NPE if null
521 >     */
522 >    public void testHasWaitersNPE() {
523 >        final Mutex sync = new Mutex();
524          try {
525 <            t.start();
526 <            t.interrupt();
527 <            assertTrue(lock.isLocked());
293 <            t.join();
294 <        } catch(Exception e){
295 <            unexpectedException();
296 <        }
525 >            sync.hasWaiters(null);
526 >            shouldThrow();
527 >        } catch (NullPointerException success) {}
528      }
529  
530      /**
531 <     * Calling await without holding lock throws IllegalMonitorStateException
531 >     * getWaitQueueLength throws NPE if null
532       */
533 <    public void testAwait_IllegalMonitor() {
534 <        final Mutex lock = new Mutex();
304 <        final Condition c = lock.newCondition();
533 >    public void testGetWaitQueueLengthNPE() {
534 >        final Mutex sync = new Mutex();
535          try {
536 <            c.await();
536 >            sync.getWaitQueueLength(null);
537              shouldThrow();
538 <        }
309 <        catch (IllegalMonitorStateException success) {
310 <        }
311 <        catch (Exception ex) {
312 <            unexpectedException();
313 <        }
538 >        } catch (NullPointerException success) {}
539      }
540  
541 +
542      /**
543 <     * Calling signal without holding lock throws IllegalMonitorStateException
543 >     * getWaitingThreads throws NPE if null
544       */
545 <    public void testSignal_IllegalMonitor() {
546 <        final Mutex lock = new Mutex();
321 <        final Condition c = lock.newCondition();
545 >    public void testGetWaitingThreadsNPE() {
546 >        final Mutex sync = new Mutex();
547          try {
548 <            c.signal();
548 >            sync.getWaitingThreads(null);
549              shouldThrow();
550 <        }
326 <        catch (IllegalMonitorStateException success) {
327 <        }
328 <        catch (Exception ex) {
329 <            unexpectedException();
330 <        }
550 >        } catch (NullPointerException success) {}
551      }
552  
553 +
554      /**
555 <     * awaitNanos without a signal times out
555 >     * hasWaiters throws IAE if not owned
556       */
557 <    public void testAwaitNanos_Timeout() {
558 <        final Mutex lock = new Mutex();
559 <        final Condition c = lock.newCondition();
560 <        try {
561 <            lock.lock();
562 <            long t = c.awaitNanos(100);
563 <            assertTrue(t <= 0);
564 <            lock.unlock();
344 <        }
345 <        catch (Exception ex) {
346 <            unexpectedException();
347 <        }
557 >    public void testHasWaitersIAE() {
558 >        final Mutex sync = new Mutex();
559 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
560 >        final Mutex sync2 = new Mutex();
561 >        try {
562 >            sync2.hasWaiters(c);
563 >            shouldThrow();
564 >        } catch (IllegalArgumentException success) {}
565      }
566  
567      /**
568 <     *  timed await without a signal times out
568 >     * hasWaiters throws IMSE if not synced
569       */
570 <    public void testAwait_Timeout() {
571 <        final Mutex lock = new Mutex();
572 <        final Condition c = lock.newCondition();
573 <        try {
574 <            lock.lock();
575 <            assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
576 <            lock.unlock();
360 <        }
361 <        catch (Exception ex) {
362 <            unexpectedException();
363 <        }
570 >    public void testHasWaitersIMSE() {
571 >        final Mutex sync = new Mutex();
572 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
573 >        try {
574 >            sync.hasWaiters(c);
575 >            shouldThrow();
576 >        } catch (IllegalMonitorStateException success) {}
577      }
578  
579 +
580      /**
581 <     * awaitUntil without a signal times out
581 >     * getWaitQueueLength throws IAE if not owned
582       */
583 <    public void testAwaitUntil_Timeout() {
584 <        final Mutex lock = new Mutex();
585 <        final Condition c = lock.newCondition();
586 <        try {
587 <            lock.lock();
588 <            java.util.Date d = new java.util.Date();
589 <            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
590 <            lock.unlock();
377 <        }
378 <        catch (Exception ex) {
379 <            unexpectedException();
380 <        }
583 >    public void testGetWaitQueueLengthIAE() {
584 >        final Mutex sync = new Mutex();
585 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
586 >        final Mutex sync2 = new Mutex();
587 >        try {
588 >            sync2.getWaitQueueLength(c);
589 >            shouldThrow();
590 >        } catch (IllegalArgumentException success) {}
591      }
592  
593      /**
594 <     * await returns when signalled
594 >     * getWaitQueueLength throws IMSE if not synced
595       */
596 <    public void testAwait() {
597 <        final Mutex lock = new Mutex();
598 <        final Condition c = lock.newCondition();
599 <        Thread t = new Thread(new Runnable() {
600 <                public void run() {
601 <                    try {
602 <                        lock.lock();
603 <                        c.await();
604 <                        lock.unlock();
605 <                    }
606 <                    catch(InterruptedException e) {
607 <                        threadUnexpectedException();
608 <                    }
609 <                }
610 <            });
611 <
612 <        try {
613 <            t.start();
614 <            Thread.sleep(SHORT_DELAY_MS);
615 <            lock.lock();
616 <            c.signal();
407 <            lock.unlock();
408 <            t.join(SHORT_DELAY_MS);
409 <            assertFalse(t.isAlive());
410 <        }
411 <        catch (Exception ex) {
412 <            unexpectedException();
413 <        }
596 >    public void testGetWaitQueueLengthIMSE() {
597 >        final Mutex sync = new Mutex();
598 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
599 >        try {
600 >            sync.getWaitQueueLength(c);
601 >            shouldThrow();
602 >        } catch (IllegalMonitorStateException success) {}
603 >    }
604 >
605 >
606 >    /**
607 >     * getWaitingThreads throws IAE if not owned
608 >     */
609 >    public void testGetWaitingThreadsIAE() {
610 >        final Mutex sync = new Mutex();
611 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
612 >        final Mutex sync2 = new Mutex();
613 >        try {
614 >            sync2.getWaitingThreads(c);
615 >            shouldThrow();
616 >        } catch (IllegalArgumentException success) {}
617      }
618 <    
618 >
619 >    /**
620 >     * getWaitingThreads throws IMSE if not synced
621 >     */
622 >    public void testGetWaitingThreadsIMSE() {
623 >        final Mutex sync = new Mutex();
624 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
625 >        try {
626 >            sync.getWaitingThreads(c);
627 >            shouldThrow();
628 >        } catch (IllegalMonitorStateException success) {}
629 >    }
630 >
631 >
632 >
633 >    /**
634 >     * hasWaiters returns true when a thread is waiting, else false
635 >     */
636 >    public void testHasWaiters() throws InterruptedException {
637 >        final Mutex sync = new Mutex();
638 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
639 >        Thread t = new Thread(new CheckedRunnable() {
640 >            public void realRun() throws InterruptedException {
641 >                sync.acquire(1);
642 >                threadAssertFalse(sync.hasWaiters(c));
643 >                threadAssertEquals(0, sync.getWaitQueueLength(c));
644 >                c.await();
645 >                sync.release(1);
646 >            }});
647 >
648 >        t.start();
649 >        Thread.sleep(SHORT_DELAY_MS);
650 >        sync.acquire(1);
651 >        assertTrue(sync.hasWaiters(c));
652 >        assertEquals(1, sync.getWaitQueueLength(c));
653 >        c.signal();
654 >        sync.release(1);
655 >        Thread.sleep(SHORT_DELAY_MS);
656 >        sync.acquire(1);
657 >        assertFalse(sync.hasWaiters(c));
658 >        assertEquals(0, sync.getWaitQueueLength(c));
659 >        sync.release(1);
660 >        t.join(SHORT_DELAY_MS);
661 >        assertFalse(t.isAlive());
662 >    }
663 >
664 >    /**
665 >     * getWaitQueueLength returns number of waiting threads
666 >     */
667 >    public void testGetWaitQueueLength() throws InterruptedException {
668 >        final Mutex sync = new Mutex();
669 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
670 >        Thread t1 = new Thread(new CheckedRunnable() {
671 >            public void realRun() throws InterruptedException {
672 >                sync.acquire(1);
673 >                threadAssertFalse(sync.hasWaiters(c));
674 >                threadAssertEquals(0, sync.getWaitQueueLength(c));
675 >                c.await();
676 >                sync.release(1);
677 >            }});
678 >
679 >        Thread t2 = new Thread(new CheckedRunnable() {
680 >            public void realRun() throws InterruptedException {
681 >                sync.acquire(1);
682 >                threadAssertTrue(sync.hasWaiters(c));
683 >                threadAssertEquals(1, sync.getWaitQueueLength(c));
684 >                c.await();
685 >                sync.release(1);
686 >            }});
687 >
688 >        t1.start();
689 >        Thread.sleep(SHORT_DELAY_MS);
690 >        t2.start();
691 >        Thread.sleep(SHORT_DELAY_MS);
692 >        sync.acquire(1);
693 >        assertTrue(sync.hasWaiters(c));
694 >        assertEquals(2, sync.getWaitQueueLength(c));
695 >        c.signalAll();
696 >        sync.release(1);
697 >        Thread.sleep(SHORT_DELAY_MS);
698 >        sync.acquire(1);
699 >        assertFalse(sync.hasWaiters(c));
700 >        assertEquals(0, sync.getWaitQueueLength(c));
701 >        sync.release(1);
702 >        t1.join(SHORT_DELAY_MS);
703 >        t2.join(SHORT_DELAY_MS);
704 >        assertFalse(t1.isAlive());
705 >        assertFalse(t2.isAlive());
706 >    }
707 >
708 >    /**
709 >     * getWaitingThreads returns only and all waiting threads
710 >     */
711 >    public void testGetWaitingThreads() throws InterruptedException {
712 >        final Mutex sync = new Mutex();
713 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
714 >        Thread t1 = new Thread(new CheckedRunnable() {
715 >            public void realRun() throws InterruptedException {
716 >                sync.acquire(1);
717 >                threadAssertTrue(sync.getWaitingThreads(c).isEmpty());
718 >                c.await();
719 >                sync.release(1);
720 >            }});
721 >
722 >        Thread t2 = new Thread(new CheckedRunnable() {
723 >            public void realRun() throws InterruptedException {
724 >                sync.acquire(1);
725 >                threadAssertFalse(sync.getWaitingThreads(c).isEmpty());
726 >                c.await();
727 >                sync.release(1);
728 >            }});
729 >
730 >        sync.acquire(1);
731 >        assertTrue(sync.getWaitingThreads(c).isEmpty());
732 >        sync.release(1);
733 >        t1.start();
734 >        Thread.sleep(SHORT_DELAY_MS);
735 >        t2.start();
736 >        Thread.sleep(SHORT_DELAY_MS);
737 >        sync.acquire(1);
738 >        assertTrue(sync.hasWaiters(c));
739 >        assertTrue(sync.getWaitingThreads(c).contains(t1));
740 >        assertTrue(sync.getWaitingThreads(c).contains(t2));
741 >        c.signalAll();
742 >        sync.release(1);
743 >        Thread.sleep(SHORT_DELAY_MS);
744 >        sync.acquire(1);
745 >        assertFalse(sync.hasWaiters(c));
746 >        assertTrue(sync.getWaitingThreads(c).isEmpty());
747 >        sync.release(1);
748 >        t1.join(SHORT_DELAY_MS);
749 >        t2.join(SHORT_DELAY_MS);
750 >        assertFalse(t1.isAlive());
751 >        assertFalse(t2.isAlive());
752 >    }
753 >
754 >
755 >
756 >    /**
757 >     * awaitUninterruptibly doesn't abort on interrupt
758 >     */
759 >    public void testAwaitUninterruptibly() throws InterruptedException {
760 >        final Mutex sync = new Mutex();
761 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
762 >        Thread t = new Thread(new CheckedRunnable() {
763 >            public void realRun() {
764 >                sync.acquire(1);
765 >                c.awaitUninterruptibly();
766 >                sync.release(1);
767 >            }});
768 >
769 >        t.start();
770 >        Thread.sleep(SHORT_DELAY_MS);
771 >        t.interrupt();
772 >        sync.acquire(1);
773 >        c.signal();
774 >        sync.release(1);
775 >        t.join(SHORT_DELAY_MS);
776 >        assertFalse(t.isAlive());
777 >    }
778 >
779 >    /**
780 >     * await is interruptible
781 >     */
782 >    public void testAwait_Interrupt() throws InterruptedException {
783 >        final Mutex sync = new Mutex();
784 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
785 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
786 >            public void realRun() throws InterruptedException {
787 >                sync.acquire(1);
788 >                c.await();
789 >                sync.release(1);
790 >            }});
791 >
792 >        t.start();
793 >        Thread.sleep(SHORT_DELAY_MS);
794 >        t.interrupt();
795 >        t.join(SHORT_DELAY_MS);
796 >        assertFalse(t.isAlive());
797 >    }
798 >
799 >    /**
800 >     * awaitNanos is interruptible
801 >     */
802 >    public void testAwaitNanos_Interrupt() throws InterruptedException {
803 >        final Mutex sync = new Mutex();
804 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
805 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
806 >            public void realRun() throws InterruptedException {
807 >                sync.acquire(1);
808 >                c.awaitNanos(1000 * 1000 * 1000); // 1 sec
809 >                sync.release(1);
810 >            }});
811 >
812 >        t.start();
813 >        Thread.sleep(SHORT_DELAY_MS);
814 >        t.interrupt();
815 >        t.join(SHORT_DELAY_MS);
816 >        assertFalse(t.isAlive());
817 >    }
818 >
819 >    /**
820 >     * awaitUntil is interruptible
821 >     */
822 >    public void testAwaitUntil_Interrupt() throws InterruptedException {
823 >        final Mutex sync = new Mutex();
824 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
825 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
826 >            public void realRun() throws InterruptedException {
827 >                sync.acquire(1);
828 >                java.util.Date d = new java.util.Date();
829 >                c.awaitUntil(new java.util.Date(d.getTime() + 10000));
830 >                sync.release(1);
831 >            }});
832 >
833 >        t.start();
834 >        Thread.sleep(SHORT_DELAY_MS);
835 >        t.interrupt();
836 >        t.join(SHORT_DELAY_MS);
837 >        assertFalse(t.isAlive());
838 >    }
839 >
840 >    /**
841 >     * signalAll wakes up all threads
842 >     */
843 >    public void testSignalAll() throws InterruptedException {
844 >        final Mutex sync = new Mutex();
845 >        final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();
846 >        Thread t1 = new Thread(new CheckedRunnable() {
847 >            public void realRun() throws InterruptedException {
848 >                sync.acquire(1);
849 >                c.await();
850 >                sync.release(1);
851 >            }});
852 >
853 >        Thread t2 = new Thread(new CheckedRunnable() {
854 >            public void realRun() throws InterruptedException {
855 >                sync.acquire(1);
856 >                c.await();
857 >                sync.release(1);
858 >            }});
859 >
860 >        t1.start();
861 >        t2.start();
862 >        Thread.sleep(SHORT_DELAY_MS);
863 >        sync.acquire(1);
864 >        c.signalAll();
865 >        sync.release(1);
866 >        t1.join(SHORT_DELAY_MS);
867 >        t2.join(SHORT_DELAY_MS);
868 >        assertFalse(t1.isAlive());
869 >        assertFalse(t2.isAlive());
870 >    }
871 >
872 >
873 >    /**
874 >     * toString indicates current state
875 >     */
876 >    public void testToString() {
877 >        Mutex sync = new Mutex();
878 >        String us = sync.toString();
879 >        assertTrue(us.indexOf("State = 0") >= 0);
880 >        sync.acquire(1);
881 >        String ls = sync.toString();
882 >        assertTrue(ls.indexOf("State = 1") >= 0);
883 >    }
884 >
885 >    /**
886 >     * A serialized AQS deserializes with current state
887 >     */
888 >    public void testSerialization() throws Exception {
889 >        Mutex l = new Mutex();
890 >        l.acquire(1);
891 >        assertTrue(l.isHeldExclusively());
892 >
893 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
894 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
895 >        out.writeObject(l);
896 >        out.close();
897 >
898 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
899 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
900 >        Mutex r = (Mutex) in.readObject();
901 >        assertTrue(r.isHeldExclusively());
902 >    }
903 >
904 >
905 >    /**
906 >     * tryReleaseShared setting state changes getState
907 >     */
908 >    public void testGetStateWithReleaseShared() {
909 >        final BooleanLatch l = new BooleanLatch();
910 >        assertFalse(l.isSignalled());
911 >        l.releaseShared(0);
912 >        assertTrue(l.isSignalled());
913 >    }
914 >
915 >    /**
916 >     * releaseShared has no effect when already signalled
917 >     */
918 >    public void testReleaseShared() {
919 >        final BooleanLatch l = new BooleanLatch();
920 >        assertFalse(l.isSignalled());
921 >        l.releaseShared(0);
922 >        assertTrue(l.isSignalled());
923 >        l.releaseShared(0);
924 >        assertTrue(l.isSignalled());
925 >    }
926 >
927 >    /**
928 >     * acquireSharedInterruptibly returns after release, but not before
929 >     */
930 >    public void testAcquireSharedInterruptibly() throws InterruptedException {
931 >        final BooleanLatch l = new BooleanLatch();
932 >
933 >        Thread t = new Thread(new CheckedRunnable() {
934 >            public void realRun() throws InterruptedException {
935 >                threadAssertFalse(l.isSignalled());
936 >                l.acquireSharedInterruptibly(0);
937 >                threadAssertTrue(l.isSignalled());
938 >            }});
939 >
940 >        t.start();
941 >        assertFalse(l.isSignalled());
942 >        Thread.sleep(SHORT_DELAY_MS);
943 >        l.releaseShared(0);
944 >        assertTrue(l.isSignalled());
945 >        t.join();
946 >    }
947 >
948 >
949 >    /**
950 >     * acquireSharedTimed returns after release
951 >     */
952 >    public void testAsquireSharedTimed() throws InterruptedException {
953 >        final BooleanLatch l = new BooleanLatch();
954 >
955 >        Thread t = new Thread(new CheckedRunnable() {
956 >            public void realRun() throws InterruptedException {
957 >                threadAssertFalse(l.isSignalled());
958 >                threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000));
959 >                threadAssertTrue(l.isSignalled());
960 >            }});
961 >
962 >        t.start();
963 >        assertFalse(l.isSignalled());
964 >        Thread.sleep(SHORT_DELAY_MS);
965 >        l.releaseShared(0);
966 >        assertTrue(l.isSignalled());
967 >        t.join();
968 >    }
969 >
970 >    /**
971 >     * acquireSharedInterruptibly throws IE if interrupted before released
972 >     */
973 >    public void testAcquireSharedInterruptibly_InterruptedException() throws InterruptedException {
974 >        final BooleanLatch l = new BooleanLatch();
975 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
976 >            public void realRun() throws InterruptedException {
977 >                threadAssertFalse(l.isSignalled());
978 >                l.acquireSharedInterruptibly(0);
979 >            }});
980 >
981 >        t.start();
982 >        assertFalse(l.isSignalled());
983 >        t.interrupt();
984 >        t.join();
985 >    }
986 >
987 >    /**
988 >     * acquireSharedTimed throws IE if interrupted before released
989 >     */
990 >    public void testAcquireSharedNanos_InterruptedException() throws InterruptedException {
991 >        final BooleanLatch l = new BooleanLatch();
992 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
993 >            public void realRun() throws InterruptedException {
994 >                threadAssertFalse(l.isSignalled());
995 >                l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000);
996 >            }});
997 >
998 >        t.start();
999 >        Thread.sleep(SHORT_DELAY_MS);
1000 >        assertFalse(l.isSignalled());
1001 >        t.interrupt();
1002 >        t.join();
1003 >    }
1004 >
1005 >    /**
1006 >     * acquireSharedTimed times out if not released before timeout
1007 >     */
1008 >    public void testAcquireSharedNanos_Timeout() throws InterruptedException {
1009 >        final BooleanLatch l = new BooleanLatch();
1010 >        Thread t = new Thread(new CheckedRunnable() {
1011 >            public void realRun() throws InterruptedException {
1012 >                threadAssertFalse(l.isSignalled());
1013 >                threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000));
1014 >            }});
1015 >
1016 >        t.start();
1017 >        Thread.sleep(SHORT_DELAY_MS);
1018 >        assertFalse(l.isSignalled());
1019 >        t.join();
1020 >    }
1021 >
1022   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines