Most visited

Recently visited

Added in API level 1

AbstractQueuedSynchronizer

public abstract class AbstractQueuedSynchronizer
extends AbstractOwnableSynchronizer implements Serializable

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


为实现依赖于先进先出(FIFO)等待队列的阻塞锁和相关同步器(信号量,事件等)提供框架。 这个类被设计成为依赖于单个原子int值表示状态的大多数同步器的有用基础。 子类必须定义改变此状态的受保护方法,并且定义该状态对于获取或释放此对象的含义。 鉴于这些,本课程中的其他方法将执行所有排队和阻塞机制。 子类可以保持其他状态字段,但只以原子方式更新int使用方法操纵值getState()setState(int)compareAndSetState(int, int)跟踪相对于同步。

应将子类定义为用于实现其封闭类的同步属性的非公共内部辅助类。 AbstractQueuedSynchronizer类没有实现任何同步接口。 相反,它定义了一些方法,例如acquireInterruptibly(int) ,可以通过具体的锁和相关的同步器来适当调用它们来实现它们的公共方法。

此类支持默认独占模式和共享模式中的一种或两种。 当以独占模式获取时,其他线程尝试获取不成功。 共享模式被多个线程获取可能(但不一定)成功。 除了从机械意义上讲,这个类不能“理解”这些差异,当共享模式获取成功时,下一个等待线程(如果存在的话)还必须确定它是否可以获取。 在不同模式下等待的线程共享相同的FIFO队列。 通常,实现子类只支持其中一种模式,但两者都可以在ReadWriteLock 仅支持独占模式或仅共享模式的子类不需要定义支持未使用模式的方法。

这个类定义的嵌套AbstractQueuedSynchronizer.ConditionObject ,可用于作为一类Condition由子类支持独占模式用于该方法的实施isHeldExclusively()报告是否同步排他相对于保持在当前线程,方法release(int)与当前调用getState()值完全释放此目的,和acquire(int) ,考虑到这个保存的状态值,最终将该对象恢复到其先前获取的状态。 否则AbstractQueuedSynchronizer方法会创建这样一个条件,所以如果不能满足这个约束条件,就不要使用它。 AbstractQueuedSynchronizer.ConditionObject的行为当然取决于其同步器实现的语义。

此类为内部队列提供检查,检测和监视方法,以及条件对象的类似方法。 可以使用AbstractQueuedSynchronizer作为他们的同步机制,根据需要将它们导出到类中。

这个类的序列化只存储基本的原子整数维护状态,所以反序列化的对象具有空的线程队列。 需要可串行化的典型子类将定义一个readObject方法,该方法在解串行化时将其恢复到已知的初始状态。

Usage

使用这个类用作同步的基础上,重新定义以下方法,如适用,通过检查和/或修改使用所述同步状态 getState()setState(int)和/或 compareAndSetState(int, int)

Each of these methods by default throws UnsupportedOperationException. Implementations of these methods must be internally thread-safe, and should in general be short and not block. Defining these methods is the only supported means of using this class. All other methods are declared final because they cannot be independently varied.

您也可以从AbstractOwnableSynchronizer找到继承的方法,以便跟踪拥有独占同步器的线程。 我们鼓励您使用它们 - 这使监视和诊断工具能够帮助用户确定哪些线程持有锁。

尽管此类基于内部FIFO队列,但不会自动执行FIFO采集策略。 独占同步的核心形式如下:

 Acquire:
     while (!tryAcquire(arg)) {
        enqueue thread if it is not already queued;
        possibly block current thread;
     }

 Release:
     if (tryRelease(arg))
        unblock the first queued thread;
 
(Shared mode is similar but may involve cascading signals.)

因为获取中的检查在排队之前被调用,所以新获取的线程可能先于被阻塞和排队的其他线程插入 但是,如果需要,您可以通过内部调用一个或多个检查方法来定义tryAcquire和/或tryAcquireShared禁用tryAcquireShared ,从而提供公平的 FIFO采集顺序。 特别地,最公平同步器可以定义tryAcquire返回false如果hasQueuedPredecessors() (具体地设计成由公平同步器中使用的方法)返回true 其他变化是可能的。

吞吐量和可扩展性对于默认的驳船(也称为贪婪放弃车队避免 )策略而言通常是最高的。 虽然这不能保证公平或无饥饿,但可以在稍后排队的线程之前重新排队早期排队的线程,并且每次调整都有一个无偏于的机会来抵御传入的线程。 此外,虽然获取不像通常意义上的“旋转”,但在阻塞之前,它们可能会执行多个tryAcquire调用与其他计算tryAcquire交织。 当独占同步只是简单地进行时,这提供了自旋的大部分好处,而当没有大部分的责任时,则没有大部分的责任。 如果需要的话,您可以通过前面的调用来获取这些信息,以获取具有“快速路径”检查的方法,可能需要预先检查hasContended()和/或hasQueuedThreads()以便仅在同步器可能不会被争用时才这样做。

该类为部分同步提供了一种高效的可扩展基础,部分原因是它的使用范围专门用于可依赖int状态,获取和释放参数以及内部FIFO等待队列的同步器。 如果这还不够,可以使用atomic类,自己的自定义Queue类和LockSupport阻止支持从较低级别构建同步器。

Usage Examples

