[cvs] / jsr166 / src / test / tck / AbstractQueuedSynchronizerTest.java Repository:
ViewVC logotype

Diff of /jsr166/src/test/tck/AbstractQueuedSynchronizerTest.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.32, Mon Nov 30 08:31:09 2009 UTC revision 1.47, Wed Dec 31 19:05:42 2014 UTC
# Line 1  Line 1 
1  /*  /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at   * Expert Group and released to the public domain, as explained at
4   * http://creativecommons.org/licenses/publicdomain   * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.   * Pat Fisher, Mike Judd.
7   */   */
8    
   
 import junit.framework.*;  
 import java.util.*;  
 import java.util.concurrent.*;  
9  import static java.util.concurrent.TimeUnit.MILLISECONDS;  import static java.util.concurrent.TimeUnit.MILLISECONDS;
10  import java.util.concurrent.locks.*;  import static java.util.concurrent.TimeUnit.NANOSECONDS;
11  import java.io.*;  
12    import java.util.Arrays;
13    import java.util.Collection;
14    import java.util.HashSet;
15    import java.util.concurrent.locks.AbstractQueuedSynchronizer;
16    import java.util.concurrent.locks.AbstractQueuedSynchronizer.ConditionObject;
17    
18    import junit.framework.AssertionFailedError;
19    import junit.framework.Test;
20    import junit.framework.TestSuite;
21    
22  public class AbstractQueuedSynchronizerTest extends JSR166TestCase {  public class AbstractQueuedSynchronizerTest extends JSR166TestCase {
23      public static void main(String[] args) {      public static void main(String[] args) {
# Line 23  Line 28 
28      }      }
29    
30      /**      /**
31       * A simple mutex class, adapted from the       * A simple mutex class, adapted from the class javadoc.  Exclusive
32       * AbstractQueuedSynchronizer javadoc.  Exclusive acquire tests       * acquire tests exercise this as a sample user extension.  Other
33       * exercise this as a sample user extension.  Other       * methods/features of AbstractQueuedSynchronizer are tested via
34       * methods/features of AbstractQueuedSynchronizerTest are tested       * other test classes, including those for ReentrantLock,
35       * via other test classes, including those for ReentrantLock,       * ReentrantReadWriteLock, and Semaphore.
      * ReentrantReadWriteLock, and Semaphore  
36       */       */
37      static class Mutex extends AbstractQueuedSynchronizer {      static class Mutex extends AbstractQueuedSynchronizer {
38          public boolean isHeldExclusively() { return getState() == 1; }          /** An eccentric value for locked synchronizer state. */
39            static final int LOCKED = (1 << 31) | (1 << 15);
40    
41          public boolean tryAcquire(int acquires) {          static final int UNLOCKED = 0;
42              assertTrue(acquires == 1);  
43              return compareAndSetState(0, 1);          @Override public boolean isHeldExclusively() {
44                int state = getState();
45                assertTrue(state == UNLOCKED || state == LOCKED);
46                return state == LOCKED;
47          }          }
48    
49          public boolean tryRelease(int releases) {          @Override public boolean tryAcquire(int acquires) {
50              if (getState() == 0) throw new IllegalMonitorStateException();              assertEquals(LOCKED, acquires);
51              setState(0);              return compareAndSetState(UNLOCKED, LOCKED);
52            }
53    
54            @Override public boolean tryRelease(int releases) {
55                if (getState() != LOCKED) throw new IllegalMonitorStateException();
56                assertEquals(LOCKED, releases);
57                setState(UNLOCKED);
58              return true;              return true;
59          }          }
60    
61          public AbstractQueuedSynchronizer.ConditionObject newCondition() { return new AbstractQueuedSynchronizer.ConditionObject(); }          public boolean tryAcquireNanos(long nanos) throws InterruptedException {
62                return tryAcquireNanos(LOCKED, nanos);
63            }
64    
65            public boolean tryAcquire() {
66                return tryAcquire(LOCKED);
67            }
68    
69            public boolean tryRelease() {
70                return tryRelease(LOCKED);
71            }
72    
73            public void acquire() {
74                acquire(LOCKED);
75            }
76    
77            public void acquireInterruptibly() throws InterruptedException {
78                acquireInterruptibly(LOCKED);
79            }
80    
81            public void release() {
82                release(LOCKED);
83      }      }
84    
85            public ConditionObject newCondition() {
86                return new ConditionObject();
87            }
88        }
89    
90      /**      /**
91       * A simple latch class, to test shared mode.       * A simple latch class, to test shared mode.
# Line 71  Line 109 
109       */       */
110      class InterruptibleSyncRunnable extends CheckedRunnable {      class InterruptibleSyncRunnable extends CheckedRunnable {
111          final Mutex sync;          final Mutex sync;
112          InterruptibleSyncRunnable(Mutex l) { sync = l; }          InterruptibleSyncRunnable(Mutex sync) { this.sync = sync; }
113          public void realRun() throws InterruptedException {          public void realRun() throws InterruptedException {
114              sync.acquireInterruptibly(1);              sync.acquireInterruptibly();
115          }          }
116      }      }
117    
   
118      /**      /**
119       * A runnable calling acquireInterruptibly that expects to be       * A runnable calling acquireInterruptibly that expects to be
120       * interrupted.       * interrupted.
121       */       */
122      class InterruptedSyncRunnable extends CheckedInterruptedRunnable {      class InterruptedSyncRunnable extends CheckedInterruptedRunnable {
123          final Mutex sync;          final Mutex sync;
124          InterruptedSyncRunnable(Mutex l) { sync = l; }          InterruptedSyncRunnable(Mutex sync) { this.sync = sync; }
125          public void realRun() throws InterruptedException {          public void realRun() throws InterruptedException {
126              sync.acquireInterruptibly(1);              sync.acquireInterruptibly();
127            }
128        }
129    
130        /** A constant to clarify calls to checking methods below. */
131        static final Thread[] NO_THREADS = new Thread[0];
132    
133        /**
134         * Spin-waits until sync.isQueued(t) becomes true.
135         */
136        void waitForQueuedThread(AbstractQueuedSynchronizer sync, Thread t) {
137            long startTime = System.nanoTime();
138            while (!sync.isQueued(t)) {
139                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
140                    throw new AssertionFailedError("timed out");
141                Thread.yield();
142            }
143            assertTrue(t.isAlive());
144        }
145    
146        /**
147         * Checks that sync has exactly the given queued threads.
148         */
149        void assertHasQueuedThreads(AbstractQueuedSynchronizer sync,
150                                    Thread... expected) {
151            Collection<Thread> actual = sync.getQueuedThreads();
152            assertEquals(expected.length > 0, sync.hasQueuedThreads());
153            assertEquals(expected.length, sync.getQueueLength());
154            assertEquals(expected.length, actual.size());
155            assertEquals(expected.length == 0, actual.isEmpty());
156            assertEquals(new HashSet<Thread>(actual),
157                         new HashSet<Thread>(Arrays.asList(expected)));
158        }
159    
160        /**
161         * Checks that sync has exactly the given (exclusive) queued threads.
162         */
163        void assertHasExclusiveQueuedThreads(AbstractQueuedSynchronizer sync,
164                                             Thread... expected) {
165            assertHasQueuedThreads(sync, expected);
166            assertEquals(new HashSet<Thread>(sync.getExclusiveQueuedThreads()),
167                         new HashSet<Thread>(sync.getQueuedThreads()));
168            assertEquals(0, sync.getSharedQueuedThreads().size());
169            assertTrue(sync.getSharedQueuedThreads().isEmpty());
170        }
171    
172        /**
173         * Checks that sync has exactly the given (shared) queued threads.
174         */
175        void assertHasSharedQueuedThreads(AbstractQueuedSynchronizer sync,
176                                          Thread... expected) {
177            assertHasQueuedThreads(sync, expected);
178            assertEquals(new HashSet<Thread>(sync.getSharedQueuedThreads()),
179                         new HashSet<Thread>(sync.getQueuedThreads()));
180            assertEquals(0, sync.getExclusiveQueuedThreads().size());
181            assertTrue(sync.getExclusiveQueuedThreads().isEmpty());
182        }
183    
184        /**
185         * Checks that condition c has exactly the given waiter threads,
186         * after acquiring mutex.
187         */
188        void assertHasWaitersUnlocked(Mutex sync, ConditionObject c,
189                                     Thread... threads) {
190            sync.acquire();
191            assertHasWaitersLocked(sync, c, threads);
192            sync.release();
193        }
194    
195        /**
196         * Checks that condition c has exactly the given waiter threads.
197         */
198        void assertHasWaitersLocked(Mutex sync, ConditionObject c,
199                                    Thread... threads) {
200            assertEquals(threads.length > 0, sync.hasWaiters(c));
201            assertEquals(threads.length, sync.getWaitQueueLength(c));
202            assertEquals(threads.length == 0, sync.getWaitingThreads(c).isEmpty());
203            assertEquals(threads.length, sync.getWaitingThreads(c).size());
204            assertEquals(new HashSet<Thread>(sync.getWaitingThreads(c)),
205                         new HashSet<Thread>(Arrays.asList(threads)));
206        }
207    
208        enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }
209    
210        /**
211         * Awaits condition using the specified AwaitMethod.
212         */
213        void await(ConditionObject c, AwaitMethod awaitMethod)
214                throws InterruptedException {
215            long timeoutMillis = 2 * LONG_DELAY_MS;
216            switch (awaitMethod) {
217            case await:
218                c.await();
219                break;
220            case awaitTimed:
221                assertTrue(c.await(timeoutMillis, MILLISECONDS));
222                break;
223            case awaitNanos:
224                long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
225                long nanosRemaining = c.awaitNanos(nanosTimeout);
226                assertTrue(nanosRemaining > 0);
227                break;
228            case awaitUntil:
229                assertTrue(c.awaitUntil(delayedDate(timeoutMillis)));
230                break;
231            }
232        }
233    
234        /**
235         * Checks that awaiting the given condition times out (using the
236         * default timeout duration).
237         */
238        void assertAwaitTimesOut(ConditionObject c, AwaitMethod awaitMethod) {
239            long timeoutMillis = timeoutMillis();
240            long startTime = System.nanoTime();
241            try {
242                switch (awaitMethod) {
243                case awaitTimed:
244                    assertFalse(c.await(timeoutMillis, MILLISECONDS));
245                    break;
246                case awaitNanos:
247                    long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
248                    long nanosRemaining = c.awaitNanos(nanosTimeout);
249                    assertTrue(nanosRemaining <= 0);
250                    break;
251                case awaitUntil:
252                    assertFalse(c.awaitUntil(delayedDate(timeoutMillis)));
253                    break;
254                default:
255                    throw new UnsupportedOperationException();
256          }          }
257            } catch (InterruptedException ie) { threadUnexpectedException(ie); }
258            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
259      }      }
260    
261      /**      /**
262       * isHeldExclusively is false upon construction       * isHeldExclusively is false upon construction
263       */       */
264      public void testIsHeldExclusively() {      public void testIsHeldExclusively() {
265          Mutex rl = new Mutex();          Mutex sync = new Mutex();
266          assertFalse(rl.isHeldExclusively());          assertFalse(sync.isHeldExclusively());
267      }      }
268    
269      /**      /**
270       * acquiring released sync succeeds       * acquiring released sync succeeds
271       */       */
272      public void testAcquire() {      public void testAcquire() {
273          Mutex rl = new Mutex();          Mutex sync = new Mutex();
274          rl.acquire(1);          sync.acquire();
275          assertTrue(rl.isHeldExclusively());          assertTrue(sync.isHeldExclusively());
276          rl.release(1);          sync.release();
277          assertFalse(rl.isHeldExclusively());          assertFalse(sync.isHeldExclusively());
278      }      }
279    
280      /**      /**
281       * tryAcquire on an released sync succeeds       * tryAcquire on a released sync succeeds
282       */       */
283      public void testTryAcquire() {      public void testTryAcquire() {
284          Mutex rl = new Mutex();          Mutex sync = new Mutex();
285          assertTrue(rl.tryAcquire(1));          assertTrue(sync.tryAcquire());
286          assertTrue(rl.isHeldExclusively());          assertTrue(sync.isHeldExclusively());
287          rl.release(1);          sync.release();
288            assertFalse(sync.isHeldExclusively());
289      }      }
290    
291      /**      /**
292       * hasQueuedThreads reports whether there are waiting threads       * hasQueuedThreads reports whether there are waiting threads
293       */       */
294      public void testhasQueuedThreads() throws InterruptedException {      public void testHasQueuedThreads() {
295          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
         Thread t1 = new Thread(new InterruptedSyncRunnable(sync));  
         Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));  
296          assertFalse(sync.hasQueuedThreads());          assertFalse(sync.hasQueuedThreads());
297          sync.acquire(1);          sync.acquire();
298          t1.start();          Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync));
299          Thread.sleep(SHORT_DELAY_MS);          waitForQueuedThread(sync, t1);
300          assertTrue(sync.hasQueuedThreads());          assertTrue(sync.hasQueuedThreads());
301          t2.start();          Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync));
302          Thread.sleep(SHORT_DELAY_MS);          waitForQueuedThread(sync, t2);
303          assertTrue(sync.hasQueuedThreads());          assertTrue(sync.hasQueuedThreads());
304          t1.interrupt();          t1.interrupt();
305          Thread.sleep(SHORT_DELAY_MS);          awaitTermination(t1);
306          assertTrue(sync.hasQueuedThreads());          assertTrue(sync.hasQueuedThreads());
307          sync.release(1);          sync.release();
308          Thread.sleep(SHORT_DELAY_MS);          awaitTermination(t2);
309          assertFalse(sync.hasQueuedThreads());          assertFalse(sync.hasQueuedThreads());
         t1.join();  
         t2.join();  
310      }      }
311    
312      /**      /**
313       * isQueued(null) throws NPE       * isQueued(null) throws NullPointerException
314       */       */
315      public void testIsQueuedNPE() {      public void testIsQueuedNPE() {
316          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
# Line 156  Line 321 
321      }      }
322    
323      /**      /**
324       * isQueued reports whether a thread is queued.       * isQueued reports whether a thread is queued
325       */       */
326      public void testIsQueued() throws InterruptedException {      public void testIsQueued() {
327          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
328          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
329          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
330          assertFalse(sync.isQueued(t1));          assertFalse(sync.isQueued(t1));
331          assertFalse(sync.isQueued(t2));          assertFalse(sync.isQueued(t2));
332          sync.acquire(1);          sync.acquire();
333          t1.start();          t1.start();
334          Thread.sleep(SHORT_DELAY_MS);          waitForQueuedThread(sync, t1);
335          assertTrue(sync.isQueued(t1));          assertTrue(sync.isQueued(t1));
336            assertFalse(sync.isQueued(t2));
337          t2.start();          t2.start();
338          Thread.sleep(SHORT_DELAY_MS);          waitForQueuedThread(sync, t2);
339          assertTrue(sync.isQueued(t1));          assertTrue(sync.isQueued(t1));
340          assertTrue(sync.isQueued(t2));          assertTrue(sync.isQueued(t2));
341          t1.interrupt();          t1.interrupt();
342          Thread.sleep(SHORT_DELAY_MS);          awaitTermination(t1);
343          assertFalse(sync.isQueued(t1));          assertFalse(sync.isQueued(t1));
344          assertTrue(sync.isQueued(t2));          assertTrue(sync.isQueued(t2));
345          sync.release(1);          sync.release();
346          Thread.sleep(SHORT_DELAY_MS);          awaitTermination(t2);
347          assertFalse(sync.isQueued(t1));          assertFalse(sync.isQueued(t1));
         Thread.sleep(SHORT_DELAY_MS);  
348          assertFalse(sync.isQueued(t2));          assertFalse(sync.isQueued(t2));
         t1.join();  
         t2.join();  
349      }      }
350    
351      /**      /**
352       * getFirstQueuedThread returns first waiting thread or null if none       * getFirstQueuedThread returns first waiting thread or null if none
353       */       */
354      public void testGetFirstQueuedThread() throws InterruptedException {      public void testGetFirstQueuedThread() {
355          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
         Thread t1 = new Thread(new InterruptedSyncRunnable(sync));  
         Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));  
356          assertNull(sync.getFirstQueuedThread());          assertNull(sync.getFirstQueuedThread());
357          sync.acquire(1);          sync.acquire();
358          t1.start();          Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync));
359          Thread.sleep(SHORT_DELAY_MS);          waitForQueuedThread(sync, t1);
360          assertEquals(t1, sync.getFirstQueuedThread());          assertEquals(t1, sync.getFirstQueuedThread());
361          t2.start();          Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync));
362          Thread.sleep(SHORT_DELAY_MS);          waitForQueuedThread(sync, t2);
363          assertEquals(t1, sync.getFirstQueuedThread());          assertEquals(t1, sync.getFirstQueuedThread());
364          t1.interrupt();          t1.interrupt();
365          Thread.sleep(SHORT_DELAY_MS);          awaitTermination(t1);
         Thread.sleep(SHORT_DELAY_MS);  
366          assertEquals(t2, sync.getFirstQueuedThread());          assertEquals(t2, sync.getFirstQueuedThread());
367          sync.release(1);          sync.release();
368          Thread.sleep(SHORT_DELAY_MS);          awaitTermination(t2);
369          assertNull(sync.getFirstQueuedThread());          assertNull(sync.getFirstQueuedThread());
         t1.join();  
         t2.join();  
370      }      }
371    
   
372      /**      /**
373       * hasContended reports false if no thread has ever blocked, else true       * hasContended reports false if no thread has ever blocked, else true
374       */       */
375      public void testHasContended() throws InterruptedException {      public void testHasContended() {
376          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
         Thread t1 = new Thread(new InterruptedSyncRunnable(sync));  
         Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));  
