Most visited

Recently visited

Added in API level 1

ReentrantReadWriteLock.ReadLock

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

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


由方法 readLock()返回的锁。

Summary

Protected constructors

ReentrantReadWriteLock.ReadLock(ReentrantReadWriteLock lock)

构造函数由子类使用。

Public methods

void lock()

获取读锁。

void lockInterruptibly()

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

Condition newCondition()

抛出 UnsupportedOperationException因为 ReadLocks不支持条件。

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

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

构造函数由子类使用。

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

Public methods

lock

Added in API level 1
void lock ()

获取读锁。

如果写入锁不是由另一个线程保存并立即返回,则获取读取锁。

如果写入锁由另一个线程保存,则当前线程因为线程调度目的而被禁用,并且处于休眠状态,直到获取读取锁。

lockInterruptibly

Added in API level 1
void lockInterruptibly ()

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

如果写入锁不是由另一个线程保存并立即返回,则获取读取锁。

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

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

如果当前线程:

  • has its interrupted status set on entry to this method; or
  • is interrupted while acquiring the read 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 ()

抛出 UnsupportedOperationException因为 ReadLocks不支持条件。

Returns
Condition A new Condition instance for this Lock instance
Throws
UnsupportedOperationException always

toString

Added in API level 1
String toString ()

返回标识此锁定的字符串以及其锁定状态。 括号中的状态包括字符串"Read locks ="后面跟着持有读取锁的数量。

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

tryLock

Added in API level 1
boolean tryLock ()

只有在调用时写入锁未被另一个线程持有的情况下才会获取读取锁。

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

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

Returns
boolean true if the read lock was acquired

tryLock

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

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

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

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

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

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

如果读锁被获取,则返回值 true

如果当前线程:

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

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

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

Parameters
timeout long: the time to wait for the read lock
unit TimeUnit: the time unit of the timeout argument
Returns
boolean true if the read lock was 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!