这是一个不可重入的互斥锁类,它使用零值表示解锁状态,一个表示锁定状态。 尽管非重入锁并不严格要求记录当前所有者线程,但该类无论如何都会这样做,以便更易于监视使用。 它还支持条件并公开其中一种工具方法:

 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);
   }
 }

Summary

Nested classes

class AbstractQueuedSynchronizer.ConditionObject

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

Protected constructors

AbstractQueuedSynchronizer()

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

Public methods

final void acquire(int arg)

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

final void acquireInterruptibly(int arg)

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

final void acquireShared(int arg)

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

final void acquireSharedInterruptibly(int arg)

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

final Collection<Thread> getExclusiveQueuedThreads()

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

final Thread getFirstQueuedThread()

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

final int getQueueLength()

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

final Collection<Thread> getQueuedThreads()

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

final Collection<Thread> getSharedQueuedThreads()

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

final int getWaitQueueLength(AbstractQueuedSynchronizer.ConditionObject condition)

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

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

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

final boolean hasContended()

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

final boolean hasQueuedPredecessors()

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

final boolean hasQueuedThreads()

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

final boolean hasWaiters(AbstractQueuedSynchronizer.ConditionObject condition)

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

final boolean isQueued(Thread thread)

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

final boolean owns(AbstractQueuedSynchronizer.ConditionObject condition)

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

final boolean release(int arg)

以独占模式发布。

final boolean releaseShared(int arg)

以共享模式发布。

String toString()

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

final boolean tryAcquireNanos(int arg, long nanosTimeout)

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

final boolean tryAcquireSharedNanos(int arg, long nanosTimeout)

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

Protected methods

final boolean compareAndSetState(int expect, int update)

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

final int getState()

返回同步状态的当前值。

boolean isHeldExclusively()

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

final void setState(int newState)

设置同步状态的值。

boolean tryAcquire(int arg)

尝试以独占模式获取。

int tryAcquireShared(int arg)

尝试以共享模式获取。

boolean tryRelease(int arg)

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

boolean tryReleaseShared(int arg)

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

Inherited methods

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

Protected constructors

AbstractQueuedSynchronizer

Added in API level 1
AbstractQueuedSynchronizer ()

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

Public methods

acquire

Added in API level 1
void acquire (int arg)

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

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

acquireInterruptibly

Added in API level 1
void acquireInterruptibly (int arg)

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

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

acquireShared

Added in API level 1
void acquireShared (int arg)

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

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

acquireSharedInterruptibly

Added in API level 1
void acquireSharedInterruptibly (int arg)

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

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

getExclusiveQueuedThreads

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

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

Returns
Collection<Thread> the collection of threads

getFirstQueuedThread

Added in API level 1
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 1
int getQueueLength ()

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

Returns
int the estimated number of threads waiting to acquire

getQueuedThreads

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

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

Returns
Collection<Thread> the collection of threads

getSharedQueuedThreads

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

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

Returns
Collection<Thread> the collection of threads

getWaitQueueLength

Added in API level 1
int getWaitQueueLength (AbstractQueuedSynchronizer.ConditionObject condition)

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

Parameters
condition AbstractQueuedSynchronizer.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 1
Collection<Thread> getWaitingThreads (AbstractQueuedSynchronizer.ConditionObject condition)

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

Parameters
condition AbstractQueuedSynchronizer.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 1
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 如果此方法返回true (除非这是可重入获取),则此同步器的tryAcquire(int)方法应返回false ,并且其tryAcquireShared(int)方法应返回负值。 例如,用于公平,可重入,独占模式同步器的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 1
boolean hasQueuedThreads ()

查询是否有线程正在等待获取。 请注意,因为中断和超时造成的取消可能随时发生,所以true退回并不保证任何其他线程都会获取。

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

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

hasWaiters

Added in API level 1
boolean hasWaiters (AbstractQueuedSynchronizer.ConditionObject condition)

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

Parameters
condition AbstractQueuedSynchronizer.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 1
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 1
boolean owns (AbstractQueuedSynchronizer.ConditionObject condition)

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

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

release

Added in API level 1
boolean release (int arg)

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

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

releaseShared

Added in API level 1
boolean releaseShared (int arg)

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

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

toString

Added in API level 1
String toString ()

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

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

tryAcquireNanos

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

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

Parameters
arg int: the acquire argument. This value is conveyed to tryAcquire(int) 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 1
boolean tryAcquireSharedNanos (int arg, 
                long nanosTimeout)

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

Parameters
arg int: the acquire argument. This value is conveyed to tryAcquireShared(int) 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 1
boolean compareAndSetState (int expect, 
                int update)

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

Parameters
expect int: the expected value
update int: 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 1
int getState ()

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

Returns
int current state value

isHeldExclusively

Added in API level 1
boolean isHeldExclusively ()

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

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

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

setState

Added in API level 1
void setState (int newState)

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

Parameters
newState int: the new state value

tryAcquire

Added in API level 1
boolean tryAcquire (int arg)

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

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

默认实现抛出 UnsupportedOperationException

Parameters
arg int: 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 1
int tryAcquireShared (int arg)

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

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

默认实现抛出 UnsupportedOperationException

Parameters
arg int: 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
int 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 1
boolean tryRelease (int arg)

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

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

默认实现抛出 UnsupportedOperationException

Parameters
arg int: 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 1
boolean tryReleaseShared (int arg)

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

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

默认实现抛出 UnsupportedOperationException

Parameters
arg int: 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!