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

Comparing jsr166/src/jsr166e/SequenceLock.java (file contents):
Revision 1.1 by dl, Fri Jul 15 13:14:47 2011 UTC vs.
Revision 1.16 by jsr166, Mon Dec 5 04:50:19 2011 UTC

# Line 6 | Line 6
6  
7   package jsr166e;
8   import java.util.concurrent.TimeUnit;
9 + import java.util.concurrent.TimeoutException;
10   import java.util.concurrent.locks.Lock;
11   import java.util.concurrent.locks.ReentrantLock;
12   import java.util.concurrent.locks.Condition;
# Line 19 | Line 20 | import java.io.IOException;
20   * A reentrant mutual exclusion {@link Lock} in which each lock
21   * acquisition or release advances a sequence number.  When the
22   * sequence number (accessible using {@link #getSequence()}) is odd,
23 < * the lock is held. When it is even (i.e., {@code lock.getSequence()
23 > * the lock is held. When it is even (i.e., ({@code lock.getSequence()
24   * & 1L) == 0L}), the lock is released. Method {@link
25   * #awaitAvailability} can be used to await availability of the lock,
26 < * returning its current sequence number. Sequence numbers are of type
27 < * {@code long} to ensure that they will not wrap around until
28 < * hundreds of years of use under current processor rates.  A
29 < * SequenceLock can be created with a specified number of
30 < * spins. Attempts to lock or await release retry at least the given
31 < * number of times before blocking. If not specified, a default,
32 < * possibly platform-specific, value is used.
26 > * returning its current sequence number. Sequence numbers (as well as
27 > * reentrant hold counts) are of type {@code long} to ensure that they
28 > * will not wrap around until hundreds of years of use under current
29 > * processor rates.  A SequenceLock can be created with a specified
30 > * number of spins. Attempts to acquire the lock in method {@link
31 > * #lock} will retry at least the given number of times before
32 > * blocking. If not specified, a default, possibly platform-specific,
33 > * value is used.
34   *
35   * <p>Except for the lack of support for specified fairness policies,
36 < * a SequenceLock can be used in the same way as {@link
37 < * ReentrantLock}, and has a nearly identical API. SequenceLocks may
38 < * be preferable in contexts in which multiple threads invoke
39 < * read-only methods much more frequently than fully locked methods.
40 < *
36 > * or {@link Condition} objects, a SequenceLock can be used in the
37 > * same way as {@link ReentrantLock}. It provides similar status and
38 > * monitoring methods, such as {@link #isHeldByCurrentThread}.
39 > * SequenceLocks may be preferable in contexts in which multiple
40 > * threads invoke short read-only methods much more frequently than
41 > * fully locked methods.
42 > *
43   * <p> Methods {@code awaitAvailability} and {@code getSequence} can
44   * be used together to define (partially) optimistic read-only methods
45   * that are usually more efficient than ReadWriteLocks when they
46 < * apply.  These read-only methods typically read multiple field
47 < * values into local variables when the lock is not held, retrying if
48 < * the sequence number changed while doing so.  Alternatively, because
49 < * {@code awaitAvailability} accommodates reentrancy, a method can
50 < * retry a bounded number of times before switching to locking mode.
51 < * While conceptually straightforward, expressing these ideas can be
52 < * verbose. For example:
46 > * apply.  These methods should in general be structured as loops that
47 > * await lock availability, then read {@code volatile} fields into
48 > * local variables (and may further read other values derived from
49 > * these, for example the {@code length} of a {@code volatile} array),
50 > * and retry if the sequence number changed while doing so.
51 > * Alternatively, because {@code awaitAvailability} accommodates
52 > * reentrancy, a method can retry a bounded number of times before
53 > * switching to locking mode.  While conceptually straightforward,
54 > * expressing these ideas can be verbose. For example:
55   *
56 < * <pre> {@code
56 > *  <pre> {@code
57   * class Point {
58 < *     private float x, y;
59 < *     private final SequenceLock sl = new SequenceLock();
58 > *   private volatile double x, y;
59 > *   private final SequenceLock sl = new SequenceLock();
60   *
61 < *     void move(float deltaX, float deltaY) { // an excluively locked method
62 < *        sl.lock();
63 < *        try {
64 < *            x += deltaX;
65 < *            y += deltaY;
66 < *        } finally {
67 < *          sl.unlock();
68 < *      }
69 < *  }
61 > *   // an exclusively locked method
62 > *   void move(double deltaX, double deltaY) {
63 > *     sl.lock();
64 > *     try {
65 > *       x += deltaX;
66 > *       y += deltaY;
67 > *     } finally {
68 > *       sl.unlock();
69 > *     }
70 > *   }
71   *
72 < *  float distanceFromOriginV1() { // A read-only method
73 < *      float currentX, currentY;
74 < *      long seq;
75 < *      do {
76 < *          seq = sl.awaitAvailability();
77 < *          currentX = x;
78 < *          currentY = y;
79 < *      } while (sl.getSequence() != seq); // retry if sequence changed
80 < *      return (float)Math.sqrt(currentX * currentX + currentY * currentY);
81 < *  }
72 > *   // A read-only method
73 > *   double distanceFromOriginV1() {
74 > *     double currentX, currentY;
75 > *     long seq;
76 > *     do {
77 > *       seq = sl.awaitAvailability();
78 > *       currentX = x;
79 > *       currentY = y;
80 > *     } while (sl.getSequence() != seq); // retry if sequence changed
81 > *     return Math.sqrt(currentX * currentX + currentY * currentY);
82 > *   }
83   *
84 < *  float distanceFromOriginV2() { // Uses bounded retries before locking
85 < *      float currentX, currentY;
86 < *      long seq;
87 < *      int retries = RETRIES_BEFORE_LOCKING; // for example 8
88 < *      try {
89 < *        do {
90 < *           if (--retries < 0)
91 < *              sl.lock();
92 < *           seq = sl.awaitAvailability();
93 < *           currentX = x;
94 < *           currentY = y;
95 < *        } while (sl.getSequence() != seq);
96 < *      } finally {
97 < *        if (retries < 0)
98 < *           sl.unlock();
99 < *      }
100 < *      return (float)Math.sqrt(currentX * currentX + currentY * currentY);
101 < *  }
102 < *}}</pre>
84 > *   // Uses bounded retries before locking
85 > *   double distanceFromOriginV2() {
86 > *     double currentX, currentY;
87 > *     long seq;
88 > *     int retries = RETRIES_BEFORE_LOCKING; // for example 8
89 > *     try {
90 > *       do {
91 > *         if (--retries < 0)
92 > *           sl.lock();
93 > *         seq = sl.awaitAvailability();
94 > *         currentX = x;
95 > *         currentY = y;
96 > *       } while (sl.getSequence() != seq);
97 > *     } finally {
98 > *       if (retries < 0)
99 > *         sl.unlock();
100 > *     }
101 > *     return Math.sqrt(currentX * currentX + currentY * currentY);
102 > *   }
103 > * }}</pre>
104   *
105   * @since 1.8
106   * @author Doug Lea
# Line 100 | Line 109 | public class SequenceLock implements Loc
109      private static final long serialVersionUID = 7373984872572414699L;
110  
111      static final class Sync extends AbstractQueuedLongSynchronizer {
112 +        static final long serialVersionUID = 2540673546047039555L;
113 +
114          /**
115           * The number of times to spin in lock() and awaitAvailability().
116           */
# Line 108 | Line 119 | public class SequenceLock implements Loc
119          /**
120           * The number of reentrant holds on this lock. Uses a long for
121           * compatibility with other AbstractQueuedLongSynchronizer
122 <         * operations.
122 >         * operations. Accessed only by lock holder.
123           */
124          long holds;
125  
# Line 120 | Line 131 | public class SequenceLock implements Loc
131              return (getState() & 1L) != 0L &&
132                  getExclusiveOwnerThread() == Thread.currentThread();
133          }
134 <        
134 >
135          public final boolean tryAcquire(long acquires) {
136              Thread current = Thread.currentThread();
137              long c = getState();
# Line 137 | Line 148 | public class SequenceLock implements Loc
148              }
149              return false;
150          }
151 <        
151 >
152          public final boolean tryRelease(long releases) {
153              if (Thread.currentThread() != getExclusiveOwnerThread())
154                  throw new IllegalMonitorStateException();
# Line 150 | Line 161 | public class SequenceLock implements Loc
161          }
162  
163          public final long tryAcquireShared(long unused) {
164 <            return ((getState() & 1L) == 0L ||
165 <                    getExclusiveOwnerThread() == Thread.currentThread())?
166 <                1L : -1L; // must return long
164 >            return (((getState() & 1L) == 0L) ? 1L :
165 >                    (getExclusiveOwnerThread() == Thread.currentThread()) ? 0L:
166 >                    -1L);
167          }
168  
169          public final boolean tryReleaseShared(long unused) {
170 <            return true;
170 >            return (getState() & 1L) == 0L;
171          }
172  
173 <        public final Condition newCondition() { return new ConditionObject(); }
173 >        public final Condition newCondition() {
174 >            throw new UnsupportedOperationException();
175 >        }
176  
177          // Other methods in support of SequenceLock
178  
179          final long getSequence() {
180              return getState();
181          }
182 <        
182 >
183          final void lock() {
184              int k = spins;
185 <            while (!tryAcquire(1)) {
185 >            while (!tryAcquire(1L)) {
186                  if (k == 0) {
187 <                    acquire(1);
187 >                    acquire(1L);
188                      break;
189                  }
190                  --k;
191              }
192          }
193 <        
193 >
194          final long awaitAvailability() {
195              long s;
183            int k = spins;
196              while (((s = getState()) & 1L) != 0L &&
197                     getExclusiveOwnerThread() != Thread.currentThread()) {
198 <                if (k > 0)
199 <                    --k;
188 <                else {
189 <                    acquireShared(1);
190 <                    releaseShared(1);
191 <                }
198 >                acquireShared(1L);
199 >                releaseShared(1L);
200              }
201              return s;
202          }
203  
204 +        final long tryAwaitAvailability(long nanos)
205 +            throws InterruptedException, TimeoutException {
206 +            Thread current = Thread.currentThread();
207 +            for (;;) {
208 +                long s = getState();
209 +                if ((s & 1L) == 0L || getExclusiveOwnerThread() == current) {
210 +                    releaseShared(1L);
211 +                    return s;
212 +                }
213 +                if (!tryAcquireSharedNanos(1L, nanos))
214 +                    throw new TimeoutException();
215 +                // since tryAcquireSharedNanos doesn't return seq
216 +                // retry with minimal wait time.
217 +                nanos = 1L;
218 +            }
219 +        }
220 +
221          final boolean isLocked() {
222              return (getState() & 1L) != 0L;
223          }
224  
225          final Thread getOwner() {
226 <            return (getState() & 1L) != 0L ? null : getExclusiveOwnerThread();
226 >            return (getState() & 1L) == 0L ? null : getExclusiveOwnerThread();
227          }
228  
229          final long getHoldCount() {
230 <            return isHeldExclusively()? holds : 0;
230 >            return isHeldExclusively() ? holds : 0;
231          }
232  
233          private void readObject(ObjectInputStream s)
# Line 215 | Line 240 | public class SequenceLock implements Loc
240  
241      private final Sync sync;
242  
243 <    /**
243 >    /**
244       * The default spin value for constructor. Future versions of this
245       * class might choose platform-specific values.  Currently, except
246 <     * on uniprocessors, it is set to a small value that ovecomes near
246 >     * on uniprocessors, it is set to a small value that overcomes near
247       * misses between releases and acquires.
248       */
249 <    static final int DEFAULT_SPINS =
249 >    static final int DEFAULT_SPINS =
250          Runtime.getRuntime().availableProcessors() > 1 ? 64 : 0;
251  
252      /**
253       * Creates an instance of {@code SequenceLock} with the default
254 <     * number of retry attempts to lock or await release before
230 <     * blocking.
254 >     * number of retry attempts to acquire the lock before blocking.
255       */
256      public SequenceLock() { sync = new Sync(DEFAULT_SPINS); }
257  
258      /**
259 <     * Creates an instance of {@code SequenceLock} that
260 <     * will retry attempts to lock or await release
261 <     * at least the given number times before blocking.
259 >     * Creates an instance of {@code SequenceLock} that will retry
260 >     * attempts to acquire the lock at least the given number times
261 >     * before blocking.
262       */
263      public SequenceLock(int spins) { sync = new Sync(spins); }
264  
265      /**
266       * Returns the current sequence number of this lock.  The sequence
267 <     * number is advanced upon each lock or unlock action. When this
268 <     * value is odd, the lock is held; when even, it is released.
267 >     * number is advanced upon each acquire or release action. When
268 >     * this value is odd, the lock is held; when even, it is released.
269       *
270       * @return the current sequence number
271       */
# Line 258 | Line 282 | public class SequenceLock implements Loc
282       * @return the current sequence number
283       */
284      public long awaitAvailability() { return sync.awaitAvailability(); }
285 <    
285 >
286      /**
287 <     * Acquires the lock.
287 >     * Returns the current sequence number if the lock is, or
288 >     * becomes, available within the specified waiting time.
289 >     *
290 >     * <p>If the lock is not available, the current thread becomes
291 >     * disabled for thread scheduling purposes and lies dormant until
292 >     * one of three things happens:
293 >     *
294 >     * <ul>
295 >     *
296 >     * <li>The lock becomes available, in which case the current
297 >     * sequence number is returned.
298 >     *
299 >     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
300 >     * the current thread, in which case this method throws
301 >     * {@link InterruptedException}.
302 >     *
303 >     * <li>The specified waiting time elapses, in which case
304 >     * this method throws {@link TimeoutException}.
305       *
306 <     * <p>Acquires the lock if it is not held by another thread and returns
307 <     * immediately, setting the lock hold count to one.
306 >     * </ul>
307 >     *
308 >     * @param timeout the time to wait for availability
309 >     * @param unit the time unit of the timeout argument
310 >     * @return the current sequence number if the lock is available
311 >     *         upon return from this method
312 >     * @throws InterruptedException if the current thread is interrupted
313 >     * @throws TimeoutException if the lock was not available within
314 >     * the specified waiting time
315 >     * @throws NullPointerException if the time unit is null
316 >     */
317 >    public long tryAwaitAvailability(long timeout, TimeUnit unit)
318 >        throws InterruptedException, TimeoutException {
319 >        return sync.tryAwaitAvailability(unit.toNanos(timeout));
320 >    }
321 >
322 >    /**
323 >     * Acquires the lock.
324       *
325 <     * <p>If the current thread already holds the lock then the hold
326 <     * count is incremented by one and the method returns immediately.
325 >     * <p>If the current thread already holds this lock then the hold count
326 >     * is incremented by one and the method returns immediately without
327 >     * incrementing the sequence number.
328       *
329 <     * <p>If the lock is held by another thread then the
330 <     * current thread becomes disabled for thread scheduling
331 <     * purposes and lies dormant until the lock has been acquired,
332 <     * at which time the lock hold count is set to one.
329 >     * <p>If this lock not held by another thread, this method
330 >     * increments the sequence number (which thus becomes an odd
331 >     * number), sets the lock hold count to one, and returns
332 >     * immediately.
333 >     *
334 >     * <p>If the lock is held by another thread then the current
335 >     * thread may retry acquiring this lock, depending on the {@code
336 >     * spin} count established in constructor.  If the lock is still
337 >     * not acquired, the current thread becomes disabled for thread
338 >     * scheduling purposes and lies dormant until enabled by
339 >     * some other thread releasing the lock.
340       */
341      public void lock() { sync.lock(); }
342  
# Line 279 | Line 344 | public class SequenceLock implements Loc
344       * Acquires the lock unless the current thread is
345       * {@linkplain Thread#interrupt interrupted}.
346       *
282     * <p>Acquires the lock if it is not held by another thread and returns
283     * immediately, setting the lock hold count to one.
284     *
347       * <p>If the current thread already holds this lock then the hold count
348 <     * is incremented by one and the method returns immediately.
348 >     * is incremented by one and the method returns immediately without
349 >     * incrementing the sequence number.
350       *
351 <     * <p>If the lock is held by another thread then the
352 <     * current thread becomes disabled for thread scheduling
353 <     * purposes and lies dormant until one of two things happens:
351 >     * <p>If this lock not held by another thread, this method
352 >     * increments the sequence number (which thus becomes an odd
353 >     * number), sets the lock hold count to one, and returns
354 >     * immediately.
355 >     *
356 >     * <p>If the lock is held by another thread then the current
357 >     * thread may retry acquiring this lock, depending on the {@code
358 >     * spin} count established in constructor.  If the lock is still
359 >     * not acquired, the current thread becomes disabled for thread
360 >     * scheduling purposes and lies dormant until one of two things
361 >     * happens:
362       *
363       * <ul>
364       *
# Line 299 | Line 370 | public class SequenceLock implements Loc
370       * </ul>
371       *
372       * <p>If the lock is acquired by the current thread then the lock hold
373 <     * count is set to one.
373 >     * count is set to one and the sequence number is incremented.
374       *
375       * <p>If the current thread:
376       *
# Line 322 | Line 393 | public class SequenceLock implements Loc
393       * @throws InterruptedException if the current thread is interrupted
394       */
395      public void lockInterruptibly() throws InterruptedException {
396 <        sync.acquireInterruptibly(1);
396 >        sync.acquireInterruptibly(1L);
397      }
398  
399      /**
400       * Acquires the lock only if it is not held by another thread at the time
401       * of invocation.
402       *
403 <     * <p>Acquires the lock if it is not held by another thread and
404 <     * returns immediately with the value {@code true}, setting the
405 <     * lock hold count to one.
406 <     *
407 <     * <p> If the current thread already holds this lock then the hold
408 <     * count is incremented by one and the method returns {@code true}.
403 >     * <p>If the current thread already holds this lock then the hold
404 >     * count is incremented by one and the method returns {@code true}
405 >     * without incrementing the sequence number.
406 >     *
407 >     * <p>If this lock not held by another thread, this method
408 >     * increments the sequence number (which thus becomes an odd
409 >     * number), sets the lock hold count to one, and returns {@code
410 >     * true}.
411       *
412 <     * <p>If the lock is held by another thread then this method will return
413 <     * immediately with the value {@code false}.
412 >     * <p>If the lock is held by another thread then this method
413 >     * returns {@code false}.
414       *
415       * @return {@code true} if the lock was free and was acquired by the
416       *         current thread, or the lock was already held by the current
417       *         thread; and {@code false} otherwise
418       */
419 <    public boolean tryLock() { return sync.tryAcquire(1); }
419 >    public boolean tryLock() { return sync.tryAcquire(1L); }
420  
421      /**
422       * Acquires the lock if it is not held by another thread within the given
423       * waiting time and the current thread has not been
424       * {@linkplain Thread#interrupt interrupted}.
425       *
426 <     * <p>Acquires the lock if it is not held by another thread and returns
427 <     * immediately with the value {@code true}, setting the lock hold count
428 <     * to one. If this lock has been set to use a fair ordering policy then
429 <     * an available lock <em>will not</em> be acquired if any other threads
430 <     * are waiting for the lock. This is in contrast to the {@link #tryLock()}
431 <     * method. If you want a timed {@code tryLock} that does permit barging on
432 <     * a fair lock then combine the timed and un-timed forms together:
433 <     *
434 <     *  <pre> {@code
435 <     * if (lock.tryLock() ||
436 <     *     lock.tryLock(timeout, unit)) {
437 <     *   ...
438 <     * }}</pre>
439 <     *
440 <     * <p>If the current thread
368 <     * already holds this lock then the hold count is incremented by one and
369 <     * the method returns {@code true}.
370 <     *
371 <     * <p>If the lock is held by another thread then the
372 <     * current thread becomes disabled for thread scheduling
373 <     * purposes and lies dormant until one of three things happens:
426 >     * <p>If the current thread already holds this lock then the hold count
427 >     * is incremented by one and the method returns immediately without
428 >     * incrementing the sequence number.
429 >     *
430 >     * <p>If this lock not held by another thread, this method
431 >     * increments the sequence number (which thus becomes an odd
432 >     * number), sets the lock hold count to one, and returns
433 >     * immediately.
434 >     *
435 >     * <p>If the lock is held by another thread then the current
436 >     * thread may retry acquiring this lock, depending on the {@code
437 >     * spin} count established in constructor.  If the lock is still
438 >     * not acquired, the current thread becomes disabled for thread
439 >     * scheduling purposes and lies dormant until one of three things
440 >     * happens:
441       *
442       * <ul>
443       *
# Line 420 | Line 487 | public class SequenceLock implements Loc
487       */
488      public boolean tryLock(long timeout, TimeUnit unit)
489          throws InterruptedException {
490 <        return sync.tryAcquireNanos(1, unit.toNanos(timeout));
490 >        return sync.tryAcquireNanos(1L, unit.toNanos(timeout));
491      }
492  
493      /**
494       * Attempts to release this lock.
495       *
496 <     * <p>If the current thread is the holder of this lock then the hold
497 <     * count is decremented.  If the hold count is now zero then the lock
498 <     * is released.  If the current thread is not the holder of this
499 <     * lock then {@link IllegalMonitorStateException} is thrown.
496 >     * <p>If the current thread is the holder of this lock then the
497 >     * hold count is decremented.  If the hold count is now zero then
498 >     * the sequence number is incremented (thus becoming an even
499 >     * number) and the lock is released.  If the current thread is not
500 >     * the holder of this lock then {@link
501 >     * IllegalMonitorStateException} is thrown.
502       *
503       * @throws IllegalMonitorStateException if the current thread does not
504       *         hold this lock
# Line 437 | Line 506 | public class SequenceLock implements Loc
506      public void unlock()              { sync.release(1); }
507  
508      /**
509 <     * Returns a {@link Condition} instance for use with this
510 <     * {@link Lock} instance.
442 <     *
443 <     * <p>The returned {@link Condition} instance supports the same
444 <     * usages as do the {@link Object} monitor methods ({@link
445 <     * Object#wait() wait}, {@link Object#notify notify}, and {@link
446 <     * Object#notifyAll notifyAll}) when used with the built-in
447 <     * monitor lock.
448 <     *
449 <     * <ul>
450 <     *
451 <     * <li>If this lock is not held when any of the {@link Condition}
452 <     * {@linkplain Condition#await() waiting} or {@linkplain
453 <     * Condition#signal signalling} methods are called, then an {@link
454 <     * IllegalMonitorStateException} is thrown.
455 <     *
456 <     * <li>When the condition {@linkplain Condition#await() waiting}
457 <     * methods are called the lock is released and, before they
458 <     * return, the lock is reacquired and the lock hold count restored
459 <     * to what it was when the method was called.
460 <     *
461 <     * <li>If a thread is {@linkplain Thread#interrupt interrupted}
462 <     * while waiting then the wait will terminate, an {@link
463 <     * InterruptedException} will be thrown, and the thread's
464 <     * interrupted status will be cleared.
465 <     *
466 <     * <li> Waiting threads are signalled in FIFO order.
467 <     *
468 <     * <li>The ordering of lock reacquisition for threads returning
469 <     * from waiting methods is the same as for threads initially
470 <     * acquiring the lock.
471 <     * </ul>
509 >     * Throws UnsupportedOperationException. SequenceLocks
510 >     * do not support Condition objects.
511       *
512 <     * @return the Condition object
512 >     * @throws UnsupportedOperationException
513       */
514 <    public Condition newCondition()   { return sync.newCondition(); }
514 >    public Condition newCondition()   {
515 >        throw new UnsupportedOperationException();
516 >    }
517  
518      /**
519       * Queries the number of holds on this lock by the current thread.
# Line 481 | Line 522 | public class SequenceLock implements Loc
522       * matched by an unlock action.
523       *
524       * <p>The hold count information is typically only used for testing and
525 <     * debugging purposes.
525 >     * debugging purposes.
526       *
527       * @return the number of holds on this lock by the current thread,
528       *         or zero if this lock is not held by the current thread
529       */
530 <    public long getHoldCount() { return sync.getHoldCount();  }
530 >    public long getHoldCount() { return sync.getHoldCount(); }
531  
532      /**
533       * Queries if this lock is held by the current thread.
# Line 580 | Line 621 | public class SequenceLock implements Loc
621      }
622  
623      /**
583     * Queries whether any threads are waiting on the given condition
584     * associated with this lock. Note that because timeouts and
585     * interrupts may occur at any time, a {@code true} return does
586     * not guarantee that a future {@code signal} will awaken any
587     * threads.  This method is designed primarily for use in
588     * monitoring of the system state.
589     *
590     * @param condition the condition
591     * @return {@code true} if there are any waiting threads
592     * @throws IllegalMonitorStateException if this lock is not held
593     * @throws IllegalArgumentException if the given condition is
594     *         not associated with this lock
595     * @throws NullPointerException if the condition is null
596     */
597    public boolean hasWaiters(Condition condition) {
598        if (condition == null)
599            throw new NullPointerException();
600        if (!(condition instanceof AbstractQueuedLongSynchronizer.ConditionObject))
601            throw new IllegalArgumentException("not owner");
602        return sync.hasWaiters((AbstractQueuedLongSynchronizer.ConditionObject)condition);
603    }
604
605    /**
606     * Returns an estimate of the number of threads waiting on the
607     * given condition associated with this lock. Note that because
608     * timeouts and interrupts may occur at any time, the estimate
609     * serves only as an upper bound on the actual number of waiters.
610     * This method is designed for use in monitoring of the system
611     * state, not for synchronization control.
612     *
613     * @param condition the condition
614     * @return the estimated number of waiting threads
615     * @throws IllegalMonitorStateException if this lock is not held
616     * @throws IllegalArgumentException if the given condition is
617     *         not associated with this lock
618     * @throws NullPointerException if the condition is null
619     */
620    public int getWaitQueueLength(Condition condition) {
621        if (condition == null)
622            throw new NullPointerException();
623        if (!(condition instanceof AbstractQueuedLongSynchronizer.ConditionObject))
624            throw new IllegalArgumentException("not owner");
625        return sync.getWaitQueueLength((AbstractQueuedLongSynchronizer.ConditionObject)condition);
626    }
627
628    /**
629     * Returns a collection containing those threads that may be
630     * waiting on the given condition associated with this lock.
631     * Because the actual set of threads may change dynamically while
632     * constructing this result, the returned collection is only a
633     * best-effort estimate. The elements of the returned collection
634     * are in no particular order.  This method is designed to
635     * facilitate construction of subclasses that provide more
636     * extensive condition monitoring facilities.
637     *
638     * @param condition the condition
639     * @return the collection of threads
640     * @throws IllegalMonitorStateException if this lock is not held
641     * @throws IllegalArgumentException if the given condition is
642     *         not associated with this lock
643     * @throws NullPointerException if the condition is null
644     */
645    protected Collection<Thread> getWaitingThreads(Condition condition) {
646        if (condition == null)
647            throw new NullPointerException();
648        if (!(condition instanceof AbstractQueuedLongSynchronizer.ConditionObject))
649            throw new IllegalArgumentException("not owner");
650        return sync.getWaitingThreads((AbstractQueuedLongSynchronizer.ConditionObject)condition);
651    }
652
653    /**
624       * Returns a string identifying this lock, as well as its lock state.
625       * The state, in brackets, includes either the String {@code "Unlocked"}
626       * or the String {@code "Locked by"} followed by the
# Line 666 | Line 636 | public class SequenceLock implements Loc
636      }
637  
638   }
639 <            
639 >

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines