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 "barging" 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 |
|
/** |
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 |
+ |
* "barging" 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> |
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 |
|
/** |