标签:purpose 互斥锁 操作 情况下 rem 相同 monitor 不可 tle
目录
All Implemented Interfaces(所有已实现接口)
所在包:java.util.concurrent.locks.ReentrantLock
public class ReentrantLock
extends Object
implements Lock, Serializable
Lock
with the same basic behavior and semantics as the implicit monitor lock accessed using synchronized
methods and statements, but with extended capabilities.可重入互斥锁,具有与使用同步方法和语句访问的隐式监视器锁相同的基本行为和语义,但具有扩展功能。
A ReentrantLock
is owned by the thread last successfully locking, but not yet unlocking it.
A thread invoking lock
will return, successfully acquiring the lock, when the lock is not owned by another thread.
The method will return immediately if the current thread already owns the lock.
This can be checked using methods isHeldByCurrentThread()
, and getHoldCount()
.
ReentrantLock由最后一次成功锁定的线程拥有,但尚未解锁它。
当锁不属于另一个线程时,调用锁的线程将返回,并成功获取锁。
如果当前线程已经拥有锁,则该方法将立即返回。
可以使用方法isHeldByCurrentThread()和getHoldCount()进行检查。
The constructor for this class accepts an optional fairness parameter. When set true
, under contention, locks favor granting access to the longest-waiting thread. Otherwise this lock does not guarantee any particular access order. Programs using fair locks accessed by many threads may display lower overall throughput (i.e., are slower; often much slower) than those using the default setting, but have smaller variances in times to obtain locks and guarantee lack of starvation. Note however, that fairness of locks does not guarantee fairness of thread scheduling. Thus, one of many threads using a fair lock may obtain it multiple times in succession while other active threads are not progressing and not currently holding the lock. Also note that the untimed tryLock()
method does not honor the fairness setting. It will succeed if the lock is available even if other threads are waiting.
该类的构造函数接受一个可选的公平性参数。当设置为真时,在争用状态下,锁定倾向于授予对等待时间最长的线程的访问权。否则,此锁不保证任何特定的访问顺序。使用多个线程访问的公平锁的程序可能会显示较低的总体吞吐量(即更慢;通常比那些使用默认设置的要慢得多),但是在获得锁和保证不会饿死方面的时间差异更小。但是请注意,锁的公平性并不保证线程调度的公平性。因此,使用公平锁的多个线程中的一个可能会连续多次获得它,而其他活动线程没有进展,也没有当前持有锁。还要注意,不定时的tryLock()方法不支持公平性设置。如果锁可用,即使其他线程正在等待,它也会成功。
It is recommended practice to always immediately follow a call to lock
with a try
block, most typically in a before/after construction such as:
建议的做法是,总是立即跟随一个调用来锁定一个try块,最典型的是在一个前后结构,如:
class X {
private final ReentrantLock lock = new ReentrantLock();
// ...
public void m() {
lock.lock(); // block until condition holds
try {
// ... method body
} finally {
lock.unlock()
}
}
}
In addition to implementing the Lock
interface, this class defines a number of public
and protected
methods for inspecting the state of the lock. Some of these methods are only useful for instrumentation and monitoring.
除了实现锁接口之外,该类还定义了许多用于检查锁状态的公共方法和受保护方法。其中一些方法仅对检测和监视有用。
Serialization of this class behaves in the same way as built-in locks: a deserialized lock is in the unlocked state, regardless of its state when serialized.
这个类的序列化与内置锁的行为方式相同:反序列化的锁处于解锁状态,而与它在序列化时的状态无关。
This lock supports a maximum of 2147483647 recursive locks by the same thread. Attempts to exceed this limit result in Error
throws from locking methods.
此锁最多支持同一线程的2147483647个递归锁。试图超过此限制将导致锁定方法抛出错误。
Constructor and Description 构造函数和描述 |
---|
ReentrantLock()
Creates an instance of
ReentrantLock .创建ReentrantLock的实例。 |
ReentrantLock(boolean fair)
Creates an instance of
ReentrantLock with the given fairness policy.使用给定的公平策略创建ReentrantLock实例。
|
Modifier and Type 修饰符和类型 |
Method and Description 方法和描述 |
---|---|
int |
getHoldCount()
Queries the number of holds on this lock by the current thread.
查询当前线程持有此锁的次数。
|
protected Thread |
getOwner()
Returns the thread that currently owns this lock, or
null if not owned.返回当前拥有此锁的线程,如果不拥有则返回null。
|
protected Collection<Thread> |
getQueuedThreads()
Returns a collection containing threads that may be waiting to acquire this lock.
返回当前拥有此锁的线程,如果不拥有则返回null。返回一个包含可能正在等待获取此锁的线程的集合。
|
int |
getQueueLength()
Returns an estimate of the number of threads waiting to acquire this lock.
返回等待获取此锁的线程数量的估计值。
|
protected Collection<Thread> |
getWaitingThreads(Condition condition)
Returns a collection containing those threads that may be waiting on the given condition associated with this lock.
返回一个集合,其中包含可能正在等待与此锁关联的给定条件的线程。
|
int |
getWaitQueueLength(Condition condition)
Returns an estimate of the number of threads waiting on the given condition associated with this lock.
返回与此锁关联的给定条件下等待的线程数的估计值。
|
boolean |
hasQueuedThread(Thread thread)
Queries whether the given thread is waiting to acquire this lock.
查询给定线程是否正在等待获取此锁。
|
boolean |
hasQueuedThreads()
Queries whether any threads are waiting to acquire this lock.
查询是否有线程正在等待获取此锁。
|
boolean |
hasWaiters(Condition condition)
Queries whether any threads are waiting on the given condition associated with this lock.
查询是否有线程正在等待与此锁关联的给定条件。
|
boolean |
isFair()
Returns
true if this lock has fairness set true.如果此锁的公平设置为真,则返回真。
|
boolean |
isHeldByCurrentThread()
Queries if this lock is held by the current thread.
查询此锁是否由当前线程持有。
|
boolean |
isLocked()
Queries if this lock is held by any thread.
查询此锁是否由任何线程持有。
|
void |
lock()
Acquires the lock.
获得锁。
|
void |
lockInterruptibly()
Acquires the lock unless the current thread is interrupted.
除非当前线程中断,否则获取锁。
|
Condition |
newCondition()
除非当前线程中断,否则获取锁。返回用于此锁实例的条件实例。
|
String |
toString()
Returns a string identifying this lock, as well as its lock state.
返回标识此锁的字符串及其锁状态。
|
boolean |
tryLock()
Acquires the lock only if it is not held by another thread at the time of invocation.
仅当锁在调用时不被其他线程持有时,才获取锁。
|
boolean |
tryLock(long timeout, TimeUnit unit)
Acquires the lock if it is not held by another thread within the given waiting time and the current thread has not been interrupted.
如果在给定的等待时间内没有被其他线程持有,并且当前线程没有被中断,则获取锁。
|
void |
unlock()
Attempts to release this lock.
试图释放此锁。 |
方法继承自java.lang.Object类
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
public ReentrantLock()
ReentrantLock
. This is equivalent to using ReentrantLock(false)
.创建ReentrantLock的实例。这相当于使用ReentrantLock(false)。
public ReentrantLock(boolean fair)
ReentrantLock
with the given fairness policy.fair
- true
if this lock should use a fair ordering policy 如果此锁应该使用公平的排序策略,则为truepublic void lock()
Acquires the lock if it is not held by another thread and returns immediately, setting the lock hold count to one.
If the current thread already holds the lock then the hold count is incremented by one and the method returns immediately.
If the lock is held by another thread then the current thread becomes disabled for thread scheduling purposes and lies dormant until the lock has been acquired, at which time the lock hold count is set to one.
获得锁。
如果锁不被其他线程持有,则获取锁并立即返回,将锁持有计数设置为1。
如果当前线程已经持有锁,那么持有计数将增加1,该方法立即返回。
如果锁被另一个线程持有,那么当前线程将出于线程调度的目的而禁用,并处于休眠状态,直到锁被获取,此时锁持有计数被设置为1。
public void lockInterruptibly() throws InterruptedException
Acquires the lock if it is not held by another thread and returns immediately, setting the lock hold count to one.
除非当前线程中断,否则获取锁。如果锁不被其他线程持有,则获取锁并立即返回,将锁持有计数设置为1。
If the current thread already holds this lock then the hold count is incremented by one and the method returns immediately.
如果当前线程已经持有该锁,那么持有计数将增加1,该方法立即返回。
If the lock is held by another thread then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happens:
如果锁是由另一个线程持有,那么当前线程就会出于线程调度的目的而被禁用,并处于休眠状态,直到发生以下两种情况之一:
If the lock is acquired by the current thread then the lock hold count is set to one.
如果锁被当前线程获取,那么锁持有计数被设置为1。
If the current thread:
如果当前线程:
在进入此方法时已设置其中断状态;或
InterruptedException
is thrown and the current thread‘s interrupted status is cleared.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.
在这个实现中,由于这个方法是一个显式的中断点,所以优先响应中断,而不是正常的或可重入的锁获取。
lockInterruptibly
in interface Lock
InterruptedException
- if the current thread is interrupted 如果当前线程中断public void lockInterruptibly() throws InterruptedException
Acquires the lock if it is not held by another thread and returns immediately, setting the lock hold count to one.
如果锁不被其他线程持有,则获取锁并立即返回,将锁持有计数设置为1。
If the current thread already holds this lock then the hold count is incremented by one and the method returns immediately.
如果锁不被其他线程持有,则获取锁并立即返回,将锁持有计数设置为1。如果当前线程已经持有该锁,那么持有计数将增加1,该方法立即返回。
If the lock is held by another thread then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happens:
如果锁是由另一个线程持有,那么当前线程就会出于线程调度的目的而被禁用,并处于休眠状态,直到发生以下两种情况之一:
If the lock is acquired by the current thread then the lock hold count is set to one.
如果锁被当前线程获取,那么锁持有计数被设置为1。
If the current thread:
如果当前线程:
在进入此方法时已设置其中断状态;或
InterruptedException
is thrown and the current thread‘s interrupted status is cleared.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.
在这个实现中,由于这个方法是一个显式的中断点,所以优先响应中断,而不是正常的或可重入的锁获取。
lockInterruptibly
in interface Lock
InterruptedException
- if the current thread is interrupted public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException
Acquires the lock if it is not held by another thread and returns immediately with the value true
, setting the lock hold count to one. If this lock has been set to use a fair ordering policy then an available lock will not 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:
如果锁未被其他线程持有,则获取锁,并立即返回值true,将锁持有计数设置为1。
如果这个锁被设置为使用公平的排序策略,那么如果任何其他线程正在等待这个锁,那么将不会获得一个可用的锁。
这与tryLock()方法相反。
如果你想要一个定时的tryLock,允许对一个公平的锁,然后结合在一起的时间和不定时的形式:
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
.
如果当前线程已经持有该锁,那么持有计数将增加1,该方法返回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:
如果锁是由另一个线程持有,那么当前线程就会出于线程调度的目的而被禁用,并处于休眠状态,直到发生以下三种情况之一:
If the lock is acquired then the value true
is returned and the lock hold count is set to one.
如果获取了锁,则返回true值,并将锁持有计数设置为1。
If the current thread:
如果当前线程:
在进入此方法时已设置其中断状态;或
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.
如果指定的等待时间过期,则返回false值。如果时间小于或等于0,则该方法根本不会等待。
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.
在这个实现中,由于这个方法是一个显式的中断点,所以优先响应中断,而不是正常的或可重入的锁获取,也不是报告等待时间的流逝。
tryLock
in interface Lock
timeout
- the time to wait for the lock等待锁的时间到了unit
- the time unit of the timeout argumenttrue
if the lock was free and was acquired by the current thread, or the lock was already held by the current thread; and false
if the waiting time elapsed before the lock could be acquired如果锁是空闲的并且被当前线程获取,或者锁已经被当前线程持有,则为true;
如果在获取锁之前的等待时间已经过去,则为false
InterruptedException
- if the current thread is interrupted 如果当前线程中断NullPointerException
- if the time unit is null 如果时间单位为空public void unlock()
If the current thread is the holder of this lock then the hold count is decremented.
If the hold count is now zero then the lock is released.
If the current thread is not the holder of this lock then IllegalMonitorStateException
is thrown.
如果试图释放这个锁。
如果当前线程是这个锁的持有者,那么持有计数将递减。
如果持有计数现在为零,那么锁将被释放。
如果当前线程不是这个锁的持有者,则抛出IllegalMonitorStateException。
unlock
in interface Lock
IllegalMonitorStateException
- if the current thread does not hold this lock 如果当前线程不持有此锁
public Condition newCondition()
Condition
instance for use with this Lock
instance.The returned Condition
instance supports the same usages as do the Object
monitor methods (wait
, notify
, and notifyAll
) when used with the built-in monitor lock.
当与内置的监视器锁一起使用时,返回的条件实例支持与对象监视器方法(wait、notify和notifyAll)相同的用法。
Condition
waiting or signalling methods are called, then an IllegalMonitorStateException
is thrown.InterruptedException
will be thrown, and the thread‘s interrupted status will be cleared.newCondition
in interface Lock
public int getHoldCount()
A thread has a hold on a lock for each lock action that is not matched by an unlock action.
一个线程对每个锁操作都持有一个锁,而每个锁操作都与一个解锁操作不匹配。
The hold count information is typically only used for testing and debugging purposes. For example, if a certain section of code should not be entered with the lock already held then we can assert that fact:
hold count信息通常仅用于测试和调试目的。例如,如果一个特定的代码段不应该与已经持有的锁一起输入,那么我们可以断言这个事实:
class X { ReentrantLock lock = new ReentrantLock(); // ... public void m() { assert lock.getHoldCount() == 0; lock.lock(); try { // ... method body } finally { lock.unlock(); } } }
public boolean isHeldByCurrentThread()
Analogous to the Thread.holdsLock(Object)
method for built-in monitor locks, this method is typically used for debugging and testing. For example, a method that should only be called while a lock is held can assert that this is the case:
与用于内置监视器锁的Thread.holdsLock(Object)方法类似,此方法通常用于调试和测试。例如,只有在锁被持有时才应该调用的方法可以断言:
class X { ReentrantLock lock = new ReentrantLock(); // ... public void m() { assert lock.isHeldByCurrentThread(); // ... method body } }
It can also be used to ensure that a reentrant lock is used in a non-reentrant manner, for example:
它也可以用来确保可重入锁以不可重入的方式使用,例如:
class X { ReentrantLock lock = new ReentrantLock(); // ... public void m() { assert !lock.isHeldByCurrentThread(); lock.lock(); try { // ... method body } finally { lock.unlock(); } } }
true
if current thread holds this lock and false
otherwise public boolean isLocked()
true
if any thread holds this lock and false
otherwise public final boolean isFair()
true
if this lock has fairness set true.true
if this lock has fairness set true protected Thread getOwner()
null
if not owned. When this method is called by a thread that is not the owner, the return value reflects a best-effort approximation of current lock status. For example, the owner may be momentarily null
even if there are threads trying to acquire the lock but have not yet done so. This method is designed to facilitate construction of subclasses that provide more extensive lock monitoring facilities.null
if not owned public final boolean hasQueuedThreads()
true
return does not guarantee that any other thread will ever acquire this lock. 查询是否有线程正在等待获取此锁。
注意,因为取消可能在任何时候发生,一个真正的返回并不保证任何其他线程将获得这个锁。
该方法主要用于监控系统状态。
true
if there may be other threads waiting to acquire the lock public final boolean hasQueuedThread(Thread thread)
true
return does not guarantee that this thread will ever acquire this lock. thread
- the threadtrue
if the given thread is queued waiting for this lockNullPointerException
- if the thread is null 如果线程为空public final int getQueueLength()
返回等待获取此锁的线程数量的估计值。
这个值只是一个估计值,因为当这个方法遍历内部数据结构时,线程的数量可能会动态变化。
此方法设计用于监视系统状态,而不是用于同步控制。
protected Collection<Thread> getQueuedThreads()
public boolean hasWaiters(Condition condition)
true
return does not guarantee that a future signal
will awaken any threads. 查询是否有线程正在等待与此锁关联的给定条件。
注意,因为超时和中断可能在任何时候发生,所以真正的返回并不保证将来的信号将唤醒任何线程。
该方法主要用于监控系统状态。
condition
- the conditiontrue
if there are any waiting threadsIllegalMonitorStateException
- if this lock is not held如果这个锁没有被持有IllegalArgumentException
- if the given condition is not associated with this lock如果给定的条件与此锁没有关联NullPointerException
- if the condition is null 如果条件为空public int getWaitQueueLength(Condition condition)
返回与此锁关联的给定条件下等待的线程数的估计值。
请注意,由于超时和中断可能随时发生,因此估计值仅作为实际等待者数量的上限。
此方法设计用于监视系统状态,而不是用于同步控制。
condition
- the conditionIllegalMonitorStateException
- if this lock is not held如果这个锁没有被持有IllegalArgumentException
- if the given condition is not associated with this lock如果给定的条件与此锁没有关联NullPointerException
- if the condition is null 如果条件为空protected Collection<Thread> getWaitingThreads(Condition condition)
返回一个集合,其中包含可能正在等待与此锁关联的给定条件的线程。
因为在构造这个结果时,实际的线程集可能会动态变化,所以返回的集合只是一个最佳效果的估计。
返回集合的元素没有特定的顺序。
这种方法的目的是为了方便构建提供更广泛的状态监视设施的子类。
condition
- the conditionIllegalMonitorStateException
- if this lock is not held如果这个锁没有被持有IllegalArgumentException
- if the given condition is not associated with this lock如果给定的条件与此锁没有关联NullPointerException
- if the condition is null 如果条件为空public String toString()
"Unlocked"
or the String "Locked by"
followed by the name of the owning thread.返回标识此锁的字符串及其锁状态。
括号中的状态包括字符串“解锁”或字符串“Locked by”,后跟所属线程的名称。
标签:purpose 互斥锁 操作 情况下 rem 相同 monitor 不可 tle
原文地址:https://www.cnblogs.com/LinQingYang/p/12577354.html