Freigeben über


Semaphore.Acquire Method

Definition

Overloads

Acquire()

Acquires a permit from this semaphore, blocking until one is available, or the thread is Thread#interrupt interrupted.

Acquire(Int32)

Acquires the given number of permits from this semaphore, blocking until all are available, or the thread is Thread#interrupt interrupted.

Acquire()

Acquires a permit from this semaphore, blocking until one is available, or the thread is Thread#interrupt interrupted.

[Android.Runtime.Register("acquire", "()V", "GetAcquireHandler")]
public virtual void Acquire ();
[<Android.Runtime.Register("acquire", "()V", "GetAcquireHandler")>]
abstract member Acquire : unit -> unit
override this.Acquire : unit -> unit
Attributes

Exceptions

if the current thread is interrupted

Remarks

Acquires a permit from this semaphore, blocking until one is available, or the thread is Thread#interrupt interrupted.

Acquires a permit, if one is available and returns immediately, 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 two 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. </ul>

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

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

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

Acquire(Int32)

Acquires the given number of permits from this semaphore, blocking until all are available, or the thread is Thread#interrupt interrupted.

[Android.Runtime.Register("acquire", "(I)V", "GetAcquire_IHandler")]
public virtual void Acquire (int permits);
[<Android.Runtime.Register("acquire", "(I)V", "GetAcquire_IHandler")>]
abstract member Acquire : int -> unit
override this.Acquire : int -> unit

Parameters

permits
Int32

the number of permits to acquire

Attributes

Exceptions

if the current thread is interrupted

if permits is negative

Remarks

Acquires the given number of permits from this semaphore, blocking until all are available, or the thread is Thread#interrupt interrupted.

Acquires the given number of permits, if they are available, and returns immediately, reducing the number of available permits by the given amount. This method has the same effect as the loop for (int i = 0; i < permits; ++i) acquire(); except that it atomically acquires the permits all at once:

If insufficient permits are available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two 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. </ul>

If the current thread: <ul> <li>has its interrupted status set on entry to this method; or <li>is Thread#interrupt interrupted while waiting for a permit, </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 permits had been made available by a call to #release().

Java documentation for java.util.concurrent.Semaphore.acquire(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