目录
All Implemented Interfaces(所有已实现接口)
所在包:java.util.concurrent.locks.ReentrantLock
- All Implemented Interfaces(所有已实现接口):
- Serializable, Lock
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个递归锁。试图超过此限制将导致锁定方法抛出错误。
- Since:
- 1.5
- See Also:
- Serialized Form
Constructor Summary(构造函数的总结)
Constructor and Description 构造函数和描述 |
---|
ReentrantLock()
Creates an instance of
ReentrantLock .创建ReentrantLock的实例。 |
ReentrantLock(boolean fair)
Creates an instance of
ReentrantLock with the given fairness policy.使用给定的公平策略创建ReentrantLock实例。
|
Method Summary
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.
试图释放此锁。 |
Methods inherited from class java.lang.Object
方法继承自java.lang.Object类
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Constructor Detailgou(构造方法详述)
-
ReentrantLock
public ReentrantLock()
Creates an instance ofReentrantLock
. This is equivalent to usingReentrantLock(false)
.
创建ReentrantLock的实例。这相当于使用ReentrantLock(false)。
-
ReentrantLock
public ReentrantLock(boolean fair)
Creates an instance ofReentrantLock
with the given fairness policy.
使用给定的公平策略创建ReentrantLock实例。- Parameters:
fair
-true
if this lock should use a fair ordering policy 如果此锁应该使用公平的排序策略,则为true
Method Detail(方法详述)
-
lock
public void lock()
Acquires the 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。
-
lockInterruptibly
public void lockInterruptibly() throws InterruptedException
Acquires the lock unless the current thread is interrupted.
除非当前线程中断,否则获取锁。
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:
如果锁是由另一个线程持有,那么当前线程就会出于线程调度的目的而被禁用,并处于休眠状态,直到发生以下两种情况之一:
- The lock is acquired by the current thread; or
锁被当前线程获取;或 - Some other thread interrupts the current thread.
其他一些线程会中断当前线程。
If the lock is acquired by the current thread then the lock hold count is set to one.
如果锁被当前线程获取,那么锁持有计数被设置为1。If the current thread:
如果当前线程:- has its interrupted status set on entry to this method; or
在进入此方法时已设置其中断状态;或
- is interrupted while acquiring the lock,
在获取锁时中断,
InterruptedException
is thrown and the current thread's interrupted status is cleared.
然后抛出InterruptedException,并清除当前线程的中断状态。
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.
在这个实现中,由于这个方法是一个显式的中断点,所以优先响应中断,而不是正常的或可重入的锁获取。- Specified by:
lockInterruptibly
in interfaceLock
- Throws:
InterruptedException
- if the current thread is interrupted 如果当前线程中断
- The lock is acquired by the current thread; or
-
lockInterruptibly
public void lockInterruptibly() throws InterruptedException
Acquires the lock unless the current thread is interrupted.
除非当前线程中断,否则获取锁。
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:
如果锁是由另一个线程持有,那么当前线程就会出于线程调度的目的而被禁用,并处于休眠状态,直到发生以下两种情况之一:
- The lock is acquired by the current thread; or
锁被当前线程获取;或 - Some other thread interrupts the current thread.
其他一些线程会中断当前线程。
If the lock is acquired by the current thread then the lock hold count is set to one.
如果锁被当前线程获取,那么锁持有计数被设置为1。If the current thread:
如果当前线程:- has its interrupted status set on entry to this method; or
在进入此方法时已设置其中断状态;或
- is interrupted while acquiring the lock,
在获取锁时中断,
InterruptedException
is thrown and the current thread's interrupted status is cleared.
然后抛出InterruptedException,并清除当前线程的中断状态。
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.
在这个实现中,由于这个方法是一个显式的中断点,所以优先响应中断,而不是正常的或可重入的锁获取。- Specified by:
lockInterruptibly
in interfaceLock
- Throws:
InterruptedException
- if the current thread is interrupted
如果当前线程中断
- The lock is acquired by the current thread; or
-
tryLock
public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException
Acquires the lock if it is not held by another thread within the given waiting time and the current thread has not been interrupted.
如果在给定的等待时间内没有被其他线程持有,并且当前线程没有被中断,则获取锁。
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 thetryLock()
method. If you want a timedtryLock
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:
如果锁是由另一个线程持有,那么当前线程就会出于线程调度的目的而被禁用,并处于休眠状态,直到发生以下三种情况之一:- The lock is acquired by the current thread; or
锁被当前线程获取;或 - Some other thread interrupts the current thread; or
其他一些线程中断当前线程;或 - The specified waiting time elapses
指定的等待时间已经过了
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:
如果当前线程:- has its interrupted status set on entry to this method; or
在进入此方法时已设置其中断状态;或
- is interrupted while acquiring the lock,
在获取锁时中断,
InterruptedException
is thrown and the current thread's interrupted status is cleared.
然后抛出InterruptedException,并清除当前线程的中断状态。
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.
在这个实现中,由于这个方法是一个显式的中断点,所以优先响应中断,而不是正常的或可重入的锁获取,也不是报告等待时间的流逝。- Specified by:
tryLock
in interfaceLock
- Parameters:
timeout
- the time to wait for the lock等待锁的时间到了unit
- the time unit of the timeout argument
超时参数的时间单位- Returns:
true
if the lock was free and was acquired by the current thread, or the lock was already held by the current thread; andfalse
if the waiting time elapsed before the lock could be acquired
如果锁是空闲的并且被当前线程获取,或者锁已经被当前线程持有,则为true;
如果在获取锁之前的等待时间已经过去,则为false- Throws:
InterruptedException
- if the current thread is interrupted 如果当前线程中断NullPointerException
- if the time unit is null 如果时间单位为空
- The lock is acquired by the current thread; or
-
public void unlock()
Attempts to release this lock.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。- Specified by:
unlock
in interfaceLock
- Throws:
IllegalMonitorStateException
- if the current thread does not hold this lock 如果当前线程不持有此锁
-
newCondition
public Condition newCondition()
Returns aCondition
instance for use with thisLock
instance.
返回用于此锁实例的条件实例。
The returned
Condition
instance supports the same usages as do theObject
monitor methods (wait
,notify
, andnotifyAll
) when used with the built-in monitor lock.
当与内置的监视器锁一起使用时,返回的条件实例支持与对象监视器方法(wait、notify和notifyAll)相同的用法。- If this lock is not held when any of the
Condition
waiting or signalling methods are called, then anIllegalMonitorStateException
is thrown.
如果在调用任何条件等待或发送信号方法时未持有此锁,则抛出IllegalMonitorStateException。 - When the condition waiting methods are called the lock is released and, before they return, the 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.
如果线程在等待期间被中断,那么等待将终止,抛出InterruptedException,并清除线程的中断状态。 - Waiting threads are signalled in FIFO order.
等待线程按FIFO顺序发出信号。 - 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.
从等待方法返回的线程的锁重获顺序与最初获取锁的线程相同(在默认情况下未指定),但对于公平锁,优先使用那些等待时间最长的线程。
- Specified by:
newCondition
in interfaceLock
- Returns:
- the Condition object
- If this lock is not held when any of the
-
getHoldCount
public int getHoldCount()
Queries the number of holds on this lock by the current thread.
查询当前线程持有此锁的次数。
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(); } } }
- Returns:
- the number of holds on this lock by the current thread, or zero if this lock is not held by the current thread
当前线程持有此锁的次数,如果当前线程不持有此锁,则为零
-
isHeldByCurrentThread
public boolean isHeldByCurrentThread()
Queries if this lock is held by the current thread.
查询此锁是否由当前线程持有。
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(); } } }
- Returns:
true
if current thread holds this lock andfalse
otherwise
如果当前线程持有此锁,则为真,否则为假
-
isLocked
public boolean isLocked()
Queries if this lock is held by any thread. This method is designed for use in monitoring of the system state, not for synchronization control.
查询此锁是否由任何线程持有。此方法设计用于监视系统状态,而不是用于同步控制。- Returns:
true
if any thread holds this lock andfalse
otherwise
如果任何线程持有此锁,则为真,否则为假
-
isFair
public final boolean isFair()
Returnstrue
if this lock has fairness set true.
如果此锁的公平设置为真,则返回真。- Returns:
true
if this lock has fairness set true
如果此锁的公平设置为真,则为真
-
getOwner
protected Thread getOwner()
Returns the thread that currently owns this lock, ornull
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 momentarilynull
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。当非所有者的线程调用此方法时,返回值反映当前锁状态的最佳状态近似值。例如,所有者可能暂时为空,即使有线程试图获取锁,但尚未这样做。此方法的目的是为了方便构造提供更广泛的锁监视设施的子类。- Returns:
- the owner, or
null
if not owned
所有者,如果不拥有则为空
-
hasQueuedThreads
public final boolean hasQueuedThreads()
Queries whether any threads are waiting to acquire this lock.
Note that because cancellations may occur at any time, atrue
return does not guarantee that any other thread will ever acquire this lock.
This method is designed primarily for use in monitoring of the system state.
查询是否有线程正在等待获取此锁。
注意,因为取消可能在任何时候发生,一个真正的返回并不保证任何其他线程将获得这个锁。
该方法主要用于监控系统状态。- Returns:
true
if there may be other threads waiting to acquire the lock
如果可能有其他线程等待获取锁,则为真
-
hasQueuedThread
public final boolean hasQueuedThread(Thread thread)
Queries whether the given thread is waiting to acquire this lock. Note that because cancellations may occur at any time, atrue
return does not guarantee that this thread will ever acquire this lock.
This method is designed primarily for use in monitoring of the system state.
查询给定线程是否正在等待获取此锁。
注意,因为取消可能随时发生,所以真正的返回并不保证这个线程将获得这个锁。该方法主要用于监控系统状态。- Parameters:
thread
- the thread- Returns:
true
if the given thread is queued waiting for this lock
如果给定线程正在排队等待此锁,则为真- Throws:
NullPointerException
- if the thread is null 如果线程为空
-
getQueueLength
public final int getQueueLength()
Returns an estimate of the number of threads waiting to acquire this lock.
The value is only an estimate because the number of threads may change dynamically while this method traverses internal data structures.
This method is designed for use in monitoring of the system state, not for synchronization control.
返回等待获取此锁的线程数量的估计值。
这个值只是一个估计值,因为当这个方法遍历内部数据结构时,线程的数量可能会动态变化。
此方法设计用于监视系统状态,而不是用于同步控制。- Returns:
- the estimated number of threads waiting for this lock
等待此锁的线程的估计数目
-
getQueuedThreads
protected Collection<Thread> getQueuedThreads()
Returns a collection containing threads that may be waiting to acquire this lock. Because the actual set of threads may change dynamically while constructing this result, the returned collection is only a best-effort estimate. The elements of the returned collection are in no particular order. This method is designed to facilitate construction of subclasses that provide more extensive monitoring facilities.
返回一个包含可能正在等待获取此锁的线程的集合。因为在构造这个结果时,实际的线程集可能会动态变化,所以返回的集合只是一个最佳效果的估计。返回集合的元素没有特定的顺序。这种方法的目的是为了方便构建提供更广泛的监视设施的子类。- Returns:
- the collection of threads 线程集合
-
hasWaiters
public boolean hasWaiters(Condition condition)
Queries whether any threads are waiting on the given condition associated with this lock.
Note that because timeouts and interrupts may occur at any time, atrue
return does not guarantee that a futuresignal
will awaken any threads.
This method is designed primarily for use in monitoring of the system state.
查询是否有线程正在等待与此锁关联的给定条件。
注意,因为超时和中断可能在任何时候发生,所以真正的返回并不保证将来的信号将唤醒任何线程。
该方法主要用于监控系统状态。- Parameters:
condition
- the condition- Returns:
true
if there are any waiting threads
如果有任何正在等待的线程,则为true- Throws:
IllegalMonitorStateException
- if this lock is not held如果这个锁没有被持有IllegalArgumentException
- if the given condition is not associated with this lock如果给定的条件与此锁没有关联NullPointerException
- if the condition is null 如果条件为空
-
getWaitQueueLength
public int getWaitQueueLength(Condition condition)
Returns an estimate of the number of threads waiting on the given condition associated with this lock.
Note that because timeouts and interrupts may occur at any time, the estimate serves only as an upper bound on the actual number of waiters.
This method is designed for use in monitoring of the system state, not for synchronization control.
返回与此锁关联的给定条件下等待的线程数的估计值。
请注意,由于超时和中断可能随时发生,因此估计值仅作为实际等待者数量的上限。
此方法设计用于监视系统状态,而不是用于同步控制。- Parameters:
condition
- the condition- Returns:
- the estimated number of waiting threads
等待线程的估计数目 - Throws:
IllegalMonitorStateException
- if this lock is not held如果这个锁没有被持有IllegalArgumentException
- if the given condition is not associated with this lock如果给定的条件与此锁没有关联NullPointerException
- if the condition is null 如果条件为空
-
getWaitingThreads
protected Collection<Thread> getWaitingThreads(Condition condition)
Returns a collection containing those threads that may be waiting on the given condition associated with this lock.
Because the actual set of threads may change dynamically while constructing this result, the returned collection is only a best-effort estimate.
The elements of the returned collection are in no particular order.
This method is designed to facilitate construction of subclasses that provide more extensive condition monitoring facilities.
返回一个集合,其中包含可能正在等待与此锁关联的给定条件的线程。
因为在构造这个结果时,实际的线程集可能会动态变化,所以返回的集合只是一个最佳效果的估计。
返回集合的元素没有特定的顺序。
这种方法的目的是为了方便构建提供更广泛的状态监视设施的子类。- Parameters:
condition
- the condition- Returns:
- the collection of threads线程集合
- Throws:
IllegalMonitorStateException
- if this lock is not held如果这个锁没有被持有IllegalArgumentException
- if the given condition is not associated with this lock如果给定的条件与此锁没有关联NullPointerException
- if the condition is null 如果条件为空
-
toString
public String toString()
Returns a string identifying this lock, as well as its lock state.
The state, in brackets, includes either the String"Unlocked"
or the String"Locked by"
followed by the name of the owning thread.
返回标识此锁的字符串及其锁状态。
括号中的状态包括字符串“解锁”或字符串“Locked by”,后跟所属线程的名称。