ReentrantReadWriteLock.ReentrantReadLock.TryLock Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
TryLock() |
Acquires the read lock only if the write lock is not held by another thread at the time of invocation. |
TryLock(Int64, TimeUnit) |
Acquires the read lock if the write lock is not held by another thread within the given waiting time and the current thread has not been Thread#interrupt interrupted. |
TryLock()
Acquires the read lock only if the write lock is not held by another thread at the time of invocation.
[Android.Runtime.Register("tryLock", "()Z", "GetTryLockHandler")]
public virtual bool TryLock ();
[<Android.Runtime.Register("tryLock", "()Z", "GetTryLockHandler")>]
abstract member TryLock : unit -> bool
override this.TryLock : unit -> bool
Returns
true
if the read lock was acquired
Implements
- Attributes
Remarks
Acquires the read lock only if the write lock is not held by another thread at the time of invocation.
Acquires the read lock if the write lock is not held by another thread and returns immediately with the value true
. Even when this lock has been set to use a fair ordering policy, a call to tryLock()
<em>will</em> immediately acquire the read lock if it is available, whether or not other threads are currently waiting for the read lock. This "barging" behavior can be useful in certain circumstances, even though it breaks fairness. If you want to honor the fairness setting for this lock, then use #tryLock(long, TimeUnit) tryLock(0, TimeUnit.SECONDS)
which is almost equivalent (it also detects interruption).
If the write lock is held by another thread then this method will return immediately with the value false
.
Java documentation for java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock.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 read lock if the write lock is not held by another thread 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")]
public virtual bool TryLock (long timeout, Java.Util.Concurrent.TimeUnit? unit);
[<Android.Runtime.Register("tryLock", "(JLjava/util/concurrent/TimeUnit;)Z", "GetTryLock_JLjava_util_concurrent_TimeUnit_Handler")>]
abstract member TryLock : int64 * Java.Util.Concurrent.TimeUnit -> bool
override this.TryLock : int64 * Java.Util.Concurrent.TimeUnit -> bool
Parameters
- timeout
- Int64
the time to wait for the read lock
- unit
- TimeUnit
the time unit of the timeout argument
Returns
true
if the read lock was acquired
Implements
- Attributes
Exceptions
if the current thread is interrupted
if the time unit is null
Remarks
Acquires the read lock if the write lock is not held by another thread within the given waiting time and the current thread has not been Thread#interrupt interrupted.
Acquires the read lock if the write lock is not held by another thread and returns immediately with the value true
. If this lock has been set to use a fair ordering policy then an available lock <em>will not</em> be acquired if any other threads are waiting for the lock. This is in contrast to the #tryLock()
method. If you want a timed tryLock
that does permit barging on a fair lock then combine the timed and un-timed forms together:
{@code
if (lock.tryLock() ||
lock.tryLock(timeout, unit)) {
...
}}
If the write lock is held by another thread then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happens:
<ul>
<li>The read lock is acquired by the current thread; or
<li>Some other thread Thread#interrupt interrupts the current thread; or
<li>The specified waiting time elapses.
</ul>
If the read 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 read lock,
</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.
In this implementation, as this method is an explicit interruption point, preference is given to responding to the interrupt over normal or reentrant acquisition of the lock, and over reporting the elapse of the waiting time.
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.