Most visited

Recently visited

Added in API level 1

ReentrantReadWriteLock.WriteLock

public static class ReentrantReadWriteLock.WriteLock
extends Object implements Lock, Serializable

java.lang.Object
   ↳ java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock


方法 writeLock()返回的锁。

Summary

Protected constructors

ReentrantReadWriteLock.WriteLock(ReentrantReadWriteLock lock)

构造函数由子类使用。

Public methods

int getHoldCount()

查询当前线程在此写入锁定上的保留数量。

boolean isHeldByCurrentThread()

查询当前线程是否保持该写入锁定。

void lock()

获取写入锁定。

void lockInterruptibly()

获取写入锁定,除非当前线程为 interrupted

Condition newCondition()

返回 Condition与此使用实例 Lock实例。

String toString()

返回标识此锁定的字符串以及其锁定状态。

boolean tryLock()

只有在调用时它未被另一个线程占用的情况下才能获取写入锁定。

boolean tryLock(long timeout, TimeUnit unit)

如果在给定的等待时间内没有被另一个线程 占用 ,并且当前线程不是 interrupted ,则获取写入锁定。

void unlock()

试图释放此锁。

Inherited methods

From class java.lang.Object
From interface java.util.concurrent.locks.Lock

Protected constructors

ReentrantReadWriteLock.WriteLock

Added in API level 1
ReentrantReadWriteLock.WriteLock (ReentrantReadWriteLock lock)

构造函数由子类使用。

Parameters
lock ReentrantReadWriteLock: the outer lock object
Throws
NullPointerException if the lock is null

Public methods

getHoldCount

Added in API level 9
int getHoldCount ()

查询当前线程在此写入锁定上的保留数量。 一个线程对每个锁定操作都有一个锁定,而不是通过解锁操作进行匹配。 getWriteHoldCount()相同。

Returns
int the number of holds on this lock by the current thread, or zero if this lock is not held by the current thread

isHeldByCurrentThread

Added in API level 9
boolean isHeldByCurrentThread ()

查询当前线程是否保持该写入锁定。 isWriteLockedByCurrentThread()相同。

Returns
boolean true if the current thread holds this lock and false otherwise

lock

Added in API level 1
void lock ()

获取写入锁定。

如果读锁和写锁都不被另一个线程持有并立即返回,则将写锁定保持计数设置为1。

如果当前线程已经保存了写入锁定,则保持计数增加1,并且该方法立即返回。

如果该锁由另一个线程保存,则当前线程因为线程调度目的而被禁用,并且处于休眠状态,直到已获取写锁,此时写锁定保持计数设置为1。

lockInterruptibly

Added in API level 1
void lockInterruptibly ()

获取写入锁定,除非当前线程为 interrupted

如果读锁和写锁都不被另一个线程持有并立即返回,则将写锁定保持计数设置为1。

如果当前线程已经保存了这个锁,那么保持计数加1,并且该方法立即返回。

如果锁由另一个线程保存,则当前线程因为线程调度目的而被禁用,并且处于休眠状态,直到发生以下两件事之一:

  • The write lock is acquired by the current thread; or
  • Some other thread interrupts the current thread.

如果写入锁定由当前线程获取,则锁定保持计数设置为1。

如果当前线程:

  • has its interrupted status set on entry to this method; or
  • is interrupted while acquiring the write lock,
then InterruptedException is thrown and the current thread's interrupted status is cleared.

在这个实现中,由于这个方法是一个明确的中断点,因此优先考虑响应正常或重入锁的中断。

Throws
InterruptedException if the current thread is interrupted

newCondition

Added in API level 1
Condition newCondition ()

返回 Condition与此使用实例 Lock实例。

返回 Condition实例支持相同的用途为做 Object监视器方法( waitnotify ,并 notifyAll与使用时)内置监视器锁定。

  • If this write lock is not held when any Condition method is called then an IllegalMonitorStateException is thrown. (Read locks are held independently of write locks, so are not checked or affected. However it is essentially always an error to invoke a condition waiting method when the current thread has also acquired read locks, since other threads that could unblock it will not be able to acquire the write lock.)
  • When the condition waiting methods are called the write lock is released and, before they return, the write lock is reacquired and the lock hold count restored to what it was when the method was called.
  • If a thread is interrupted while waiting then the wait will terminate, an InterruptedException will be thrown, and the thread's interrupted status will be cleared.
  • Waiting threads are signalled in FIFO order.
  • The ordering of lock reacquisition for threads returning from waiting methods is the same as for threads initially acquiring the lock, which is in the default case not specified, but for fair locks favors those threads that have been waiting the longest.

Returns
Condition the Condition object

toString

Added in API level 1
String toString ()

返回标识此锁定的字符串以及其锁定状态。 括号中的状态包括字符串"Unlocked"或字符串"Locked by"后面跟随拥有线程的name

Returns
String a string identifying this lock, as well as its lock state

tryLock

Added in API level 1
boolean tryLock ()

只有在调用时它未被另一个线程占用的情况下才能获取写入锁定。

如果读锁和写锁都不被另一个线程true ,并立即返回值true ,则将写锁定保持计数设置为1。 即使这个锁被设置为使用公平的订购策略,对tryLock()的调用也会立即获得该锁(如果该锁可用),无论其他线程当前是否正在等待写入锁定。 这种“bar”“行为在某些情况下可能会有用,即使它违背公平。 如果您想要遵守此锁的公平性设置,请使用几乎相同的tryLock(0, TimeUnit.SECONDS) (它也会检测到中断)。

如果当前线程已经拥有此锁定,则保持计数会加1并返回 true

如果锁由另一个线程保存,则此方法将立即返回值 false

Returns
boolean 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.

tryLock

Added in API level 1
boolean tryLock (long timeout, 
                TimeUnit unit)

如果在给定的等待时间内没有被另一个线程持有并且当前线程未被 interrupted占用 ,则获取写入锁定。

如果读取和写入锁定均未被另一个线程true ,并立即返回值true ,则将写入锁定保持计数设置为1,从而获取写入锁定。 如果此锁已设置为使用公平的订购策略,那么如果有其他线程正在等待写入锁, 则不会获取可用锁。 这与tryLock()方法相反。 如果你想要一个允许公平锁定的定时tryLock ,那么将定时和tryLock表格组合在一起:

 if (lock.tryLock() ||
     lock.tryLock(timeout, unit)) {
   ...
 }

如果当前线程已经拥有此锁定,则保持计数会加1并返回 true

如果锁由另一个线程保存,则当前线程因为线程调度目的而被禁用,并且处于休眠状态,直到发生三件事之一:

  • The write lock is acquired by the current thread; or
  • Some other thread interrupts the current thread; or
  • The specified waiting time elapses

如果获取写锁定,则返回值 true ,写锁定保持计数设置为1。

如果当前线程:

  • has its interrupted status set on entry to this method; or
  • is interrupted while acquiring the write lock,
then InterruptedException is thrown and the current thread's interrupted status is cleared.

如果经过了指定的等待时间,则返回值false 如果时间小于或等于零,该方法将不会等待。

在该实现中,由于该方法是明确的中断点,因此优先考虑响应正常或重入锁获取的中断,并报告等待时间的过去。

Parameters
timeout long: the time to wait for the write lock
unit TimeUnit: the time unit of the timeout argument
Returns
boolean 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.
Throws
InterruptedException if the current thread is interrupted
NullPointerException if the time unit is null

unlock

Added in API level 1
void unlock ()

试图释放此锁。

如果当前线程是该锁的持有者,则保持计数递减。 如果保持计数现在为零,则锁定被释放。 如果当前线程不是该锁的持有者,则引发IllegalMonitorStateException

Throws
IllegalMonitorStateException if the current thread does not hold this lock

Hooray!