377          assertFalse(sync.hasContended());          assertFalse(sync.hasContended());
378          sync.acquire(1);          sync.acquire();
379          t1.start();          assertFalse(sync.hasContended());
380          Thread.sleep(SHORT_DELAY_MS);          Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync));
381            waitForQueuedThread(sync, t1);
382          assertTrue(sync.hasContended());          assertTrue(sync.hasContended());
383          t2.start();          Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync));
384          Thread.sleep(SHORT_DELAY_MS);          waitForQueuedThread(sync, t2);
385          assertTrue(sync.hasContended());          assertTrue(sync.hasContended());
386          t1.interrupt();          t1.interrupt();
387          Thread.sleep(SHORT_DELAY_MS);          awaitTermination(t1);
388          assertTrue(sync.hasContended());          assertTrue(sync.hasContended());
389          sync.release(1);          sync.release();
390          Thread.sleep(SHORT_DELAY_MS);          awaitTermination(t2);
391          assertTrue(sync.hasContended());          assertTrue(sync.hasContended());
         t1.join();  
         t2.join();  
392      }      }
393    
394      /**      /**
395       * getQueuedThreads includes waiting threads       * getQueuedThreads returns all waiting threads
396       */       */
397      public void testGetQueuedThreads() throws InterruptedException {      public void testGetQueuedThreads() {
398          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
399          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
400          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
401          assertTrue(sync.getQueuedThreads().isEmpty());          assertHasExclusiveQueuedThreads(sync, NO_THREADS);
402          sync.acquire(1);          sync.acquire();
403          assertTrue(sync.getQueuedThreads().isEmpty());          assertHasExclusiveQueuedThreads(sync, NO_THREADS);
404          t1.start();          t1.start();
405          Thread.sleep(SHORT_DELAY_MS);          waitForQueuedThread(sync, t1);
406            assertHasExclusiveQueuedThreads(sync, t1);
407          assertTrue(sync.getQueuedThreads().contains(t1));          assertTrue(sync.getQueuedThreads().contains(t1));
408            assertFalse(sync.getQueuedThreads().contains(t2));
409          t2.start();          t2.start();
410          Thread.sleep(SHORT_DELAY_MS);          waitForQueuedThread(sync, t2);
411            assertHasExclusiveQueuedThreads(sync, t1, t2);
412          assertTrue(sync.getQueuedThreads().contains(t1));          assertTrue(sync.getQueuedThreads().contains(t1));
413          assertTrue(sync.getQueuedThreads().contains(t2));          assertTrue(sync.getQueuedThreads().contains(t2));
414          t1.interrupt();          t1.interrupt();
415          Thread.sleep(SHORT_DELAY_MS);          awaitTermination(t1);
416          assertFalse(sync.getQueuedThreads().contains(t1));          assertHasExclusiveQueuedThreads(sync, t2);
417          assertTrue(sync.getQueuedThreads().contains(t2));          sync.release();
418          sync.release(1);          awaitTermination(t2);
419          Thread.sleep(SHORT_DELAY_MS);          assertHasExclusiveQueuedThreads(sync, NO_THREADS);
         assertTrue(sync.getQueuedThreads().isEmpty());  
         t1.join();  
         t2.join();  
420      }      }
421    
422      /**      /**
423       * getExclusiveQueuedThreads includes waiting threads       * getExclusiveQueuedThreads returns all exclusive waiting threads
424       */       */
425      public void testGetExclusiveQueuedThreads() throws InterruptedException {      public void testGetExclusiveQueuedThreads() {
426          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
427          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));          Thread t1 = new Thread(new InterruptedSyncRunnable(sync));
428          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));          Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));
429          assertTrue(sync.getExclusiveQueuedThreads().isEmpty());          assertHasExclusiveQueuedThreads(sync, NO_THREADS);
430          sync.acquire(1);          sync.acquire();
431          assertTrue(sync.getExclusiveQueuedThreads().isEmpty());          assertHasExclusiveQueuedThreads(sync, NO_THREADS);
432          t1.start();          t1.start();
433          Thread.sleep(SHORT_DELAY_MS);          waitForQueuedThread(sync, t1);
434            assertHasExclusiveQueuedThreads(sync, t1);
435          assertTrue(sync.getExclusiveQueuedThreads().contains(t1));          assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
436            assertFalse(sync.getExclusiveQueuedThreads().contains(t2));
437          t2.start();          t2.start();
438          Thread.sleep(SHORT_DELAY_MS);          waitForQueuedThread(sync, t2);
439            assertHasExclusiveQueuedThreads(sync, t1, t2);
440          assertTrue(sync.getExclusiveQueuedThreads().contains(t1));          assertTrue(sync.getExclusiveQueuedThreads().contains(t1));
441          assertTrue(sync.getExclusiveQueuedThreads().contains(t2));          assertTrue(sync.getExclusiveQueuedThreads().contains(t2));
442          t1.interrupt();          t1.interrupt();
443          Thread.sleep(SHORT_DELAY_MS);          awaitTermination(t1);
444          assertFalse(sync.getExclusiveQueuedThreads().contains(t1));          assertHasExclusiveQueuedThreads(sync, t2);
445          assertTrue(sync.getExclusiveQueuedThreads().contains(t2));          sync.release();
446          sync.release(1);          awaitTermination(t2);
447          Thread.sleep(SHORT_DELAY_MS);          assertHasExclusiveQueuedThreads(sync, NO_THREADS);
         assertTrue(sync.getExclusiveQueuedThreads().isEmpty());  
         t1.join();  
         t2.join();  
448      }      }
449    
450      /**      /**
451       * getSharedQueuedThreads does not include exclusively waiting threads       * getSharedQueuedThreads does not include exclusively waiting threads
452       */       */
453      public void testGetSharedQueuedThreads() throws InterruptedException {      public void testGetSharedQueuedThreads_Exclusive() {
454          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
         Thread t1 = new Thread(new InterruptedSyncRunnable(sync));  
         Thread t2 = new Thread(new InterruptibleSyncRunnable(sync));  
455          assertTrue(sync.getSharedQueuedThreads().isEmpty());          assertTrue(sync.getSharedQueuedThreads().isEmpty());
456          sync.acquire(1);          sync.acquire();
457          assertTrue(sync.getSharedQueuedThreads().isEmpty());          assertTrue(sync.getSharedQueuedThreads().isEmpty());
458          t1.start();          Thread t1 = newStartedThread(new InterruptedSyncRunnable(sync));
459          Thread.sleep(SHORT_DELAY_MS);          waitForQueuedThread(sync, t1);
460          assertTrue(sync.getSharedQueuedThreads().isEmpty());          assertTrue(sync.getSharedQueuedThreads().isEmpty());
461          t2.start();          Thread t2 = newStartedThread(new InterruptibleSyncRunnable(sync));
462          Thread.sleep(SHORT_DELAY_MS);          waitForQueuedThread(sync, t2);
463          assertTrue(sync.getSharedQueuedThreads().isEmpty());          assertTrue(sync.getSharedQueuedThreads().isEmpty());
464          t1.interrupt();          t1.interrupt();
465          Thread.sleep(SHORT_DELAY_MS);          awaitTermination(t1);
466          assertTrue(sync.getSharedQueuedThreads().isEmpty());          assertTrue(sync.getSharedQueuedThreads().isEmpty());
467          sync.release(1);          sync.release();
468          Thread.sleep(SHORT_DELAY_MS);          awaitTermination(t2);
469          assertTrue(sync.getSharedQueuedThreads().isEmpty());          assertTrue(sync.getSharedQueuedThreads().isEmpty());
         t1.join();  
         t2.join();  
470      }      }
471    
472      /**      /**
473       * tryAcquireNanos is interruptible.       * getSharedQueuedThreads returns all shared waiting threads
474         */
475        public void testGetSharedQueuedThreads_Shared() {
476            final BooleanLatch l = new BooleanLatch();
477            assertHasSharedQueuedThreads(l, NO_THREADS);
478            Thread t1 = newStartedThread(new CheckedInterruptedRunnable() {
479                public void realRun() throws InterruptedException {
480                    l.acquireSharedInterruptibly(0);
481                }});
482            waitForQueuedThread(l, t1);
483            assertHasSharedQueuedThreads(l, t1);
484            Thread t2 = newStartedThread(new CheckedRunnable() {
485                public void realRun() throws InterruptedException {
486                    l.acquireSharedInterruptibly(0);
487                }});
488            waitForQueuedThread(l, t2);
489            assertHasSharedQueuedThreads(l, t1, t2);
490            t1.interrupt();
491            awaitTermination(t1);
492            assertHasSharedQueuedThreads(l, t2);
493            assertTrue(l.releaseShared(0));
494            awaitTermination(t2);
495            assertHasSharedQueuedThreads(l, NO_THREADS);
496        }
497    
498        /**
499         * tryAcquireNanos is interruptible
500       */       */
501      public void testInterruptedException2() throws InterruptedException {      public void testTryAcquireNanos_Interruptible() {
502          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
503          sync.acquire(1);          sync.acquire();
504          Thread t = new Thread(new CheckedInterruptedRunnable() {          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
505              public void realRun() throws InterruptedException {              public void realRun() throws InterruptedException {
506                  sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000L * 1000L);                  sync.tryAcquireNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
507              }});              }});
508    
509          t.start();          waitForQueuedThread(sync, t);
         Thread.sleep(SHORT_DELAY_MS);  
510          t.interrupt();          t.interrupt();
511          t.join();          awaitTermination(t);
512      }      }
513    
   
514      /**      /**
515       * TryAcquire on exclusively held sync fails       * tryAcquire on exclusively held sync fails
516       */       */
517      public void testTryAcquireWhenSynced() throws InterruptedException {      public void testTryAcquireWhenSynced() {
518          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
519          sync.acquire(1);          sync.acquire();
520          Thread t = new Thread(new CheckedRunnable() {          Thread t = newStartedThread(new CheckedRunnable() {
521              public void realRun() {              public void realRun() {
522                  threadAssertFalse(sync.tryAcquire(1));                  assertFalse(sync.tryAcquire());
523              }});              }});
524    
525          t.start();          awaitTermination(t);
526          t.join();          sync.release();
         sync.release(1);  
527      }      }
528    
529      /**      /**
530       * tryAcquireNanos on an exclusively held sync times out       * tryAcquireNanos on an exclusively held sync times out
531       */       */
532      public void testAcquireNanos_Timeout() throws InterruptedException {      public void testAcquireNanos_Timeout() {
533          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
534          sync.acquire(1);          sync.acquire();
535          Thread t = new Thread(new CheckedRunnable() {          Thread t = newStartedThread(new CheckedRunnable() {
536              public void realRun() throws InterruptedException {              public void realRun() throws InterruptedException {
537                  threadAssertFalse(sync.tryAcquireNanos(1, SHORT_DELAY_MS * 1000L * 1000L));                  long startTime = System.nanoTime();
538                    long nanos = MILLISECONDS.toNanos(timeoutMillis());
539                    assertFalse(sync.tryAcquireNanos(nanos));
540                    assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
541              }});              }});
542    
543          t.start();          awaitTermination(t);
544          t.join();          sync.release();
         sync.release(1);  
545      }      }
546    
   
547      /**      /**
548       * getState is true when acquired and false when not       * getState is true when acquired and false when not
549       */       */
550      public void testGetState() throws InterruptedException {      public void testGetState() {
551          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
552          sync.acquire(1);          sync.acquire();
553          assertTrue(sync.isHeldExclusively());          assertTrue(sync.isHeldExclusively());
554          sync.release(1);          sync.release();
555          assertFalse(sync.isHeldExclusively());          assertFalse(sync.isHeldExclusively());
556          Thread t = new Thread(new CheckedRunnable() {  
557            final BooleanLatch acquired = new BooleanLatch();
558            final BooleanLatch done = new BooleanLatch();
559            Thread t = newStartedThread(new CheckedRunnable() {
560              public void realRun() throws InterruptedException {              public void realRun() throws InterruptedException {
561                  sync.acquire(1);                  sync.acquire();
562                  Thread.sleep(SMALL_DELAY_MS);                  assertTrue(acquired.releaseShared(0));
563                  sync.release(1);                  done.acquireShared(0);
564                    sync.release();
565              }});              }});
566    
567          t.start();          acquired.acquireShared(0);
         Thread.sleep(SHORT_DELAY_MS);  
568          assertTrue(sync.isHeldExclusively());          assertTrue(sync.isHeldExclusively());
569          t.join();          assertTrue(done.releaseShared(0));
570            awaitTermination(t);
571          assertFalse(sync.isHeldExclusively());          assertFalse(sync.isHeldExclusively());
572      }      }
573    
   
     /**  
      * acquireInterruptibly is interruptible.  
      */  
     public void testAcquireInterruptibly1() throws InterruptedException {  
         final Mutex sync = new Mutex();  
         sync.acquire(1);  
         Thread t = new Thread(new InterruptedSyncRunnable(sync));  
   
         t.start();  
         Thread.sleep(SHORT_DELAY_MS);  
         t.interrupt();  
         Thread.sleep(SHORT_DELAY_MS);  
         sync.release(1);  
         t.join();  
     }  
   
574      /**      /**
575       * acquireInterruptibly succeeds when released, else is interruptible       * acquireInterruptibly succeeds when released, else is interruptible
576       */       */
577      public void testAcquireInterruptibly2() throws InterruptedException {      public void testAcquireInterruptibly() throws InterruptedException {
578          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
579          sync.acquireInterruptibly(1);          final BooleanLatch threadStarted = new BooleanLatch();
580          Thread t = new Thread(new InterruptedSyncRunnable(sync));          sync.acquireInterruptibly();
581          t.start();          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
582          Thread.sleep(SHORT_DELAY_MS);              public void realRun() throws InterruptedException {
583                    assertTrue(threadStarted.releaseShared(0));
584                    sync.acquireInterruptibly();
585                }});
586    
587            threadStarted.acquireShared(0);
588            waitForQueuedThread(sync, t);
589          t.interrupt();          t.interrupt();
590            awaitTermination(t);
591          assertTrue(sync.isHeldExclusively());          assertTrue(sync.isHeldExclusively());
         t.join();  
592      }      }
593    
594      /**      /**
# Line 429  Line 596 
596       */       */
597      public void testOwns() {      public void testOwns() {
598          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
599          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();          final ConditionObject c = sync.newCondition();
600          final Mutex sync2 = new Mutex();          final Mutex sync2 = new Mutex();
601          assertTrue(sync.owns(c));          assertTrue(sync.owns(c));
602          assertFalse(sync2.owns(c));          assertFalse(sync2.owns(c));
# Line 438  Line 605 
605      /**      /**
606       * Calling await without holding sync throws IllegalMonitorStateException       * Calling await without holding sync throws IllegalMonitorStateException
607       */       */
608      public void testAwait_IllegalMonitor() throws InterruptedException {      public void testAwait_IMSE() {
609          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
610          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();          final ConditionObject c = sync.newCondition();
611            for (AwaitMethod awaitMethod : AwaitMethod.values()) {
612                long startTime = System.nanoTime();
613          try {          try {
614              c.await();                  await(c, awaitMethod);
615              shouldThrow();              shouldThrow();
616          } catch (IllegalMonitorStateException success) {}              } catch (IllegalMonitorStateException success) {
617                } catch (InterruptedException e) { threadUnexpectedException(e); }
618                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
619            }
620      }      }
621    
622      /**      /**
623       * Calling signal without holding sync throws IllegalMonitorStateException       * Calling signal without holding sync throws IllegalMonitorStateException
624       */       */
625      public void testSignal_IllegalMonitor() {      public void testSignal_IMSE() {
626          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
627          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();          final ConditionObject c = sync.newCondition();
628          try {          try {
629              c.signal();              c.signal();
630              shouldThrow();              shouldThrow();
631          } catch (IllegalMonitorStateException success) {}          } catch (IllegalMonitorStateException success) {}
632            assertHasWaitersUnlocked(sync, c, NO_THREADS);
633      }      }
634    
635      /**      /**
636       * awaitNanos without a signal times out       * Calling signalAll without holding sync throws IllegalMonitorStateException
      */  
     public void testAwaitNanos_Timeout() throws InterruptedException {  
         final Mutex sync = new Mutex();  
         final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();  
         sync.acquire(1);  
         long t = c.awaitNanos(100);  
         assertTrue(t <= 0);  
         sync.release(1);  
     }  
   
     /**  
      *  Timed await without a signal times out  
637       */       */
638      public void testAwait_Timeout() throws InterruptedException {      public void testSignalAll_IMSE() {
639          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
640          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();          final ConditionObject c = sync.newCondition();
641          sync.acquire(1);          try {
642          assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS));              c.signalAll();
643          sync.release(1);              shouldThrow();
644            } catch (IllegalMonitorStateException success) {}
645      }      }
646    
647      /**      /**
648       * awaitUntil without a signal times out       * await/awaitNanos/awaitUntil without a signal times out
649       */       */
650      public void testAwaitUntil_Timeout() throws InterruptedException {      public void testAwaitTimed_Timeout() { testAwait_Timeout(AwaitMethod.awaitTimed); }
651        public void testAwaitNanos_Timeout() { testAwait_Timeout(AwaitMethod.awaitNanos); }
652        public void testAwaitUntil_Timeout() { testAwait_Timeout(AwaitMethod.awaitUntil); }
653        public void testAwait_Timeout(AwaitMethod awaitMethod) {
654          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
655          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();          final ConditionObject c = sync.newCondition();
656          sync.acquire(1);          sync.acquire();
657          java.util.Date d = new java.util.Date();          assertAwaitTimesOut(c, awaitMethod);
658          assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));          sync.release();
         sync.release(1);  
659      }      }
660    
661      /**      /**
662       * await returns when signalled       * await/awaitNanos/awaitUntil returns when signalled
663       */       */
664      public void testAwait() throws InterruptedException {      public void testSignal_await()      { testSignal(AwaitMethod.await); }
665        public void testSignal_awaitTimed() { testSignal(AwaitMethod.awaitTimed); }
666        public void testSignal_awaitNanos() { testSignal(AwaitMethod.awaitNanos); }
667        public void testSignal_awaitUntil() { testSignal(AwaitMethod.awaitUntil); }
668        public void testSignal(final AwaitMethod awaitMethod) {
669          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
670          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();          final ConditionObject c = sync.newCondition();
671          Thread t = new Thread(new CheckedRunnable() {          final BooleanLatch acquired = new BooleanLatch();
672            Thread t = newStartedThread(new CheckedRunnable() {
673              public void realRun() throws InterruptedException {              public void realRun() throws InterruptedException {
674                  sync.acquire(1);                  sync.acquire();
675                  c.await();                  assertTrue(acquired.releaseShared(0));
676                  sync.release(1);                  await(c, awaitMethod);
677                    sync.release();
678              }});              }});
679    
680          t.start();          acquired.acquireShared(0);
681          Thread.sleep(SHORT_DELAY_MS);          sync.acquire();
682          sync.acquire(1);          assertHasWaitersLocked(sync, c, t);
683            assertHasExclusiveQueuedThreads(sync, NO_THREADS);
684          c.signal();          c.signal();
685          sync.release(1);          assertHasWaitersLocked(sync, c, NO_THREADS);
686          t.join(SHORT_DELAY_MS);          assertHasExclusiveQueuedThreads(sync, t);
687          assertFalse(t.isAlive());          sync.release();
688            awaitTermination(t);
689      }      }
690    
   
   
691      /**      /**
692       * hasWaiters throws NPE if null       * hasWaiters(null) throws NullPointerException
693       */       */
694      public void testHasWaitersNPE() {      public void testHasWaitersNPE() {
695          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
# Line 530  Line 700 
700      }      }
701    
702      /**      /**
703       * getWaitQueueLength throws NPE if null       * getWaitQueueLength(null) throws NullPointerException
704       */       */
705      public void testGetWaitQueueLengthNPE() {      public void testGetWaitQueueLengthNPE() {
706          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
# Line 540  Line 710 
710          } catch (NullPointerException success) {}          } catch (NullPointerException success) {}
711      }      }
712    
   
713      /**      /**
714       * getWaitingThreads throws NPE if null       * getWaitingThreads(null) throws NullPointerException
715       */       */
716      public void testGetWaitingThreadsNPE() {      public void testGetWaitingThreadsNPE() {
717          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
# Line 552  Line 721 
721          } catch (NullPointerException success) {}          } catch (NullPointerException success) {}
722      }      }
723    
   
724      /**      /**
725       * hasWaiters throws IAE if not owned       * hasWaiters throws IllegalArgumentException if not owned
726       */       */
727      public void testHasWaitersIAE() {      public void testHasWaitersIAE() {
728          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
729          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();          final ConditionObject c = sync.newCondition();
730          final Mutex sync2 = new Mutex();          final Mutex sync2 = new Mutex();
731          try {          try {
732              sync2.hasWaiters(c);              sync2.hasWaiters(c);
733              shouldThrow();              shouldThrow();
734          } catch (IllegalArgumentException success) {}          } catch (IllegalArgumentException success) {}
735            assertHasWaitersUnlocked(sync, c, NO_THREADS);
736      }      }
737    
738      /**      /**
739       * hasWaiters throws IMSE if not synced       * hasWaiters throws IllegalMonitorStateException if not synced
740       */       */
741      public void testHasWaitersIMSE() {      public void testHasWaitersIMSE() {
742          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
743          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();          final ConditionObject c = sync.newCondition();
744          try {          try {
745              sync.hasWaiters(c);              sync.hasWaiters(c);
746              shouldThrow();              shouldThrow();
747          } catch (IllegalMonitorStateException success) {}          } catch (IllegalMonitorStateException success) {}
748            assertHasWaitersUnlocked(sync, c, NO_THREADS);
749      }      }
750    
   
751      /**      /**
752       * getWaitQueueLength throws IAE if not owned       * getWaitQueueLength throws IllegalArgumentException if not owned
753       */       */
754      public void testGetWaitQueueLengthIAE() {      public void testGetWaitQueueLengthIAE() {
755          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
756          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();          final ConditionObject c = sync.newCondition();
757          final Mutex sync2 = new Mutex();          final Mutex sync2 = new Mutex();
758          try {          try {
759              sync2.getWaitQueueLength(c);              sync2.getWaitQueueLength(c);
760              shouldThrow();              shouldThrow();
761          } catch (IllegalArgumentException success) {}          } catch (IllegalArgumentException success) {}
762            assertHasWaitersUnlocked(sync, c, NO_THREADS);
763      }      }
764    
765      /**      /**
766       * getWaitQueueLength throws IMSE if not synced       * getWaitQueueLength throws IllegalMonitorStateException if not synced
767       */       */
768      public void testGetWaitQueueLengthIMSE() {      public void testGetWaitQueueLengthIMSE() {
769          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
770          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();          final ConditionObject c = sync.newCondition();
771          try {          try {
772              sync.getWaitQueueLength(c);              sync.getWaitQueueLength(c);
773              shouldThrow();              shouldThrow();
774          } catch (IllegalMonitorStateException success) {}          } catch (IllegalMonitorStateException success) {}
775            assertHasWaitersUnlocked(sync, c, NO_THREADS);
776      }      }
777    
   
778      /**      /**
779       * getWaitingThreads throws IAE if not owned       * getWaitingThreads throws IllegalArgumentException if not owned
780       */       */
781      public void testGetWaitingThreadsIAE() {      public void testGetWaitingThreadsIAE() {
782          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
783          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();          final ConditionObject c = sync.newCondition();
784          final Mutex sync2 = new Mutex();          final Mutex sync2 = new Mutex();
785          try {          try {
786              sync2.getWaitingThreads(c);              sync2.getWaitingThreads(c);
787              shouldThrow();              shouldThrow();
788          } catch (IllegalArgumentException success) {}          } catch (IllegalArgumentException success) {}
789            assertHasWaitersUnlocked(sync, c, NO_THREADS);
790      }      }
791    
792      /**      /**
793       * getWaitingThreads throws IMSE if not synced       * getWaitingThreads throws IllegalMonitorStateException if not synced
794       */       */
795      public void testGetWaitingThreadsIMSE() {      public void testGetWaitingThreadsIMSE() {
796          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
797          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();          final ConditionObject c = sync.newCondition();
798          try {          try {
799              sync.getWaitingThreads(c);              sync.getWaitingThreads(c);
800              shouldThrow();              shouldThrow();
801          } catch (IllegalMonitorStateException success) {}          } catch (IllegalMonitorStateException success) {}
802            assertHasWaitersUnlocked(sync, c, NO_THREADS);
803      }      }
804    
   
   
805      /**      /**
806       * hasWaiters returns true when a thread is waiting, else false       * hasWaiters returns true when a thread is waiting, else false
807       */       */
808      public void testHasWaiters() throws InterruptedException {      public void testHasWaiters() {
809          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
810          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();          final ConditionObject c = sync.newCondition();
811          Thread t = new Thread(new CheckedRunnable() {          final BooleanLatch acquired = new BooleanLatch();
812            Thread t = newStartedThread(new CheckedRunnable() {
813              public void realRun() throws InterruptedException {              public void realRun() throws InterruptedException {
814                  sync.acquire(1);                  sync.acquire();
815                  threadAssertFalse(sync.hasWaiters(c));                  assertHasWaitersLocked(sync, c, NO_THREADS);
816                  threadAssertEquals(0, sync.getWaitQueueLength(c));                  assertFalse(sync.hasWaiters(c));
817                    assertTrue(acquired.releaseShared(0));
818                  c.await();                  c.await();
819                  sync.release(1);                  sync.release();
820              }});              }});
821    
822          t.start();          acquired.acquireShared(0);
823          Thread.sleep(SHORT_DELAY_MS);          sync.acquire();
824          sync.acquire(1);          assertHasWaitersLocked(sync, c, t);
825            assertHasExclusiveQueuedThreads(sync, NO_THREADS);
826          assertTrue(sync.hasWaiters(c));          assertTrue(sync.hasWaiters(c));
         assertEquals(1, sync.getWaitQueueLength(c));  
827          c.signal();          c.signal();
828          sync.release(1);          assertHasWaitersLocked(sync, c, NO_THREADS);
829          Thread.sleep(SHORT_DELAY_MS);          assertHasExclusiveQueuedThreads(sync, t);
         sync.acquire(1);  
830          assertFalse(sync.hasWaiters(c));          assertFalse(sync.hasWaiters(c));
831          assertEquals(0, sync.getWaitQueueLength(c));          sync.release();
832          sync.release(1);  
833          t.join(SHORT_DELAY_MS);          awaitTermination(t);
834          assertFalse(t.isAlive());          assertHasWaitersUnlocked(sync, c, NO_THREADS);
835      }      }
836    
837      /**      /**
838       * getWaitQueueLength returns number of waiting threads       * getWaitQueueLength returns number of waiting threads
839       */       */
840      public void testGetWaitQueueLength() throws InterruptedException {      public void testGetWaitQueueLength() {
841          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
842          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();          final ConditionObject c = sync.newCondition();
843          Thread t1 = new Thread(new CheckedRunnable() {          final BooleanLatch acquired1 = new BooleanLatch();
844            final BooleanLatch acquired2 = new BooleanLatch();
845            final Thread t1 = newStartedThread(new CheckedRunnable() {
846              public void realRun() throws InterruptedException {              public void realRun() throws InterruptedException {
847                  sync.acquire(1);                  sync.acquire();
848                  threadAssertFalse(sync.hasWaiters(c));                  assertHasWaitersLocked(sync, c, NO_THREADS);
849                  threadAssertEquals(0, sync.getWaitQueueLength(c));                  assertEquals(0, sync.getWaitQueueLength(c));
850                    assertTrue(acquired1.releaseShared(0));
851                  c.await();                  c.await();
852                  sync.release(1);                  sync.release();
853              }});              }});
854            acquired1.acquireShared(0);
855            sync.acquire();
856            assertHasWaitersLocked(sync, c, t1);
857            assertEquals(1, sync.getWaitQueueLength(c));
858            sync.release();
859    
860          Thread t2 = new Thread(new CheckedRunnable() {          final Thread t2 = newStartedThread(new CheckedRunnable() {
861              public void realRun() throws InterruptedException {              public void realRun() throws InterruptedException {
862                  sync.acquire(1);                  sync.acquire();
863                  threadAssertTrue(sync.hasWaiters(c));                  assertHasWaitersLocked(sync, c, t1);
864                  threadAssertEquals(1, sync.getWaitQueueLength(c));                  assertEquals(1, sync.getWaitQueueLength(c));
865                    assertTrue(acquired2.releaseShared(0));
866                  c.await();                  c.await();
867                  sync.release(1);                  sync.release();
868              }});              }});
869            acquired2.acquireShared(0);
870          t1.start();          sync.acquire();
871          Thread.sleep(SHORT_DELAY_MS);          assertHasWaitersLocked(sync, c, t1, t2);
872          t2.start();          assertHasExclusiveQueuedThreads(sync, NO_THREADS);
         Thread.sleep(SHORT_DELAY_MS);  
         sync.acquire(1);  
         assertTrue(sync.hasWaiters(c));  
873          assertEquals(2, sync.getWaitQueueLength(c));          assertEquals(2, sync.getWaitQueueLength(c));
874          c.signalAll();          c.signalAll();
875          sync.release(1);          assertHasWaitersLocked(sync, c, NO_THREADS);
876          Thread.sleep(SHORT_DELAY_MS);          assertHasExclusiveQueuedThreads(sync, t1, t2);
         sync.acquire(1);  
         assertFalse(sync.hasWaiters(c));  
877          assertEquals(0, sync.getWaitQueueLength(c));          assertEquals(0, sync.getWaitQueueLength(c));
878          sync.release(1);          sync.release();
879          t1.join(SHORT_DELAY_MS);  
880          t2.join(SHORT_DELAY_MS);          awaitTermination(t1);
881          assertFalse(t1.isAlive());          awaitTermination(t2);
882          assertFalse(t2.isAlive());          assertHasWaitersUnlocked(sync, c, NO_THREADS);
883      }      }
884    
885      /**      /**
886       * getWaitingThreads returns only and all waiting threads       * getWaitingThreads returns only and all waiting threads
887       */       */
888      public void testGetWaitingThreads() throws InterruptedException {      public void testGetWaitingThreads() {
889          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
890          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();          final ConditionObject c = sync.newCondition();
891          Thread t1 = new Thread(new CheckedRunnable() {          final BooleanLatch acquired1 = new BooleanLatch();
892            final BooleanLatch acquired2 = new BooleanLatch();
893            final Thread t1 = new Thread(new CheckedRunnable() {
894              public void realRun() throws InterruptedException {              public void realRun() throws InterruptedException {
895                  sync.acquire(1);                  sync.acquire();
896                  threadAssertTrue(sync.getWaitingThreads(c).isEmpty());                  assertHasWaitersLocked(sync, c, NO_THREADS);
897                    assertTrue(sync.getWaitingThreads(c).isEmpty());
898                    assertTrue(acquired1.releaseShared(0));
899                  c.await();                  c.await();
900                  sync.release(1);                  sync.release();
901              }});              }});
902    
903          Thread t2 = new Thread(new CheckedRunnable() {          final Thread t2 = new Thread(new CheckedRunnable() {
904              public void realRun() throws InterruptedException {              public void realRun() throws InterruptedException {
905                  sync.acquire(1);                  sync.acquire();
906                  threadAssertFalse(sync.getWaitingThreads(c).isEmpty());                  assertHasWaitersLocked(sync, c, t1);
907                    assertTrue(sync.getWaitingThreads(c).contains(t1));
908                    assertFalse(sync.getWaitingThreads(c).isEmpty());
909                    assertEquals(1, sync.getWaitingThreads(c).size());
910                    assertTrue(acquired2.releaseShared(0));
911                  c.await();                  c.await();
912                  sync.release(1);                  sync.release();
913              }});              }});
914    
915          sync.acquire(1);          sync.acquire();
916            assertHasWaitersLocked(sync, c, NO_THREADS);
917            assertFalse(sync.getWaitingThreads(c).contains(t1));
918            assertFalse(sync.getWaitingThreads(c).contains(t2));
919          assertTrue(sync.getWaitingThreads(c).isEmpty());          assertTrue(sync.getWaitingThreads(c).isEmpty());
920          sync.release(1);          assertEquals(0, sync.getWaitingThreads(c).size());
921            sync.release();
922    
923          t1.start();          t1.start();
924          Thread.sleep(SHORT_DELAY_MS);          acquired1.acquireShared(0);
925            sync.acquire();
926            assertHasWaitersLocked(sync, c, t1);
927            assertTrue(sync.getWaitingThreads(c).contains(t1));
928            assertFalse(sync.getWaitingThreads(c).contains(t2));
929            assertFalse(sync.getWaitingThreads(c).isEmpty());
930            assertEquals(1, sync.getWaitingThreads(c).size());
931            sync.release();
932    
933          t2.start();          t2.start();
934          Thread.sleep(SHORT_DELAY_MS);          acquired2.acquireShared(0);
935          sync.acquire(1);          sync.acquire();
936          assertTrue(sync.hasWaiters(c));          assertHasWaitersLocked(sync, c, t1, t2);
937            assertHasExclusiveQueuedThreads(sync, NO_THREADS);
938          assertTrue(sync.getWaitingThreads(c).contains(t1));          assertTrue(sync.getWaitingThreads(c).contains(t1));
939          assertTrue(sync.getWaitingThreads(c).contains(t2));          assertTrue(sync.getWaitingThreads(c).contains(t2));
940            assertFalse(sync.getWaitingThreads(c).isEmpty());
941            assertEquals(2, sync.getWaitingThreads(c).size());
942          c.signalAll();          c.signalAll();
943          sync.release(1);          assertHasWaitersLocked(sync, c, NO_THREADS);
944          Thread.sleep(SHORT_DELAY_MS);          assertHasExclusiveQueuedThreads(sync, t1, t2);
945          sync.acquire(1);          assertFalse(sync.getWaitingThreads(c).contains(t1));
946          assertFalse(sync.hasWaiters(c));          assertFalse(sync.getWaitingThreads(c).contains(t2));
947          assertTrue(sync.getWaitingThreads(c).isEmpty());          assertTrue(sync.getWaitingThreads(c).isEmpty());
948          sync.release(1);          assertEquals(0, sync.getWaitingThreads(c).size());
949          t1.join(SHORT_DELAY_MS);          sync.release();
         t2.join(SHORT_DELAY_MS);  
         assertFalse(t1.isAlive());  
         assertFalse(t2.isAlive());  
     }  
   
950    
951            awaitTermination(t1);
952            awaitTermination(t2);
953            assertHasWaitersUnlocked(sync, c, NO_THREADS);
954        }
955    
956      /**      /**
957       * awaitUninterruptibly doesn't abort on interrupt       * awaitUninterruptibly is uninterruptible
958       */       */
959      public void testAwaitUninterruptibly() throws InterruptedException {      public void testAwaitUninterruptibly() {
960          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
961          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();          final ConditionObject c = sync.newCondition();
962          Thread t = new Thread(new CheckedRunnable() {          final BooleanLatch pleaseInterrupt = new BooleanLatch();
963            Thread t = newStartedThread(new CheckedRunnable() {
964              public void realRun() {              public void realRun() {
965                  sync.acquire(1);                  sync.acquire();
966                    assertTrue(pleaseInterrupt.releaseShared(0));
967                  c.awaitUninterruptibly();                  c.awaitUninterruptibly();
968                  sync.release(1);                  assertTrue(Thread.interrupted());
969                    assertHasWaitersLocked(sync, c, NO_THREADS);
970                    sync.release();
971              }});              }});
972    
973          t.start();          pleaseInterrupt.acquireShared(0);
974          Thread.sleep(SHORT_DELAY_MS);          sync.acquire();
975            assertHasWaitersLocked(sync, c, t);
976            sync.release();
977          t.interrupt();          t.interrupt();
978          sync.acquire(1);          assertHasWaitersUnlocked(sync, c, t);
979            assertThreadStaysAlive(t);
980            sync.acquire();
981            assertHasWaitersLocked(sync, c, t);
982            assertHasExclusiveQueuedThreads(sync, NO_THREADS);
983          c.signal();          c.signal();
984          sync.release(1);          assertHasWaitersLocked(sync, c, NO_THREADS);
985          t.join(SHORT_DELAY_MS);          assertHasExclusiveQueuedThreads(sync, t);
986          assertFalse(t.isAlive());          sync.release();
987            awaitTermination(t);
988      }      }
989    
990      /**      /**
991       * await is interruptible       * await/awaitNanos/awaitUntil is interruptible
992       */       */
993      public void testAwait_Interrupt() throws InterruptedException {      public void testInterruptible_await()      { testInterruptible(AwaitMethod.await); }
994        public void testInterruptible_awaitTimed() { testInterruptible(AwaitMethod.awaitTimed); }
995        public void testInterruptible_awaitNanos() { testInterruptible(AwaitMethod.awaitNanos); }
996        public void testInterruptible_awaitUntil() { testInterruptible(AwaitMethod.awaitUntil); }
997        public void testInterruptible(final AwaitMethod awaitMethod) {
998          final Mutex sync = new Mutex();          final Mutex sync = new Mutex();
999          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();          final ConditionObject c = sync.newCondition();
1000          Thread t = new Thread(new CheckedInterruptedRunnable() {          final BooleanLatch pleaseInterrupt = new BooleanLatch();
1001            Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1002              public void realRun() throws InterruptedException {              public void realRun() throws InterruptedException {
1003                  sync.acquire(1);                  sync.acquire();
1004                  c.await();                  assertTrue(pleaseInterrupt.releaseShared(0));
1005                    await(c, awaitMethod);
1006              }});              }});
1007    
1008          t.start();          pleaseInterrupt.acquireShared(0);
         Thread.sleep(SHORT_DELAY_MS);  
1009          t.interrupt();          t.interrupt();
1010          t.join(SHORT_DELAY_MS);          awaitTermination(t);
         assertFalse(t.isAlive());  
     }  
   
     /**  
      * awaitNanos is interruptible  
      */  
     public void testAwaitNanos_Interrupt() throws InterruptedException {  
         final Mutex sync = new Mutex();  
         final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();  
         Thread t = new Thread(new CheckedInterruptedRunnable() {  
             public void realRun() throws InterruptedException {  
                 sync.acquire(1);  
                 c.awaitNanos(LONG_DELAY_MS * 1000L * 1000L);  
             }});  
   
         t.start();  
         Thread.sleep(SHORT_DELAY_MS);  
         t.interrupt();  
         t.join(SHORT_DELAY_MS);  
         assertFalse(t.isAlive());  
     }  
   
     /**  
      * awaitUntil is interruptible  
      */  
     public void testAwaitUntil_Interrupt() throws InterruptedException {  
         final Mutex sync = new Mutex();  
         final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();  
         Thread t = new Thread(new CheckedInterruptedRunnable() {  
             public void realRun() throws InterruptedException {  
                 sync.acquire(1);  
                 java.util.Date d = new java.util.Date();  
                 c.awaitUntil(new java.util.Date(d.getTime() + 10000));  
             }});  
   
         t.start();  
         Thread.sleep(SHORT_DELAY_MS);  
         t.interrupt();  
         t.join(SHORT_DELAY_MS);  
         assertFalse(t.isAlive());  
1011      }      }
1012    
1013      /**      /**
1014       * signalAll wakes up all threads       * signalAll wakes up all threads
1015       */       */
1016      public void testSignalAll() throws InterruptedException {      public void testSignalAll_await()      { testSignalAll(AwaitMethod.await); }
1017          final Mutex sync = new Mutex();      public void testSignalAll_awaitTimed() { testSignalAll(AwaitMethod.awaitTimed); }
1018          final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition();      public void testSignalAll_awaitNanos() { testSignalAll(AwaitMethod.awaitNanos); }
1019          Thread t1 = new Thread(new CheckedRunnable() {      public void testSignalAll_awaitUntil() { testSignalAll(AwaitMethod.awaitUntil); }
1020              public void realRun() throws InterruptedException {      public void testSignalAll(final AwaitMethod awaitMethod) {
1021                  sync.acquire(1);          final Mutex sync = new Mutex();
1022                  c.await();          final ConditionObject c = sync.newCondition();
1023                  sync.release(1);          final BooleanLatch acquired1 = new BooleanLatch();
1024              }});          final BooleanLatch acquired2 = new BooleanLatch();
1025            Thread t1 = newStartedThread(new CheckedRunnable() {
1026          Thread t2 = new Thread(new CheckedRunnable() {              public void realRun() throws InterruptedException {
1027              public void realRun() throws InterruptedException {                  sync.acquire();
1028                  sync.acquire(1);                  acquired1.releaseShared(0);
1029                  c.await();                  await(c, awaitMethod);
1030                  sync.release(1);                  sync.release();
1031              }});              }});
1032    
1033          t1.start();          Thread t2 = newStartedThread(new CheckedRunnable() {
1034          t2.start();              public void realRun() throws InterruptedException {
1035          Thread.sleep(SHORT_DELAY_MS);                  sync.acquire();
1036          sync.acquire(1);                  acquired2.releaseShared(0);
1037                    await(c, awaitMethod);
1038                    sync.release();
1039                }});
1040    
1041            acquired1.acquireShared(0);
1042            acquired2.acquireShared(0);
1043            sync.acquire();
1044            assertHasWaitersLocked(sync, c, t1, t2);
1045            assertHasExclusiveQueuedThreads(sync, NO_THREADS);
1046          c.signalAll();          c.signalAll();
1047          sync.release(1);          assertHasWaitersLocked(sync, c, NO_THREADS);
1048          t1.join(SHORT_DELAY_MS);          assertHasExclusiveQueuedThreads(sync, t1, t2);
1049          t2.join(SHORT_DELAY_MS);          sync.release();
1050          assertFalse(t1.isAlive());          awaitTermination(t1);
1051          assertFalse(t2.isAlive());          awaitTermination(t2);
1052      }      }
1053    
   
1054      /**      /**
1055       * toString indicates current state       * toString indicates current state
1056       */       */
1057      public void testToString() {      public void testToString() {
1058          Mutex sync = new Mutex();          Mutex sync = new Mutex();
1059          String us = sync.toString();          assertTrue(sync.toString().contains("State = " + Mutex.UNLOCKED));
1060          assertTrue(us.indexOf("State = 0") >= 0);          sync.acquire();
1061          sync.acquire(1);          assertTrue(sync.toString().contains("State = " + Mutex.LOCKED));
         String ls = sync.toString();  
         assertTrue(ls.indexOf("State = 1") >= 0);  
1062      }      }
1063    
1064      /**      /**
1065       * A serialized AQS deserializes with current state       * A serialized AQS deserializes with current state, but no queued threads
1066       */       */
1067      public void testSerialization() throws Exception {      public void testSerialization() {
1068          Mutex l = new Mutex();          Mutex sync = new Mutex();
1069          l.acquire(1);          assertFalse(serialClone(sync).isHeldExclusively());
1070          assertTrue(l.isHeldExclusively());          sync.acquire();
1071            Thread t = newStartedThread(new InterruptedSyncRunnable(sync));
1072          ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);          waitForQueuedThread(sync, t);
1073          ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));          assertTrue(sync.isHeldExclusively());
         out.writeObject(l);  
         out.close();  
   
         ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());  
         ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));  
         Mutex r = (Mutex) in.readObject();  
         assertTrue(r.isHeldExclusively());  
     }  
