Share via


Semaphore.TryAcquire Method

Definition

Overloads

TryAcquire()

Acquires a permit from this semaphore, only if one is available at the time of invocation.

TryAcquire(Int32)

Acquires the given number of permits from this semaphore, only if all are available at the time of invocation.

TryAcquire(Int64, TimeUnit)

Acquires a permit from this semaphore, if one becomes available within the given waiting time and the current thread has not been Thread#interrupt interrupted.

TryAcquire(Int32, Int64, TimeUnit)

Acquires the given number of permits from this semaphore, if all become available within the given waiting time and the current thread has not been Thread#interrupt interrupted.

TryAcquire()

Acquires a permit from this semaphore, only if one is available at the time of invocation.

[Android.Runtime.Register("tryAcquire", "()Z", "GetTryAcquireHandler")]
public virtual bool TryAcquire ();
[<Android.Runtime.Register("tryAcquire", "()Z", "GetTryAcquireHandler")>]
abstract member TryAcquire : unit -> bool
override this.TryAcquire : unit -> bool

Returns

true if a permit was acquired and false otherwise

Attributes

Remarks

Acquires a permit from this semaphore, only if one is available at the time of invocation.

Acquires a permit, if one is available and returns immediately, with the value true, reducing the number of available permits by one.

If no permit is available then this method will return immediately with the value false.

Even when this semaphore has been set to use a fair ordering policy, a call to tryAcquire()<em>will</em> immediately acquire a permit if one is available, whether or not other threads are currently waiting. This &quot;barging&quot; behavior can be useful in certain circumstances, even though it breaks fairness. If you want to honor the fairness setting, then use #tryAcquire(long, TimeUnit) tryAcquire(0, TimeUnit.SECONDS) which is almost equivalent (it also detects interruption).

Java documentation for java.util.concurrent.Semaphore.tryAcquire().

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

TryAcquire(Int32)

Acquires the given number of permits from this semaphore, only if all are available at the time of invocation.

[Android.Runtime.Register("tryAcquire", "(I)Z", "GetTryAcquire_IHandler")]
public virtual bool TryAcquire (int permits);
[<Android.Runtime.Register("tryAcquire", "(I)Z", "GetTryAcquire_IHandler")>]
abstract member TryAcquire : int -> bool
override this.TryAcquire : int -> bool

Parameters

permits
Int32

the number of permits to acquire

Returns

true if the permits were acquired and false otherwise

Attributes

Exceptions

if permits is negative

Remarks

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<em>will</em> immediately acquire a permit if one is available, whether or not other threads are currently waiting. This &quot;barging&quot; behavior can be useful in certain circumstances, even though it breaks fairness. If you want to honor the fairness setting, then use #tryAcquire(int, long, TimeUnit) tryAcquire(permits, 0, TimeUnit.SECONDS) which is almost equivalent (it also detects interruption).

Java documentation for java.util.concurrent.Semaphore.tryAcquire(int).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

TryAcquire(Int64, TimeUnit)

Acquires a permit from this semaphore, if one becomes available within the given waiting time and the current thread has not been Thread#interrupt interrupted.

[Android.Runtime.Register("tryAcquire", "(JLjava/util/concurrent/TimeUnit;)Z", "GetTryAcquire_JLjava_util_concurrent_TimeUnit_Handler")]
public virtual bool TryAcquire (long timeout, Java.Util.Concurrent.TimeUnit? unit);
[<Android.Runtime.Register("tryAcquire", "(JLjava/util/concurrent/TimeUnit;)Z", "GetTryAcquire_JLjava_util_concurrent_TimeUnit_Handler")>]
abstract member TryAcquire : int64 * Java.Util.Concurrent.TimeUnit -> bool
override this.TryAcquire : int64 * Java.Util.Concurrent.TimeUnit -> bool

Parameters

timeout
Int64

the maximum time to wait for a permit

unit
TimeUnit

the time unit of the timeout argument

Returns

true if a permit was acquired and false if the waiting time elapsed before a permit was acquired

Attributes

Exceptions

if the current thread is interrupted

Remarks

Acquires a permit from this semaphore, if one becomes available within the given waiting time and the current thread has not been Thread#interrupt interrupted.

Acquires a permit, if one is available and returns immediately, with the value true, reducing the number of available permits by one.

If no permit is available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happens: <ul> <li>Some other thread invokes the #release method for this semaphore and the current thread is next to be assigned a permit; or <li>Some other thread Thread#interrupt interrupts the current thread; or <li>The specified waiting time elapses. </ul>

If a permit is acquired then the value true is returned.

If the current thread: <ul> <li>has its interrupted status set on entry to this method; or <li>is Thread#interrupt interrupted while waiting to acquire a permit, </ul> then InterruptedException is thrown and the current thread's interrupted status is cleared.

If the specified waiting time elapses then the value false is returned. If the time is less than or equal to zero, the method will not wait at all.

Java documentation for java.util.concurrent.Semaphore.tryAcquire(long, java.util.concurrent.TimeUnit).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

TryAcquire(Int32, Int64, TimeUnit)

Acquires the given number of permits from this semaphore, if all become available within the given waiting time and the current thread has not been Thread#interrupt interrupted.

[Android.Runtime.Register("tryAcquire", "(IJLjava/util/concurrent/TimeUnit;)Z", "GetTryAcquire_IJLjava_util_concurrent_TimeUnit_Handler")]
public virtual bool TryAcquire (int permits, long timeout, Java.Util.Concurrent.TimeUnit? unit);
[<Android.Runtime.Register("tryAcquire", "(IJLjava/util/concurrent/TimeUnit;)Z", "GetTryAcquire_IJLjava_util_concurrent_TimeUnit_Handler")>]
abstract member TryAcquire : int * int64 * Java.Util.Concurrent.TimeUnit -> bool
override this.TryAcquire : int * int64 * Java.Util.Concurrent.TimeUnit -> bool

Parameters

permits
Int32

the number of permits to acquire

timeout
Int64

the maximum time to wait for the permits

unit
TimeUnit

the time unit of the timeout argument

Returns

true if all permits were acquired and false if the waiting time elapsed before all permits were acquired

Attributes

Exceptions

if the current thread is interrupted

if permits is negative

Remarks

Acquires the given number of permits from this semaphore, if all become available within the given waiting time and the current thread has not been Thread#interrupt interrupted.

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 the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happens: <ul> <li>Some other thread invokes one of the #release() release methods for this semaphore and the current thread is next to be assigned permits and the number of available permits satisfies this request; or <li>Some other thread Thread#interrupt interrupts the current thread; or <li>The specified waiting time elapses. </ul>

If the permits are acquired then the value true is returned.

If the current thread: <ul> <li>has its interrupted status set on entry to this method; or <li>is Thread#interrupt interrupted while waiting to acquire the permits, </ul> then InterruptedException is thrown and the current thread's interrupted status is cleared. Any permits that were to be assigned to this thread, are instead assigned to other threads trying to acquire permits, as if the permits had been made available by a call to #release().

If the specified waiting time elapses then the value false is returned. If the time is less than or equal to zero, the method will not wait at all. Any permits that were to be assigned to this thread, are instead assigned to other threads trying to acquire permits, as if the permits had been made available by a call to #release().

Java documentation for java.util.concurrent.Semaphore.tryAcquire(int, long, java.util.concurrent.TimeUnit).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to