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.26 by dl, Mon Dec 29 14:19:17 2003 UTC vs.
Revision 1.27 by dl, Mon Dec 29 16:38:06 2003 UTC

# Line 247 | Line 247 | public class Semaphore implements java.i
247       * <p>If no permit is available then this method will return
248       * immediately with the value <tt>false</tt>.
249       *
250 +     * <p>Even when this semaphore has been set to use a
251 +     * fair ordering policy, a call to <tt>tryAcquire()</tt> <em>will</em>
252 +     * immediately acquire a permit if one is available, whether or not
253 +     * other threads are currently waiting.
254 +     * This &quot;barging&quot; behavior can be useful in certain
255 +     * circumstances, even though it breaks fairness. If you want to honor
256 +     * the fairness setting, then use
257 +     * {@link #tryAcquire(long, TimeUnit) tryAcquire(0, TimeUnit.SECONDS) }
258 +     * which is almost equivalent (it also detects interruption).
259 +     *
260       * @return <tt>true</tt> if a permit was acquired and <tt>false</tt>
261       * otherwise.
262       */
263      public boolean tryAcquire() {
264 <        return sync.acquireSharedState(false, 1) >= 0;
264 >        return sync.acquireSharedState(true, 1) >= 0;
265      }
266  
267      /**
# Line 400 | Line 410 | public class Semaphore implements java.i
410       * immediately with the value <tt>false</tt> and the number of available
411       * permits is unchanged.
412       *
413 +     * <p>Even when this semaphore has been set to use a fair ordering
414 +     * policy, a call to <tt>tryAcquire</tt> <em>will</em>
415 +     * immediately acquire a permit if one is available, whether or
416 +     * not other threads are currently waiting.  This
417 +     * &quot;barging&quot; behavior can be useful in certain
418 +     * circumstances, even though it breaks fairness. If you want to
419 +     * honor the fairness setting, then use {@link #tryAcquire(int,
420 +     * long, TimeUnit) tryAcquire(permits, 0, TimeUnit.SECONDS) }
421 +     * which is almost equivalent (it also detects interruption).
422 +     *
423       * @param permits the number of permits to acquire
424       *
425       * @return <tt>true</tt> if the permits were acquired and <tt>false</tt>
# Line 408 | Line 428 | public class Semaphore implements java.i
428       */
429      public boolean tryAcquire(int permits) {
430          if (permits < 0) throw new IllegalArgumentException();
431 <        return sync.acquireSharedState(false, permits) >= 0;
431 >        return sync.acquireSharedState(true, permits) >= 0;
432      }
433  
434      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines