ILock.TryLock Method

Definition

Overloads

TryLock()

Acquires the lock only if it is free at the time of invocation.

TryLock(Int64, TimeUnit)

Acquires the lock if it is free within the given waiting time and the current thread has not been Thread#interrupt interrupted.

TryLock()

Acquires the lock only if it is free at the time of invocation.

[Android.Runtime.Register("tryLock", "()Z", "GetTryLockHandler:Java.Util.Concurrent.Locks.ILockInvoker, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null")]
public bool TryLock ();
[<Android.Runtime.Register("tryLock", "()Z", "GetTryLockHandler:Java.Util.Concurrent.Locks.ILockInvoker, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null")>]
abstract member TryLock : unit -> bool

Returns

true if the lock was acquired and false otherwise

Attributes

Remarks

Acquires the lock only if it is free at the time of invocation.

Acquires the lock if it is available and returns immediately with the value true. If the lock is not available then this method will return immediately with the value false.

A typical usage idiom for this method would be:

{@code
            Lock lock = ...;
            if (lock.tryLock()) {
              try {
                // manipulate protected state
              } finally {
                lock.unlock();
              }
            } else {
              // perform alternative actions
            }}

This usage ensures that the lock is unlocked if it was acquired, and doesn't try to unlock if the lock was not acquired.

Java documentation for java.util.concurrent.locks.Lock.tryLock().

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

TryLock(Int64, TimeUnit)

Acquires the lock if it is free within the given waiting time and the current thread has not been Thread#interrupt interrupted.

[Android.Runtime.Register("tryLock", "(JLjava/util/concurrent/TimeUnit;)Z", "GetTryLock_JLjava_util_concurrent_TimeUnit_Handler:Java.Util.Concurrent.Locks.ILockInvoker, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null")]
public bool TryLock (long time, Java.Util.Concurrent.TimeUnit? unit);
[<Android.Runtime.Register("tryLock", "(JLjava/util/concurrent/TimeUnit;)Z", "GetTryLock_JLjava_util_concurrent_TimeUnit_Handler:Java.Util.Concurrent.Locks.ILockInvoker, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null")>]
abstract member TryLock : int64 * Java.Util.Concurrent.TimeUnit -> bool

Parameters

time
Int64

the maximum time to wait for the lock

unit
TimeUnit

the time unit of the time argument

Returns

true if the lock was acquired and false if the waiting time elapsed before the lock was acquired

Attributes

Exceptions

if the current thread is interrupted while acquiring the lock (and interruption of lock acquisition is supported)

Remarks

Acquires the lock if it is free within the given waiting time and the current thread has not been Thread#interrupt interrupted.

If the lock is available this method returns immediately with the value true. If the lock is not available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happens: <ul> <li>The lock is acquired by the current thread; or <li>Some other thread Thread#interrupt interrupts the current thread, and interruption of lock acquisition is supported; or <li>The specified waiting time elapses </ul>

If the lock 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 acquiring the lock, and interruption of lock acquisition is supported, </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.

<b>Implementation Considerations</b>

The ability to interrupt a lock acquisition in some implementations may not be possible, and if possible may be an expensive operation. The programmer should be aware that this may be the case. An implementation should document when this is the case.

An implementation can favor responding to an interrupt over normal method return, or reporting a timeout.

A Lock implementation may be able to detect erroneous use of the lock, such as an invocation that would cause deadlock, and may throw an (unchecked) exception in such circumstances. The circumstances and the exception type must be documented by that Lock implementation.

Java documentation for java.util.concurrent.locks.Lock.tryLock(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