Acquires the given number of permits from this semaphore, only if all are available at the time of invocation.
Acquires the given number of permits, if they are available, and
returns immediately, with the value true
,
reducing the number of available permits by the given amount.
If insufficient permits are available then this method will return
immediately with the value false
and the number of available
permits is unchanged.
Even when this semaphore has been set to use a fair ordering
policy, a call to tryAcquire
will
immediately acquire a permit if one is available, whether or
not other threads are currently waiting. This
"barging" behavior can be useful in certain
circumstances, even though it breaks fairness. If you want to
honor the fairness setting, then use tryAcquire(permits, 0, TimeUnit.SECONDS)
which is almost equivalent (it also detects interruption).
permits | the number of permits to acquire |
true
if the permits were acquired and
false
otherwise
IllegalArgumentException | if permits is negative |
Diagram: Phaser, Semaphore