1074    
1075            Mutex clone = serialClone(sync);
1076            assertTrue(clone.isHeldExclusively());
1077            assertHasExclusiveQueuedThreads(sync, t);
1078            assertHasExclusiveQueuedThreads(clone, NO_THREADS);
1079            t.interrupt();
1080            awaitTermination(t);
1081            sync.release();
1082            assertFalse(sync.isHeldExclusively());
1083            assertTrue(clone.isHeldExclusively());
1084            assertHasExclusiveQueuedThreads(sync, NO_THREADS);
1085            assertHasExclusiveQueuedThreads(clone, NO_THREADS);
1086        }
1087    
1088      /**      /**
1089       * tryReleaseShared setting state changes getState       * tryReleaseShared setting state changes getState
# Line 907  Line 1091 
1091      public void testGetStateWithReleaseShared() {      public void testGetStateWithReleaseShared() {
1092          final BooleanLatch l = new BooleanLatch();          final BooleanLatch l = new BooleanLatch();
1093          assertFalse(l.isSignalled());          assertFalse(l.isSignalled());
1094          l.releaseShared(0);          assertTrue(l.releaseShared(0));
1095          assertTrue(l.isSignalled());          assertTrue(l.isSignalled());
1096      }      }
1097    
# Line 917  Line 1101 
1101      public void testReleaseShared() {      public void testReleaseShared() {
1102          final BooleanLatch l = new BooleanLatch();          final BooleanLatch l = new BooleanLatch();
1103          assertFalse(l.isSignalled());          assertFalse(l.isSignalled());
1104          l.releaseShared(0);          assertTrue(l.releaseShared(0));
1105          assertTrue(l.isSignalled());          assertTrue(l.isSignalled());
1106          l.releaseShared(0);          assertTrue(l.releaseShared(0));
1107          assertTrue(l.isSignalled());          assertTrue(l.isSignalled());
1108      }      }
1109    
1110      /**      /**
1111       * acquireSharedInterruptibly returns after release, but not before       * acquireSharedInterruptibly returns after release, but not before
1112       */       */
1113      public void testAcquireSharedInterruptibly() throws InterruptedException {      public void testAcquireSharedInterruptibly() {
1114          final BooleanLatch l = new BooleanLatch();          final BooleanLatch l = new BooleanLatch();
1115    
1116          Thread t = new Thread(new CheckedRunnable() {          Thread t = newStartedThread(new CheckedRunnable() {
1117              public void realRun() throws InterruptedException {              public void realRun() throws InterruptedException {
1118                  threadAssertFalse(l.isSignalled());                  assertFalse(l.isSignalled());
1119                    l.acquireSharedInterruptibly(0);
1120                    assertTrue(l.isSignalled());
1121                  l.acquireSharedInterruptibly(0);                  l.acquireSharedInterruptibly(0);
1122                  threadAssertTrue(l.isSignalled());                  assertTrue(l.isSignalled());
1123              }});              }});
1124    
1125          t.start();          waitForQueuedThread(l, t);
1126          assertFalse(l.isSignalled());          assertFalse(l.isSignalled());
1127          Thread.sleep(SHORT_DELAY_MS);          assertThreadStaysAlive(t);
1128          l.releaseShared(0);          assertHasSharedQueuedThreads(l, t);
1129            assertTrue(l.releaseShared(0));
1130          assertTrue(l.isSignalled());          assertTrue(l.isSignalled());
1131          t.join();          awaitTermination(t);
1132      }      }
1133    
   
1134      /**      /**
1135       * acquireSharedTimed returns after release       * tryAcquireSharedNanos returns after release, but not before
1136       */       */
1137      public void testAsquireSharedTimed() throws InterruptedException {      public void testTryAcquireSharedNanos() {
1138          final BooleanLatch l = new BooleanLatch();          final BooleanLatch l = new BooleanLatch();
1139    
1140          Thread t = new Thread(new CheckedRunnable() {          Thread t = newStartedThread(new CheckedRunnable() {
1141              public void realRun() throws InterruptedException {              public void realRun() throws InterruptedException {
1142                  threadAssertFalse(l.isSignalled());                  assertFalse(l.isSignalled());
1143                  threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS * 1000L * 1000L));                  long nanos = MILLISECONDS.toNanos(2 * LONG_DELAY_MS);
1144                  threadAssertTrue(l.isSignalled());                  assertTrue(l.tryAcquireSharedNanos(0, nanos));
1145                    assertTrue(l.isSignalled());
1146                    assertTrue(l.tryAcquireSharedNanos(0, nanos));
1147                    assertTrue(l.isSignalled());
1148              }});              }});
1149    
1150          t.start();          waitForQueuedThread(l, t);
1151          assertFalse(l.isSignalled());          assertFalse(l.isSignalled());
1152          Thread.sleep(SHORT_DELAY_MS);          assertThreadStaysAlive(t);
1153          l.releaseShared(0);          assertTrue(l.releaseShared(0));
1154          assertTrue(l.isSignalled());          assertTrue(l.isSignalled());
1155          t.join();          awaitTermination(t);
1156      }      }
1157    
1158      /**      /**
1159       * acquireSharedInterruptibly throws IE if interrupted before released       * acquireSharedInterruptibly is interruptible
1160       */       */
1161      public void testAcquireSharedInterruptibly_InterruptedException() throws InterruptedException {      public void testAcquireSharedInterruptibly_Interruptible() {
1162          final BooleanLatch l = new BooleanLatch();          final BooleanLatch l = new BooleanLatch();
1163          Thread t = new Thread(new CheckedInterruptedRunnable() {          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1164              public void realRun() throws InterruptedException {              public void realRun() throws InterruptedException {
1165                  threadAssertFalse(l.isSignalled());                  assertFalse(l.isSignalled());
1166                  l.acquireSharedInterruptibly(0);                  l.acquireSharedInterruptibly(0);
1167              }});              }});
1168    
1169          t.start();          waitForQueuedThread(l, t);
1170          assertFalse(l.isSignalled());          assertFalse(l.isSignalled());
1171          t.interrupt();          t.interrupt();
1172          t.join();          awaitTermination(t);
1173            assertFalse(l.isSignalled());
1174      }      }
1175    
1176      /**      /**
1177       * acquireSharedTimed throws IE if interrupted before released       * tryAcquireSharedNanos is interruptible
1178       */       */
1179      public void testAcquireSharedNanos_InterruptedException() throws InterruptedException {      public void testTryAcquireSharedNanos_Interruptible() {
1180          final BooleanLatch l = new BooleanLatch();          final BooleanLatch l = new BooleanLatch();
1181          Thread t = new Thread(new CheckedInterruptedRunnable() {          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1182              public void realRun() throws InterruptedException {              public void realRun() throws InterruptedException {
1183                  threadAssertFalse(l.isSignalled());                  assertFalse(l.isSignalled());
1184                  l.tryAcquireSharedNanos(0, SMALL_DELAY_MS * 1000L * 1000L);                  long nanos = MILLISECONDS.toNanos(2 * LONG_DELAY_MS);
1185                    l.tryAcquireSharedNanos(0, nanos);
1186              }});              }});
1187    
1188          t.start();          waitForQueuedThread(l, t);
         Thread.sleep(SHORT_DELAY_MS);  
1189          assertFalse(l.isSignalled());          assertFalse(l.isSignalled());
1190          t.interrupt();          t.interrupt();
1191          t.join();          awaitTermination(t);
1192            assertFalse(l.isSignalled());
1193      }      }
1194    
1195      /**      /**
1196       * acquireSharedTimed times out if not released before timeout       * tryAcquireSharedNanos times out if not released before timeout
1197       */       */
1198      public void testAcquireSharedNanos_Timeout() throws InterruptedException {      public void testTryAcquireSharedNanos_Timeout() {
1199          final BooleanLatch l = new BooleanLatch();          final BooleanLatch l = new BooleanLatch();
1200          Thread t = new Thread(new CheckedRunnable() {          final BooleanLatch observedQueued = new BooleanLatch();
1201            final long timeoutMillis = timeoutMillis();
1202            Thread t = newStartedThread(new CheckedRunnable() {
1203              public void realRun() throws InterruptedException {              public void realRun() throws InterruptedException {
1204                  threadAssertFalse(l.isSignalled());                  assertFalse(l.isSignalled());
1205                  threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS * 1000L * 1000L));                  for (long millis = timeoutMillis();
1206                         !observedQueued.isSignalled();
1207                         millis *= 2) {
1208                        long nanos = MILLISECONDS.toNanos(millis);
1209                        long startTime = System.nanoTime();
1210                        assertFalse(l.tryAcquireSharedNanos(0, nanos));
1211                        assertTrue(millisElapsedSince(startTime) >= millis);
1212                    }
1213                    assertFalse(l.isSignalled());
1214              }});              }});
1215    
1216          t.start();          waitForQueuedThread(l, t);
1217          Thread.sleep(SHORT_DELAY_MS);          observedQueued.releaseShared(0);
1218          assertFalse(l.isSignalled());          assertFalse(l.isSignalled());
1219          t.join();          awaitTermination(t);
1220            assertFalse(l.isSignalled());
1221        }
1222    
1223        /**
1224         * awaitNanos/timed await with 0 wait times out immediately
1225         */
1226        public void testAwait_Zero() throws InterruptedException {
1227            final Mutex sync = new Mutex();
1228            final ConditionObject c = sync.newCondition();
1229            sync.acquire();
1230            assertTrue(c.awaitNanos(0L) <= 0);
1231            assertFalse(c.await(0L, NANOSECONDS));
1232            sync.release();
1233        }
1234    
1235        /**
1236         * awaitNanos/timed await with maximum negative wait times does not underflow
1237         */
1238        public void testAwait_NegativeInfinity() throws InterruptedException {
1239            final Mutex sync = new Mutex();
1240            final ConditionObject c = sync.newCondition();
1241            sync.acquire();
1242            assertTrue(c.awaitNanos(Long.MIN_VALUE) <= 0);
1243            assertFalse(c.await(Long.MIN_VALUE, NANOSECONDS));
1244            sync.release();
1245      }      }
1246    
1247  }  }

Legend:
Removed from v.1.32  
changed lines
  Added in v.1.47

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8