Java.Util.Concurrent.Locks.ILock.TryLock Method
Acquires the lock if it is free within the given waiting time and the current thread has not been Java.Lang.Thread.Interrupt.

Syntax

[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)

Parameters

time
the maximum time to wait for the lock
unit
the time unit of the time argument

Returns

Documentation for this section has not yet been entered.

Exceptions

TypeReason
Java.Lang.InterruptedExceptionif 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 Java.Lang.Thread.Interrupt.

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:

  • The lock is acquired by the current thread; or
  • Some other thread Java.Lang.Thread.Interrupt the current thread, and interruption of lock acquisition is supported; or
  • The specified waiting time elapses

If the lock is acquired then the value true is returned.

If the current thread:

  • has its interrupted status set on entry to this method; or
  • is Java.Lang.Thread.Interrupt while acquiring the lock, and interruption of lock acquisition is supported,
then Java.Lang.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.

Implementation Considerations

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.

[Android Documentation]

Requirements

Namespace: Java.Util.Concurrent.Locks
Assembly: Mono.Android (in Mono.Android.dll)
Assembly Versions: 0.0.0.0
Since: Added in API level 1