Most visited

Recently visited

Added in API level 9

AbstractQueuedLongSynchronizer

public abstract class AbstractQueuedLongSynchronizer
extends AbstractOwnableSynchronizer implements Serializable

java.lang.Object
   ↳ java.util.concurrent.locks.AbstractOwnableSynchronizer
     ↳ java.util.concurrent.locks.AbstractQueuedLongSynchronizer


版本为AbstractQueuedSynchronizer ,其中同步状态保持为long 该类具有与AbstractQueuedSynchronizer完全相同的结构,属性和方法,但所有与状态相关的参数和结果都定义为long而不是int 创建需要64位状态的同步器(如多级锁和屏障)时,此类可能非常有用。

有关使用说明和示例,请参阅 AbstractQueuedSynchronizer

Summary

Nested classes

class AbstractQueuedLongSynchronizer.ConditionObject

AbstractQueuedLongSynchronizer条件实现作为Lock实现的基础。

Protected constructors

AbstractQueuedLongSynchronizer()

创建一个初始同步状态为零的新实例 AbstractQueuedLongSynchronizer

Public methods

final void acquire(long arg)

以独占模式获取,忽略中断。

final void acquireInterruptibly(long arg)

以独占模式获取,如果中断则中止。

final void acquireShared(long arg)

采用共享模式,忽略中断。

final void acquireSharedInterruptibly(long arg)

在共享模式下获取,如果中断则中止。

final Collection<Thread> getExclusiveQueuedThreads()

返回包含可能正在等待以独占模式获取的线程的集合。

final Thread getFirstQueuedThread()

返回队列中的第一个(最长等待)线程,如果当前没有线程正在排队,则 null

final int getQueueLength()

返回等待获取的线程数的估计值。

final Collection<Thread> getQueuedThreads()

返回包含可能正在等待获取的线程的集合。

final Collection<Thread> getSharedQueuedThreads()

返回包含可能正在等待以共享模式获取的线程的集合。

final int getWaitQueueLength(AbstractQueuedLongSynchronizer.ConditionObject condition)

返回等待与此同步器关联的给定条件的线程数的估计值。

final Collection<Thread> getWaitingThreads(AbstractQueuedLongSynchronizer.ConditionObject condition)

返回包含那些可能正在等待与此同步器关联的给定条件的线程的集合。

final boolean hasContended()

查询是否有线程曾争夺过这个同步器; 也就是说,如果一种获取方法被阻止。

final boolean hasQueuedPredecessors()

查询是否有任何线程等待获取比当前线程更长的时间。

final boolean hasQueuedThreads()

查询是否有线程正在等待获取。

final boolean hasWaiters(AbstractQueuedLongSynchronizer.ConditionObject condition)

查询是否有线程正在等待与此同步器关联的给定条件。

final boolean isQueued(Thread thread)

如果给定线程当前正在排队,则返回true。

final boolean owns(AbstractQueuedLongSynchronizer.ConditionObject condition)

查询给定的ConditionObject是否使用此同步器作为其锁。

final boolean release(long arg)

以独占模式发布。

final boolean releaseShared(long arg)

以共享模式发布。

String toString()

返回标识此同步器的字符串及其状态。

final boolean tryAcquireNanos(long arg, long nanosTimeout)

尝试以独占模式获取,如果中断则中止,如果超时,则失败。

final boolean tryAcquireSharedNanos(long arg, long nanosTimeout)

尝试以共享模式获取,如果中断则中止,如果超时,则失败。

Protected methods

final boolean compareAndSetState(long expect, long update)

如果当前状态值等于期望值,则将同步状态按原子级设置为给定的更新值。

final long getState()

返回同步状态的当前值。

boolean isHeldExclusively()

如果同步仅针对当前(调用)线程进行 true则返回 true

final void setState(long newState)

设置同步状态的值。

boolean tryAcquire(long arg)

尝试以独占模式获取。

long tryAcquireShared(long arg)

尝试以共享模式获取。

boolean tryRelease(long arg)

尝试设置状态以反映独占模式下的发行版。

boolean tryReleaseShared(long arg)

尝试设置状态以反映共享模式下的发布。

Inherited methods

From class java.util.concurrent.locks.AbstractOwnableSynchronizer
From class java.lang.Object

Protected constructors

AbstractQueuedLongSynchronizer

Added in API level 9
AbstractQueuedLongSynchronizer ()

创建一个初始同步状态为零的新实例 AbstractQueuedLongSynchronizer

Public methods

acquire

Added in API level 9
void acquire (long arg)

以独占模式获取,忽略中断。 通过调用至少一次tryAcquire(long) ,成功返回。 否则,线程排队,可能重复阻塞和解除阻塞,直到成功为止,调用tryAcquire(long) 此方法可用于实施方法lock()

Parameters
arg long: the acquire argument. This value is conveyed to tryAcquire(long) but is otherwise uninterpreted and can represent anything you like.

acquireInterruptibly

Added in API level 9
void acquireInterruptibly (long arg)

以独占模式获取,如果中断则中止。 通过首先检查中断状态来实现,然后调用至少一次tryAcquire(long) ,成功返回。 否则,线程排队,可能重复阻塞和解除阻塞,调用tryAcquire(long)直到成功或线程中断。 该方法可用于实施方法lockInterruptibly()

Parameters
arg long: the acquire argument. This value is conveyed to tryAcquire(long) but is otherwise uninterpreted and can represent anything you like.
Throws
InterruptedException if the current thread is interrupted

acquireShared

Added in API level 9
void acquireShared (long arg)

采用共享模式,忽略中断。 通过首先调用至少一次tryAcquireShared(long) ,返回成功。 否则,线程排队,可能重复阻塞并解除阻塞,调用tryAcquireShared(long)直到成功。

Parameters
arg long: the acquire argument. This value is conveyed to tryAcquireShared(long) but is otherwise uninterpreted and can represent anything you like.

acquireSharedInterruptibly

Added in API level 9
void acquireSharedInterruptibly (long arg)

在共享模式下获取,如果中断则中止。 首先检查中断状态,然后调用至少一次tryAcquireShared(long) ,成功返回。 否则,线程排队,可能重复阻塞和解除阻塞,调用tryAcquireShared(long)直到成功或线程中断。

Parameters
arg long: the acquire argument. This value is conveyed to tryAcquireShared(long) but is otherwise uninterpreted and can represent anything you like.
Throws
InterruptedException if the current thread is interrupted

getExclusiveQueuedThreads

Added in API level 9
Collection<Thread> getExclusiveQueuedThreads ()

返回包含可能正在等待以独占模式获取的线程的集合。 它具有与getQueuedThreads()相同的属性,只是它仅返回由于独占获取而等待的那些线程。

Returns
Collection<Thread> the collection of threads

getFirstQueuedThread

Added in API level 9
Thread getFirstQueuedThread ()

返回队列中的第一个(等待时间最长的)线程,如果当前没有线程正在排队,则 null

在这个实现中,这个操作通常在不变的时间内返回,但是如果其他线程正在并发地修改队列,它可能会在争用时迭代。

Returns
Thread the first (longest-waiting) thread in the queue, or null if no threads are currently queued

getQueueLength

Added in API level 9
int getQueueLength ()

返回等待获取的线程数的估计值。 该值仅为估计值,因为在此方法遍历内部数据结构时,线程数可能会动态变化。 此方法设计用于监视系统状态,而不是用于同步控制。

Returns
int the estimated number of threads waiting to acquire

getQueuedThreads

Added in API level 9
Collection<Thread> getQueuedThreads ()

返回包含可能正在等待获取的线程的集合。 因为实际的一组线程可能会在构造这个结果时动态地改变,所以返回的集合只是一个尽力而为的估计。 返回的集合的元素没有特定的顺序。 该方法旨在促进提供更广泛监测设施的子类的构建。

Returns
Collection<Thread> the collection of threads

getSharedQueuedThreads

Added in API level 9
Collection<Thread> getSharedQueuedThreads ()

返回包含可能正在等待以共享模式获取的线程的集合。 它具有与getQueuedThreads()相同的属性,只是它只返回由于共享获取而等待的那些线程。

Returns
Collection<Thread> the collection of threads

getWaitQueueLength

Added in API level 9
int getWaitQueueLength (AbstractQueuedLongSynchronizer.ConditionObject condition)

返回等待与此同步器关联的给定条件的线程数的估计值。 请注意,因为超时和中断可能随时发生,所以估计仅作为服务员实际数量的上限。 此方法设计用于监视系统状态,而不是用于同步控制。

Parameters
condition AbstractQueuedLongSynchronizer.ConditionObject: the condition
Returns
int the estimated number of waiting threads
Throws
IllegalMonitorStateException if exclusive synchronization is not held
IllegalArgumentException if the given condition is not associated with this synchronizer
NullPointerException if the condition is null

getWaitingThreads

Added in API level 9
Collection<Thread> getWaitingThreads (AbstractQueuedLongSynchronizer.ConditionObject condition)

返回包含那些可能正在等待与此同步器关联的给定条件的线程的集合。 因为实际的一组线程可能会在构造这个结果时动态地改变,所以返回的集合只是一个尽力而为的估计。 返回的集合的元素没有特定的顺序。

Parameters
condition AbstractQueuedLongSynchronizer.ConditionObject: the condition
Returns
Collection<Thread> the collection of threads
Throws
IllegalMonitorStateException if exclusive synchronization is not held
IllegalArgumentException if the given condition is not associated with this synchronizer
NullPointerException if the condition is null

hasContended

Added in API level 9
boolean hasContended ()

查询是否有线程曾争夺过这个同步器; 也就是说,如果一种获取方法被阻止。

在这个实现中,这个操作在一段时间内返回。

Returns
boolean true if there has ever been contention

hasQueuedPredecessors

Added in API level 21
boolean hasQueuedPredecessors ()

查询是否有任何线程等待获取比当前线程更长的时间。

此方法的调用等效于(但可能更有效):

 getFirstQueuedThread() != Thread.currentThread()
   && hasQueuedThreads()

请注意,因为中断和超时导致的取消可能随时发生,所以true返回不能保证其他线程将在当前线程之前获取。 同样,由于队列是空的,在此方法返回false之后,另一个线程有可能赢得比赛排队。

此方法旨在供公平同步器使用,以避免barging 这种同步的tryAcquire(long)方法应该返回false ,其tryAcquireShared(long)方法应该返回一个负值,如果这个方法返回true (除非这是一个折返获取)。 例如,用于公平,可重入,独占模式同步器的tryAcquire方法可能如下所示:

 protected boolean tryAcquire(int arg) {
   if (isHeldExclusively()) {
     // A reentrant acquire; increment hold count
     return true;
   } else if (hasQueuedPredecessors()) {
     return false;
   } else {
     // try to acquire normally
   }
 }

Returns
boolean true if there is a queued thread preceding the current thread, and false if the current thread is at the head of the queue or the queue is empty

hasQueuedThreads

Added in API level 9
boolean hasQueuedThreads ()

查询是否有线程正在等待获取。 请注意,由于中断和超时导致的取消可能随时发生, true返回并不能保证任何其他线程都会获取。

在这个实现中,这个操作在一段时间内返回。

Returns
boolean true if there may be other threads waiting to acquire

hasWaiters

Added in API level 9
boolean hasWaiters (AbstractQueuedLongSynchronizer.ConditionObject condition)

查询是否有线程正在等待与此同步器关联的给定条件。 请注意,因为超时和中断可能随时发生,所以true返回不能保证将来signal将唤醒任何线程。 此方法主要用于监视系统状态。

Parameters
condition AbstractQueuedLongSynchronizer.ConditionObject: the condition
Returns
boolean true if there are any waiting threads
Throws
IllegalMonitorStateException if exclusive synchronization is not held
IllegalArgumentException if the given condition is not associated with this synchronizer
NullPointerException if the condition is null

isQueued

Added in API level 9
boolean isQueued (Thread thread)

如果给定线程当前正在排队,则返回true。

该实现遍历队列以确定给定线程的存在。

Parameters
thread Thread: the thread
Returns
boolean true if the given thread is on the queue
Throws
NullPointerException if the thread is null

owns

Added in API level 9
boolean owns (AbstractQueuedLongSynchronizer.ConditionObject condition)

查询给定的ConditionObject是否使用此同步器作为其锁。

Parameters
condition AbstractQueuedLongSynchronizer.ConditionObject: the condition
Returns
boolean true if owned
Throws
NullPointerException if the condition is null

release

Added in API level 9
boolean release (long arg)

以独占模式发布。 如果tryRelease(long)返回true,则通过解锁一个或多个线程tryRelease(long)实现。 此方法可用于实施方法unlock()

Parameters
arg long: the release argument. This value is conveyed to tryRelease(long) but is otherwise uninterpreted and can represent anything you like.
Returns
boolean the value returned from tryRelease(long)

releaseShared

Added in API level 9
boolean releaseShared (long arg)

以共享模式发布。 如果tryReleaseShared(long)返回true,则通过解锁一个或多个线程tryReleaseShared(long)实现。

Parameters
arg long: the release argument. This value is conveyed to tryReleaseShared(long) but is otherwise uninterpreted and can represent anything you like.
Returns
boolean the value returned from tryReleaseShared(long)

toString

Added in API level 9
String toString ()

返回标识此同步器的字符串及其状态。 括号中的状态包括字符串"State ="后跟当前值getState() ,以及"nonempty""empty"具体取决于队列是否为空。

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

tryAcquireNanos

Added in API level 9
boolean tryAcquireNanos (long arg, 
                long nanosTimeout)

尝试以独占模式获取,如果中断则中止,如果超时,则失败。 通过首先检查中断状态来实现,然后调用至少一次tryAcquire(long) ,成功返回。 否则,线程排队,可能重复阻塞和解除阻塞,调用tryAcquire(long)直到成功或线程中断或超时。 此方法可用于实施方法tryLock(long, TimeUnit)

Parameters
arg long: the acquire argument. This value is conveyed to tryAcquire(long) but is otherwise uninterpreted and can represent anything you like.
nanosTimeout long: the maximum number of nanoseconds to wait
Returns
boolean true if acquired; false if timed out
Throws
InterruptedException if the current thread is interrupted

tryAcquireSharedNanos

Added in API level 9
boolean tryAcquireSharedNanos (long arg, 
                long nanosTimeout)

尝试以共享模式获取,如果中断则中止,如果超时,则失败。 通过首先检查中断状态来实现,然后调用至少一次tryAcquireShared(long) ,成功返回。 否则,线程排队,可能重复阻塞和解除阻塞,调用tryAcquireShared(long)直到成功或线程中断或超时。

Parameters
arg long: the acquire argument. This value is conveyed to tryAcquireShared(long) but is otherwise uninterpreted and can represent anything you like.
nanosTimeout long: the maximum number of nanoseconds to wait
Returns
boolean true if acquired; false if timed out
Throws
InterruptedException if the current thread is interrupted

Protected methods

compareAndSetState

Added in API level 9
boolean compareAndSetState (long expect, 
                long update)

如果当前状态值等于期望值,则将同步状态按原子级设置为给定的更新值。 该操作具有volatile读写的内存语义。

Parameters
expect long: the expected value
update long: the new value
Returns
boolean true if successful. False return indicates that the actual value was not equal to the expected value.

getState

Added in API level 9
long getState ()

返回同步状态的当前值。 此操作具有volatile读取的内存语义。

Returns
long current state value

isHeldExclusively

Added in API level 9
boolean isHeldExclusively ()

如果同步仅针对当前(调用)线程进行true则返回true 每次调用非等待方法时调用AbstractQueuedLongSynchronizer.ConditionObject方法。 (等待方法改为调用release(long)

默认实现抛出UnsupportedOperationException 该方法仅在AbstractQueuedLongSynchronizer.ConditionObject方法内部调用,因此如果不使用条件,则不需要定义该方法。

Returns
boolean true if synchronization is held exclusively; false otherwise
Throws
UnsupportedOperationException if conditions are not supported

setState

Added in API level 9
void setState (long newState)

设置同步状态的值。 此操作具有volatile写入的存储器语义。

Parameters
newState long: the new state value

tryAcquire

Added in API level 9
boolean tryAcquire (long arg)

尝试以独占模式获取。 该方法应该查询对象的状态是否允许在独占模式下获取它,如果是,则获取它。

此方法总是由执行获取的线程调用。 如果此方法报告失败,则获取方法可以将该线程排队(如果该线程尚未排队),直到通过来自其他某个线程的发布发出信号。 这可以用来实现方法tryLock()

默认实现抛出 UnsupportedOperationException

Parameters
arg long: the acquire argument. This value is always the one passed to an acquire method, or is the value saved on entry to a condition wait. The value is otherwise uninterpreted and can represent anything you like.
Returns
boolean true if successful. Upon success, this object has been acquired.
Throws
IllegalMonitorStateException if acquiring would place this synchronizer in an illegal state. This exception must be thrown in a consistent fashion for synchronization to work correctly.
UnsupportedOperationException if exclusive mode is not supported

tryAcquireShared

Added in API level 9
long tryAcquireShared (long arg)

尝试以共享模式获取。 此方法应查询对象的状态是否允许在共享模式下获取它,如果是,则获取它。

此方法总是由执行获取的线程调用。 如果此方法报告失败,则获取方法可以将该线程排队(如果该线程尚未排队),直到通过来自其他某个线程的发布发出信号。

默认实现抛出 UnsupportedOperationException

Parameters
arg long: the acquire argument. This value is always the one passed to an acquire method, or is the value saved on entry to a condition wait. The value is otherwise uninterpreted and can represent anything you like.
Returns
long a negative value on failure; zero if acquisition in shared mode succeeded but no subsequent shared-mode acquire can succeed; and a positive value if acquisition in shared mode succeeded and subsequent shared-mode acquires might also succeed, in which case a subsequent waiting thread must check availability. (Support for three different return values enables this method to be used in contexts where acquires only sometimes act exclusively.) Upon success, this object has been acquired.
Throws
IllegalMonitorStateException if acquiring would place this synchronizer in an illegal state. This exception must be thrown in a consistent fashion for synchronization to work correctly.
UnsupportedOperationException if shared mode is not supported

tryRelease

Added in API level 9
boolean tryRelease (long arg)

尝试设置状态以反映独占模式下的发行版。

此方法始终由执行发布的线程调用。

默认实现抛出 UnsupportedOperationException

Parameters
arg long: the release argument. This value is always the one passed to a release method, or the current state value upon entry to a condition wait. The value is otherwise uninterpreted and can represent anything you like.
Returns
boolean true if this object is now in a fully released state, so that any waiting threads may attempt to acquire; and false otherwise.
Throws
IllegalMonitorStateException if releasing would place this synchronizer in an illegal state. This exception must be thrown in a consistent fashion for synchronization to work correctly.
UnsupportedOperationException if exclusive mode is not supported

tryReleaseShared

Added in API level 9
boolean tryReleaseShared (long arg)

尝试设置状态以反映共享模式下的发布。

此方法始终由执行发布的线程调用。

默认实现抛出 UnsupportedOperationException

Parameters
arg long: the release argument. This value is always the one passed to a release method, or the current state value upon entry to a condition wait. The value is otherwise uninterpreted and can represent anything you like.
Returns
boolean true if this release of shared mode may permit a waiting acquire (shared or exclusive) to succeed; and false otherwise
Throws
IllegalMonitorStateException if releasing would place this synchronizer in an illegal state. This exception must be thrown in a consistent fashion for synchronization to work correctly.
UnsupportedOperationException if shared mode is not supported

Hooray!