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

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

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

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

Legend:
Removed from v.1.23  
changed lines
  Added in v.1.24

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8