ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Semaphore.java
(Generate patch)

Comparing jsr166/src/main/java/util/concurrent/Semaphore.java (file contents):
Revision 1.44 by dl, Tue Mar 1 01:27:46 2005 UTC vs.
Revision 1.45 by jsr166, Tue Apr 26 01:17:18 2005 UTC

# Line 103 | Line 103 | import java.util.concurrent.atomic.*;
103   * methods.  So, it is possible for one thread to invoke
104   * <tt>acquire</tt> before another, but reach the ordering point after
105   * the other, and similarly upon return from the method.
106 < * Also note that the untimed {@link #tryAcquire() tryAcquire} methods do not
106 > * Also note that the untimed {@link #tryAcquire() tryAcquire} methods do not
107   * honor the fairness setting, but will take any permits that are
108   * available.
109   *
# Line 137 | Line 137 | public class Semaphore implements java.i
137          Sync(int permits) {
138              setState(permits);
139          }
140 <        
140 >
141          final int getPermits() {
142              return getState();
143          }
# Line 151 | Line 151 | public class Semaphore implements java.i
151                      return remaining;
152              }
153          }
154 <        
154 >
155          protected final boolean tryReleaseShared(int releases) {
156              for (;;) {
157                  int p = getState();
158 <                if (compareAndSetState(p, p + releases))
158 >                if (compareAndSetState(p, p + releases))
159                      return true;
160              }
161          }
# Line 185 | Line 185 | public class Semaphore implements java.i
185          NonfairSync(int permits) {
186              super(permits);
187          }
188 <      
188 >
189          protected int tryAcquireShared(int acquires) {
190              return nonfairTryAcquireShared(acquires);
191          }
# Line 198 | Line 198 | public class Semaphore implements java.i
198          FairSync(int permits) {
199              super(permits);
200          }
201 <        
201 >
202          protected int tryAcquireShared(int acquires) {
203              Thread current = Thread.currentThread();
204              for (;;) {
# Line 221 | Line 221 | public class Semaphore implements java.i
221       * value may be negative, in which case releases must
222       * occur before any acquires will be granted.
223       */
224 <    public Semaphore(int permits) {
224 >    public Semaphore(int permits) {
225          sync = new NonfairSync(permits);
226      }
227  
# Line 234 | Line 234 | public class Semaphore implements java.i
234       * @param fair true if this semaphore will guarantee first-in
235       * first-out granting of permits under contention, else false.
236       */
237 <    public Semaphore(int permits, boolean fair) {
237 >    public Semaphore(int permits, boolean fair) {
238          sync = (fair)? new FairSync(permits) : new NonfairSync(permits);
239      }
240  
# Line 295 | Line 295 | public class Semaphore implements java.i
295      }
296  
297      /**
298 <     * Acquires a permit from this semaphore, only if one is available at the
298 >     * Acquires a permit from this semaphore, only if one is available at the
299       * time of invocation.
300       * <p>Acquires a permit, if one is available and returns immediately,
301       * with the value <tt>true</tt>,
# Line 307 | Line 307 | public class Semaphore implements java.i
307       * <p>Even when this semaphore has been set to use a
308       * fair ordering policy, a call to <tt>tryAcquire()</tt> <em>will</em>
309       * immediately acquire a permit if one is available, whether or not
310 <     * other threads are currently waiting.
311 <     * This &quot;barging&quot; behavior can be useful in certain
310 >     * other threads are currently waiting.
311 >     * This &quot;barging&quot; behavior can be useful in certain
312       * circumstances, even though it breaks fairness. If you want to honor
313 <     * the fairness setting, then use
313 >     * the fairness setting, then use
314       * {@link #tryAcquire(long, TimeUnit) tryAcquire(0, TimeUnit.SECONDS) }
315       * which is almost equivalent (it also detects interruption).
316       *
# Line 322 | Line 322 | public class Semaphore implements java.i
322      }
323  
324      /**
325 <     * Acquires a permit from this semaphore, if one becomes available
325 >     * Acquires a permit from this semaphore, if one becomes available
326       * within the given waiting time and the
327       * current thread has not been {@link Thread#interrupt interrupted}.
328       * <p>Acquires a permit, if one is available and returns immediately,
# Line 349 | Line 349 | public class Semaphore implements java.i
349       * interrupted status is cleared.
350       * <p>If the specified waiting time elapses then the value <tt>false</tt>
351       * is returned.
352 <     * If the time is less than or equal to zero, the method will not wait
352 >     * If the time is less than or equal to zero, the method will not wait
353       * at all.
354       *
355       * @param timeout the maximum time to wait for a permit
# Line 362 | Line 362 | public class Semaphore implements java.i
362       * @see Thread#interrupt
363       *
364       */
365 <    public boolean tryAcquire(long timeout, TimeUnit unit)
365 >    public boolean tryAcquire(long timeout, TimeUnit unit)
366          throws InterruptedException {
367          return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout));
368      }
# Line 382 | Line 382 | public class Semaphore implements java.i
382      public void release() {
383          sync.releaseShared(1);
384      }
385 <      
385 >
386      /**
387 <     * Acquires the given number of permits from this semaphore,
388 <     * blocking until all are available,
387 >     * Acquires the given number of permits from this semaphore,
388 >     * blocking until all are available,
389       * or the thread is {@link Thread#interrupt interrupted}.
390       *
391       * <p>Acquires the given number of permits, if they are available,
# Line 396 | Line 396 | public class Semaphore implements java.i
396       * disabled for thread scheduling purposes and lies dormant until
397       * one of two things happens:
398       * <ul>
399 <     * <li>Some other thread invokes one of the {@link #release() release}
399 >     * <li>Some other thread invokes one of the {@link #release() release}
400       * methods for this semaphore, the current thread is next to be assigned
401       * permits and the number of available permits satisfies this request; or
402       * <li>Some other thread {@link Thread#interrupt interrupts} the current
# Line 410 | Line 410 | public class Semaphore implements java.i
410       * for a permit,
411       * </ul>
412       * then {@link InterruptedException} is thrown and the current thread's
413 <     * interrupted status is cleared.
414 <     * Any permits that were to be assigned to this thread are instead
413 >     * interrupted status is cleared.
414 >     * Any permits that were to be assigned to this thread are instead
415       * assigned to other threads trying to acquire permits, as if
416       * permits had been made available by a call to {@link #release()}.
417       *
# Line 428 | Line 428 | public class Semaphore implements java.i
428      }
429  
430      /**
431 <     * Acquires the given number of permits from this semaphore,
431 >     * Acquires the given number of permits from this semaphore,
432       * blocking until all are available.
433       *
434       * <p>Acquires the given number of permits, if they are available,
# Line 437 | Line 437 | public class Semaphore implements java.i
437       *
438       * <p>If insufficient permits are available then the current thread becomes
439       * disabled for thread scheduling purposes and lies dormant until
440 <     * some other thread invokes one of the {@link #release() release}
440 >     * some other thread invokes one of the {@link #release() release}
441       * methods for this semaphore, the current thread is next to be assigned
442       * permits and the number of available permits satisfies this request.
443       *
# Line 460 | Line 460 | public class Semaphore implements java.i
460       * Acquires the given number of permits from this semaphore, only
461       * if all are available at the time of invocation.
462       *
463 <     * <p>Acquires the given number of permits, if they are available, and
463 >     * <p>Acquires the given number of permits, if they are available, and
464       * returns immediately, with the value <tt>true</tt>,
465       * reducing the number of available permits by the given amount.
466       *
# Line 490 | Line 490 | public class Semaphore implements java.i
490      }
491  
492      /**
493 <     * Acquires the given number of permits from this semaphore, if all
493 >     * Acquires the given number of permits from this semaphore, if all
494       * become available within the given waiting time and the
495       * current thread has not been {@link Thread#interrupt interrupted}.
496 <     * <p>Acquires the given number of permits, if they are available and
496 >     * <p>Acquires the given number of permits, if they are available and
497       * returns immediately, with the value <tt>true</tt>,
498       * reducing the number of available permits by the given amount.
499       * <p>If insufficient permits are available then
500       * the current thread becomes disabled for thread scheduling
501       * purposes and lies dormant until one of three things happens:
502       * <ul>
503 <     * <li>Some other thread invokes one of the {@link #release() release}
503 >     * <li>Some other thread invokes one of the {@link #release() release}
504       * methods for this semaphore, the current thread is next to be assigned
505       * permits and the number of available permits satisfies this request; or
506       * <li>Some other thread {@link Thread#interrupt interrupts} the current
# Line 516 | Line 516 | public class Semaphore implements java.i
516       * </ul>
517       * then {@link InterruptedException} is thrown and the current thread's
518       * interrupted status is cleared.
519 <     * Any permits that were to be assigned to this thread, are instead
519 >     * Any permits that were to be assigned to this thread, are instead
520       * assigned to other threads trying to acquire permits, as if
521       * the permits had been made available by a call to {@link #release()}.
522       *
# Line 524 | Line 524 | public class Semaphore implements java.i
524       * is returned.
525       * If the time is
526       * less than or equal to zero, the method will not wait at all.
527 <     * Any permits that were to be assigned to this thread, are instead
527 >     * Any permits that were to be assigned to this thread, are instead
528       * assigned to other threads trying to acquire permits, as if
529       * the permits had been made available by a call to {@link #release()}.
530       *
# Line 548 | Line 548 | public class Semaphore implements java.i
548  
549      /**
550       * Releases the given number of permits, returning them to the semaphore.
551 <     * <p>Releases the given number of permits, increasing the number of
551 >     * <p>Releases the given number of permits, increasing the number of
552       * available permits by that amount.
553       * If any threads are trying to acquire permits, then one
554       * is selected and given the permits that were just released.
# Line 582 | Line 582 | public class Semaphore implements java.i
582      }
583  
584      /**
585 <     * Acquire and return all permits that are immediately available.
586 <     * @return the number of permits
585 >     * Acquires and returns all permits that are immediately available.
586 >     * @return the number of permits
587       */
588      public int drainPermits() {
589          return sync.drainPermits();
# Line 621 | Line 621 | public class Semaphore implements java.i
621       * @return true if there may be other threads waiting to acquire
622       * the lock.
623       */
624 <    public final boolean hasQueuedThreads() {
624 >    public final boolean hasQueuedThreads() {
625          return sync.hasQueuedThreads();
626      }
627  
# Line 654 | Line 654 | public class Semaphore implements java.i
654  
655      /**
656       * Returns a string identifying this semaphore, as well as its state.
657 <     * The state, in brackets, includes the String
657 >     * The state, in brackets, includes the String
658       * &quot;Permits =&quot; followed by the number of permits.
659       * @return a string identifying this semaphore, as well as its
660       * state

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines