public abstract class AbstractQueuedSynchronizer extends AbstractOwnableSynchronizer implements Serializable
int值来表示状态同步器种类最多的一种有用的基础。子类必须定义改变这种状态的保护方法,并且它定义了这个被获取或释放的对象的状态意味着什么。鉴于这些,在这个类中的其他方法进行所有的排队和阻塞力学。子类可以维护其他国家的领域,但只有自动更新
int值操作使用方法
getState(),
setState(int)和
compareAndSetState(int, int)相对于同步跟踪。
子类应该被定义为用于实现它们的封闭类的同步属性的非公开的内部辅助类。AbstractQueuedSynchronizer类没有实现任何同步接口。相反,它定义了方法如acquireInterruptibly(int)可以调用适当的混凝土锁和相关同步器来实现他们的公共方法。
该类支持任何一个或两个默认的独占模式和共享模式。当在独占模式下获得的,试图通过其他线程获取不能成功。共享模式通过多个线程获取(但不需要)成功。这个类不“理解”这些差异,除非在机械意义上,当一个共享模式获得成功,下一个等待线程(如果存在),也必须确定它是否可以获得以及。在不同的模式,共享同一个FIFO队列的线程。通常,实现子类只支持其中的一个模式,但都可以发挥作用,例如在ReadWriteLock。仅支持独占或唯一共享模式的子类不需要定义支持未使用的模式的方法。
这个类定义了一个嵌套的AbstractQueuedSynchronizer.ConditionObject类的子类可以作为支撑的方法isHeldExclusively()报告是否同步是相对于当前线程持有一个Condition独家独占模式实施方法release(int)调用当前getState()价值充分释放该对象,并acquire(int),鉴于这种保存的状态值,最终恢复该对象先前获得的状态。没有AbstractQueuedSynchronizer方法否则创造了这样的条件,所以如果这个约束条件无法满足,不使用它。对AbstractQueuedSynchronizer.ConditionObject行为当然取决于其同步实现的语义。
这个类提供了内部队列的检查、检测和监视方法,以及状态对象的类似方法。这些可以被导出为所需的类使用一个AbstractQueuedSynchronizer的同步机制。
这类的序列化存储只有底层原子整数维持状态,所以反序列化的对象有空线程队列。典型的子类需要串行将定义一个readObject方法恢复到一个已知的初始状态,这在反序列化。
作为一个同步的基础上这类,定义以下方法,如适用,通过检查和/或修改使用getState()同步状态,setState(int)和/或compareAndSetState(int, int):
UnsupportedOperationException。这些方法的实现必须是内部线程安全的,而且一般应该是短而不是块。定义这些方法是使用这个类的唯一支持的方法。所有其他的方法都是因为他们不能独立变化的声明
final。
你也可能从AbstractOwnableSynchronizer有用跟踪线程拥有独家同步找到继承的方法。鼓励您使用它们-这使监测和诊断工具,以帮助用户确定线程持有锁。
尽管这类是基于内部FIFO队列,它不会自动执行先进先出的收购政策。独占同步的核心是形式:
获得:而(!tryacquire(Arg)){队列的线程不是排队;可能块当前线程;}释放:如果(tryrelease(Arg))打开第一个排队的线程;(共享模式类似,但可能涉及级联信号。)
因为支票在获得之前调用入队,新获取线程可能驳船超过别人,堵塞和排队。然而,你可以,如果需要,定义tryAcquire和/或tryAcquireShared禁用闯入由内部调用一个或一个以上的检查方法,从而提供一个公平的FIFO的习得顺序。特别是,最公平的同步器可以定义tryAcquire如果hasQueuedPredecessors()返回false(一种专门设计的公平回报true同步器使用)等的变化是可能的。
吞吐量和可扩展性,一般最高默认驳运(也被称为贪婪,放弃,和车队避免)策略。虽然这不能保证公平或无饥饿,早排队的线程被允许recontend后来排队的线程,每个recontention已成功抗击来袭的线程一个公正的机会。另外,虽然取得了不“旋转”在通常意义上,他们可能会进行多次调用tryAcquire穿插在阻止其他的计算。这给大多数自旋的好处时,独家同步只简单举行,而大部分的债务时,它不是。如果需要的话,你可以增加这种由以前的电话与“快速通道”检查获取的方法,可能hasContended()预检查和/或hasQueuedThreads()只有这么做如果同步器可能不争辩。
这个类提供了一种有效的通过专门的使用同步器,可以依靠int状态,获得范围同步部分基础和可扩展性,并释放参数,以及一个内部FIFO排队等待。当这并不足够,你可以从一个较低的水平,以atomic类建立同步,您自己的自定义Queue类,和LockSupport阻断支持。
这是一个不可重入的互斥锁类使用零代表解锁状态的价值,一个代表锁定状态。而不可重入锁并不严格要求的当前所有者线程记录,这类不那么让使用更容易监控。它也支持条件和公开的仪器方法之一:
class Mutex implements Lock, java.io.Serializable {
// Our internal helper class
private static class Sync extends AbstractQueuedSynchronizer {
// Reports whether in locked state
protected boolean isHeldExclusively() {
return getState() == 1;
}
// Acquires the lock if state is zero
public boolean tryAcquire(int acquires) {
assert acquires == 1; // Otherwise unused
if (compareAndSetState(0, 1)) {
setExclusiveOwnerThread(Thread.currentThread());
return true;
}
return false;
}
// Releases the lock by setting state to zero
protected boolean tryRelease(int releases) {
assert releases == 1; // Otherwise unused
if (getState() == 0) throw new IllegalMonitorStateException();
setExclusiveOwnerThread(null);
setState(0);
return true;
}
// Provides a Condition
Condition newCondition() { return new ConditionObject(); }
// Deserializes properly
private void readObject(ObjectInputStream s)
throws IOException, ClassNotFoundException {
s.defaultReadObject();
setState(0); // reset to unlocked state
}
}
// The sync object does all the hard work. We just forward to it.
private final Sync sync = new Sync();
public void lock() { sync.acquire(1); }
public boolean tryLock() { return sync.tryAcquire(1); }
public void unlock() { sync.release(1); }
public Condition newCondition() { return sync.newCondition(); }
public boolean isLocked() { return sync.isHeldExclusively(); }
public boolean hasQueuedThreads() { return sync.hasQueuedThreads(); }
public void lockInterruptibly() throws InterruptedException {
sync.acquireInterruptibly(1);
}
public boolean tryLock(long timeout, TimeUnit unit)
throws InterruptedException {
return sync.tryAcquireNanos(1, unit.toNanos(timeout));
}
}
这里是一个闩锁类,就像一CountDownLatch除了它只需要一个单一的signal火。因为一个锁存器是非独家的,它使用的shared获取和释放的方法。
class BooleanLatch {
private static class Sync extends AbstractQueuedSynchronizer {
boolean isSignalled() { return getState() != 0; }
protected int tryAcquireShared(int ignore) {
return isSignalled() ? 1 : -1;
}
protected boolean tryReleaseShared(int ignore) {
setState(1);
return true;
}
}
private final Sync sync = new Sync();
public boolean isSignalled() { return sync.isSignalled(); }
public void signal() { sync.releaseShared(1); }
public void await() throws InterruptedException {
sync.acquireSharedInterruptibly(1);
}
}
| Modifier and Type | Class and Description |
|---|---|
class |
AbstractQueuedSynchronizer.ConditionObject
一
AbstractQueuedSynchronizer作为一个
Lock实施的基础条件的实现。
|
| Modifier | Constructor and Description |
|---|---|
protected |
AbstractQueuedSynchronizer()
创建一个新的具有零初始同步状态
AbstractQueuedSynchronizer实例。
|
| Modifier and Type | Method and Description |
|---|---|
void |
acquire(int arg)
在独占模式中获得,忽略中断。
|
void |
acquireInterruptibly(int arg)
获取独占模式,如果中断中止。
|
void |
acquireShared(int arg)
在共享模式中获取,忽略中断。
|
void |
acquireSharedInterruptibly(int arg)
获得在共享模式下,如果中断中止。
|
protected boolean |
compareAndSetState(int expect, int update)
自动设置同步状态的更新后的值,如果当前状态值等于期望值。
|
Collection<Thread> |
getExclusiveQueuedThreads()
返回一个包含可能等待在独占模式中获得的线程的集合。
|
Thread |
getFirstQueuedThread()
返回第一个(最长的等待队列中的线程),或
null如果没有线程当前排队。
|
Collection<Thread> |
getQueuedThreads()
返回一个包含可能等待获取的线程的集合。
|
int |
getQueueLength()
返回等待获取的线程数的估计值。
|
Collection<Thread> |
getSharedQueuedThreads()
返回一个包含可能等待在共享模式中获取的线程的集合。
|
protected int |
getState()
返回同步状态的当前值。
|
Collection<Thread> |
getWaitingThreads(AbstractQueuedSynchronizer.ConditionObject condition)
返回一个集合,可以在给定的条件下,与此相关的线程同步。
|
int |
getWaitQueueLength(AbstractQueuedSynchronizer.ConditionObject condition)
返回一个估计的线程数在给定的条件下,与此同步关联。
|
boolean |
hasContended()
查询是否有线程所主张获得同步;如果是一个获取方法曾受阻。
|
boolean |
hasQueuedPredecessors()
查询是否有任何线程等待获取比当前线程更长的时间。
|
boolean |
hasQueuedThreads()
查询是否有任何线程等待获取。
|
boolean |
hasWaiters(AbstractQueuedSynchronizer.ConditionObject condition)
查询是否有线程等待在给定的条件下,与此同步关联。
|
protected boolean |
isHeldExclusively()
返回
true如果同步举行专门就当前线程(调用)。
|
boolean |
isQueued(Thread thread)
如果给定线程当前正在排队,则返回真。
|
boolean |
owns(AbstractQueuedSynchronizer.ConditionObject condition)
查询是否给定的conditionobject使用同步器的锁。
|
boolean |
release(int arg)
独家发布模式。
|
boolean |
releaseShared(int arg)
共享模式中的版本。
|
protected void |
setState(int newState)
设置同步状态的值。
|
String |
toString()
返回一个字符串标识此同步器,以及其状态。
|
protected boolean |
tryAcquire(int arg)
尝试在独占模式下获得。
|
boolean |
tryAcquireNanos(int arg, long nanosTimeout)
试图在独占模式下获得的,如果不中止中断,如果给定的超时时间流逝。
|
protected int |
tryAcquireShared(int arg)
尝试在共享模式中获得。
|
boolean |
tryAcquireSharedNanos(int arg, long nanosTimeout)
试图在共享模式下获得的,如果不中止中断,如果给定的超时时间流逝。
|
protected boolean |
tryRelease(int arg)
试图设置状态以反映独占模式中的释放。
|
protected boolean |
tryReleaseShared(int arg)
试图设置状态以反映共享模式中的释放。
|
getExclusiveOwnerThread, setExclusiveOwnerThreadprotected AbstractQueuedSynchronizer()
AbstractQueuedSynchronizer实例。
protected final int getState()
volatile内存语义读。
protected final void setState(int newState)
volatile内存语义写。
newState -新的状态值
protected final boolean compareAndSetState(int expect,
int update)
volatile读写的内存语义。
expect -期望值
update -新价值
true如果成功。错误返回表示实际值不等于期望值。
protected boolean tryAcquire(int arg)
此方法总是由执行线程的线程调用的。如果此方法报告失败,则获取方法可以将该线程队列队列,如果它没有排队,直到它被另一些线程释放时发出信号。这可以用来实现方法Lock.tryLock()。
arg -获取参数。此值总是传递给获取方法的一个值,或者保存在一个条件等待的条件下的值。价值是另有解释,可以代表任何你喜欢的东西。
true如果成功。一旦成功,这个对象已被收购。
IllegalMonitorStateException如果获取将同步在非法状态。此异常必须以一致的方式被抛出,以同步正确工作。
UnsupportedOperationException如果独占模式不支持
protected boolean tryRelease(int arg)
此方法总是由线程执行释放调用。
arg -释放参数。此值总是一个传递给一个释放方法,或在进入一个条件等待时的当前状态值。价值是另有解释,可以代表任何你喜欢的东西。
true如果这个对象现在处于完全释放状态,所以任何等待的线程试图获取;否则,
false。
IllegalMonitorStateException如果释放将同步在非法状态。此异常必须以一致的方式被抛出,以同步正确工作。
UnsupportedOperationException如果独占模式不支持
protected int tryAcquireShared(int arg)
此方法总是由执行线程的线程调用的。如果此方法报告失败,则获取方法可以将该线程队列队列,如果它没有排队,直到它被另一些线程释放时发出信号。
arg -获取参数。此值总是传递给获取方法的一个值,或者保存在一个条件等待的条件下的值。价值是另有解释,可以代表任何你喜欢的东西。
IllegalMonitorStateException如果获取将同步在非法状态。此异常必须以一致的方式被抛出,以同步正确工作。
UnsupportedOperationException如果共享模式是不支持的
protected boolean tryReleaseShared(int arg)
此方法总是由线程执行释放调用。
arg -释放参数。此值总是一个传递给一个释放方法,或在进入一个条件等待时的当前状态值。价值是另有解释,可以代表任何你喜欢的东西。
true释放共享模式可以获得一等(共享或独占)成功;否则,
false
IllegalMonitorStateException如果释放将同步在非法状态。此异常必须以一致的方式被抛出,以同步正确工作。
UnsupportedOperationException如果共享模式是不支持的
protected boolean isHeldExclusively()
true如果同步举行专门就当前线程(调用)。这个方法被调用,在每次调用非等待
AbstractQueuedSynchronizer.ConditionObject方法。(等待方法调用
release(int)
默认实现将UnsupportedOperationException。这个方法调用内部只有在AbstractQueuedSynchronizer.ConditionObject方法,所以不需要被定义,如果没有使用条件。
true如果同步独家持有;
false否则
UnsupportedOperationException -如果不支持条件
public final void acquire(int arg)
tryAcquire(int)实施成功,返回。否则,线程队列,可能重复查封和解封,调用
tryAcquire(int)直到成功。这种方法可以用来实现方法
Lock.lock()。
arg -获取参数。这个值是给
tryAcquire(int)但另有解释,可以代表任何你喜欢的东西。
public final void acquireInterruptibly(int arg)
throws InterruptedException
tryAcquire(int),返回成功。否则,线程队列,可能重复查封和解封,调用
tryAcquire(int)直到成功或线程被中断。这种方法可以用来实现方法
Lock.lockInterruptibly()。
arg -获取参数。这个值是给
tryAcquire(int)但另有解释,可以代表任何你喜欢的东西。
InterruptedException -如果当前线程被中断
public final boolean tryAcquireNanos(int arg,
long nanosTimeout)
throws InterruptedException
tryAcquire(int),返回成功。否则,线程队列,可能重复查封和解封,调用
tryAcquire(int)直到成功或线程被中断或超时的逝去。这种方法可以用来实现方法
Lock.tryLock(long, TimeUnit)。
arg -获取参数。这个值是给
tryAcquire(int)但另有解释,可以代表任何你喜欢的东西。
nanosTimeout -纳秒的最大数量等
true如果获得;
false如果超时
InterruptedException -如果当前线程被中断
public final boolean release(int arg)
tryRelease(int)实现。这种方法可以用来实现方法
Lock.unlock()。
arg -释放参数。这个值是给
tryRelease(int)但另有解释,可以代表任何你喜欢的东西。
tryRelease(int)返回的值
public final void acquireShared(int arg)
tryAcquireShared(int)实施成功,返回。否则,线程队列,可能重复查封和解封,调用
tryAcquireShared(int)直到成功。
arg -获取参数。这个值是给
tryAcquireShared(int)但另有解释,可以代表任何你喜欢的东西。
public final void acquireSharedInterruptibly(int arg)
throws InterruptedException
tryAcquireShared(int),返回成功。否则,线程队列,可能重复查封和解封,调用
tryAcquireShared(int)直到成功或线程被中断。
arg -获取参数。这个值是给
tryAcquireShared(int)但另有解释,可以代表任何你喜欢的东西。
InterruptedException -如果当前线程被中断
public final boolean tryAcquireSharedNanos(int arg,
long nanosTimeout)
throws InterruptedException
tryAcquireShared(int),返回成功。否则,线程队列,可能重复查封和解封,调用
tryAcquireShared(int)直到成功或线程被中断或超时的逝去。
arg -获取参数。这个值是给
tryAcquireShared(int)但另有解释,可以代表任何你喜欢的东西。
nanosTimeout -纳秒的最大数量等
true如果获得;
false如果超时
InterruptedException -如果当前线程被中断
public final boolean releaseShared(int arg)
tryReleaseShared(int)实现。
arg -释放参数。这个值是给
tryReleaseShared(int)但另有解释,可以代表任何你喜欢的东西。
tryReleaseShared(int)返回的值
public final boolean hasQueuedThreads()
true返回不保证其他线程会获得。
在这个实现中,这种操作在恒定时间内返回。
true如果可能有其他线程等待获取
public final boolean hasContended()
在这个实现中,这种操作在恒定时间内返回。
true如果有过争
public final Thread getFirstQueuedThread()
null如果没有线程当前排队。
在这个实现中,该操作通常的回报,在固定的时间,但可以重复在争夺如果其他线程同时修改队列。
null如果没有线程当前排队
public final boolean isQueued(Thread thread)
这个实现遍历队列判断给定线程的存在。
thread -螺纹
true如果给定线程在队列中
NullPointerException -如果线程是无效的
public final boolean hasQueuedPredecessors()
这种方法的调用相当于(但可能比)更有效:
getFirstQueuedThread() != Thread.currentThread() &&
hasQueuedThreads()
注意,因为取消由于中断和超时可能发生在任何时间,一个true返回不保证其他线程将获得在当前线程。同样,经过此方法返回一个线程false赢得比赛将是可能的,因为队列是空的。
这种方法的设计是由一个公平的同步器,避免使用barging。这种同步器的tryAcquire(int)方法应该返回false,及其tryAcquireShared(int)方法应该返回一个负值,如果这个方法返回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
}
}
true如果有排队线前面的当前线程,并
false如果当前线程在队列或队列的头部是空的
public final int getQueueLength()
public final Collection<Thread> getQueuedThreads()
public final Collection<Thread> getExclusiveQueuedThreads()
getQueuedThreads()除外,它只返回那些线程等待由于独家获得。
public final Collection<Thread> getSharedQueuedThreads()
getQueuedThreads()除外,它只返回那些线程等待,由于共同的获得。
public final boolean owns(AbstractQueuedSynchronizer.ConditionObject condition)
condition -条件
true
NullPointerException -如果条件是空的
public final boolean hasWaiters(AbstractQueuedSynchronizer.ConditionObject condition)
true收益并不能保证未来的
signal将唤醒所有线程。这种方法的设计主要是用于监测系统状态。
condition -条件
true如果有任何等待的线程
IllegalMonitorStateException -如果不是独家同步举行
IllegalArgumentException -如果给定的条件是不是与此同步关联
NullPointerException -如果条件是空的
public final int getWaitQueueLength(AbstractQueuedSynchronizer.ConditionObject condition)
condition -条件
IllegalMonitorStateException -如果不是独家同步举行
IllegalArgumentException -如果给定的条件是不是与此同步关联
NullPointerException -如果条件是空的
public final Collection<Thread> getWaitingThreads(AbstractQueuedSynchronizer.ConditionObject condition)
condition -条件
IllegalMonitorStateException -如果不是独家同步举行
IllegalArgumentException -如果给定的条件是不是与此同步关联
NullPointerException -如果条件是空的
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.