public static class ReentrantReadWriteLock.ReadLock extends Object implements Lock, Serializable
Modifier | Constructor and Description |
---|---|
protected |
ReadLock(ReentrantReadWriteLock lock)
子类使用的构造函数
|
Modifier and Type | Method and Description |
---|---|
void |
lock()
获取读锁。
|
void |
lockInterruptibly()
获取读锁,除非当前线程
interrupted。
|
Condition |
newCondition()
把
UnsupportedOperationException 因为
ReadLocks 不支持条件。
|
String |
toString()
返回一个确定此锁的字符串,以及它的锁状态。
|
boolean |
tryLock()
仅当调用时不在另一个线程持有写锁时获取读锁。
|
boolean |
tryLock(long timeout, TimeUnit unit)
获取读锁,如果写入锁是不是被另一个线程在等待时间和当前线程没有被
interrupted。
|
void |
unlock()
试图释放这个锁。
|
protected ReadLock(ReentrantReadWriteLock lock)
lock
-外锁对象
NullPointerException
-如果锁是无效的
public void lock()
如果写锁不被另一个线程持有并立即返回,则获取读锁。
如果写锁是由另一个线程持有,那么当前线程成为禁用线程调度的目的,并处于休眠状态,直到读锁已被收购。
public void lockInterruptibly() throws InterruptedException
如果写锁不被另一个线程持有并立即返回,则获取读锁。
如果写锁是由另一个线程持有,那么目前的线程成为禁用线程调度的目的,并处于休眠状态,直到两个事情发生:
如果当前线程:
InterruptedException
投入和当前线程的中断状态被清除。
在这个实现中,这种方法是一种明确的中断点,优先响应中断超过正常或折返的锁的获取。
lockInterruptibly
接口
Lock
InterruptedException
-如果当前线程被中断
public boolean tryLock()
获取读锁,如果写入锁是不是被另一个线程并立即返回的值true
举行。即使这把锁已设置为使用一个公平的订货政策,呼吁tryLock()
将立即如果能获得读锁,是否有其他线程正在等待读锁。这种“闯”的行为在某些情况下可能是有用的,即使它打破了公平。如果你想尊重公平这把锁的设置,然后使用tryLock(0, TimeUnit.SECONDS)
几乎是等价的(它也检测到中断)。
如果写入锁是由另一个线程然后这个方法会立即返回的值false
。
public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException
获取读锁,如果写入锁是不是被另一个线程并立即返回的值true
举行。如果锁已设置为使用一个公平的订货策略,然后可用锁不会如果其他线程等待该锁了。与此形成对比的是,tryLock()
方法。如果你想要一个定时tryLock
不允许讨价还价在公平锁,然后结合定时和不定时的形式:
if (lock.tryLock() ||
lock.tryLock(timeout, unit)) {
...
}
如果写锁是由另一个线程持有,那么目前的线程成为禁用线程调度的目的,并处于休眠状态,直到三个事情发生:
如果读锁是后天再true
返回值。
如果当前线程:
InterruptedException
投入和当前线程的中断状态被清除。
如果指定的等待时间的流逝,然后false
返回值。如果时间小于或等于零,该方法将不会等待在所有。
在这个实现中,这种方法是一种明确的中断点,优先响应中断超过正常或折返的锁的获取,并在报告的等待时间的流逝。
tryLock
接口
Lock
timeout
-时间等待读锁
unit
- timeout参数的时间单位
true
如果获得读锁
InterruptedException
-如果当前线程被中断
NullPointerException
-如果时间单位为零
public Condition newCondition()
UnsupportedOperationException
因为
ReadLocks
不支持条件。
newCondition
接口
Lock
Condition
Lock
实例实例
UnsupportedOperationException
-永远
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.