Sdílet prostřednictvím


ReentrantReadWriteLock.ReentrantWriteLock.TryLock Method

Definition

Overloads

TryLock()

Acquires the write lock only if it is not held by another thread at the time of invocation.

TryLock(Int64, TimeUnit)

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

TryLock()

Acquires the write lock only if it 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 lock was free and was acquired by the current thread, or the write lock was already held by the current thread; and false otherwise.

Implements

Attributes

Remarks

Acquires the write lock only if it is not held by another thread at the time of invocation.

Acquires the write lock if neither the read nor write lock are held by another thread and returns immediately with the value true, setting the write lock hold count to one. Even when this lock has been set to use a fair ordering policy, a call to tryLock()<em>will</em> immediately acquire the lock if it is available, whether or not other threads are currently waiting for the write lock. This &quot;barging&quot; 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 current thread already holds this lock then the hold count is incremented by one and the method returns true.

If the lock is held by another thread then this method will return immediately with the value false.

Java documentation for java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock.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 write lock if it 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 write lock

unit
TimeUnit

the time unit of the timeout argument

Returns

true if the lock was free and was acquired by the current thread, or the write lock was already held by the current thread; and false if the waiting time elapsed before the lock could be acquired.

Implements

Attributes

Exceptions

if the current thread is interrupted

if the time unit is null

Remarks

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

Acquires the write lock if neither the read nor write lock are held by another thread and returns immediately with the value true, setting the write lock hold count to one. 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 write 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 current thread already holds this lock then the hold count is incremented by one and the method returns true.

If the 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 write 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 write lock is acquired then the value true is returned and the write lock hold count is set to one.

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 write 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.

Java documentation for java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock.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