- java.lang.Object
-
- org.hsqldb.lib.CountUpDownLatch
-
public class CountUpDownLatch extends java.lang.ObjectA variation onCountDownLatchto allow counting up as well as down.- Author:
- Campbell Burnet (campbell-burnet@users dot sourceforge.net)
-
-
Constructor Summary
Constructors Constructor Description CountUpDownLatch()Default constructor.CountUpDownLatch(int initialCount)Constructs a newCountUpDownLatchinitialized with the giveninitialCount.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidawait()Causes the current thread to wait untilcountreaches zero, unless the thread is interrupted.booleanawait(long timeout, java.util.concurrent.TimeUnit unit)Causes the current thread to wait untilcountreaches zero, unless the thread is interrupted, or the specified waiting time elapses.booleancountDown()Decrements the count of the latch, releasing all waiting threads if the count transitions to zero.booleancountDown(int amount)Decrements thecountof the latch by the givenamount, releasing all waiting threads ifcounttransitions to zero.booleancountUp()Increments the count of the latch.booleancountUp(int amount)Increments the count of the latch by the givenamount.booleanequals(java.lang.Object obj)Returns true if and only ifthisandobjrefer to the same object (this == objhas the valuetrue).booleanequals(CountUpDownLatch other)Returns true if and only ifthisandobjrefer to the same object (this == objhas the valuetrue).intgetCount()Returns the current count.inthashCode()As much as is reasonably practical, returns distinct integers for distinct objects.booleansetCount(int newCount)Updatescountto the requestednewCount, returningtrueon transition to zero.java.lang.StringtoString()Returns a string representation of this object.
-
-
-
Constructor Detail
-
CountUpDownLatch
public CountUpDownLatch()
Default constructor.Equivalent to
newCountUpDownLatch(0)
-
CountUpDownLatch
public CountUpDownLatch(int initialCount)
Constructs a newCountUpDownLatchinitialized with the giveninitialCount.- Parameters:
initialCount- the initialcount- Throws:
java.lang.IllegalArgumentException- ifinitialCountis negative
-
-
Method Detail
-
await
public void await() throws java.lang.InterruptedExceptionCauses the current thread to wait untilcountreaches zero, unless the thread is interrupted.If the current
countis already zero, then this method returns immediately.If the current
countis greater than zero, then the current thread becomes disabled for thread scheduling purposes and lies dormant until either:- The count reaches zero due an invocation of
countDown(),countDown(int}, orsetCount(int). - Some other thread interrupts the current thread.
- has its interrupted status set on entry to this method; or
- is interrupted while waiting,
InterruptedExceptionis thrown and the current thread's interrupted status is cleared.- Throws:
java.lang.InterruptedException- if the current thread is interrupted while waiting
- The count reaches zero due an invocation of
-
await
public boolean await(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedExceptionCauses the current thread to wait untilcountreaches zero, unless the thread is interrupted, or the specified waiting time elapses.If the current
countis zero, then this method returns immediately with the valuetrue.If the current
countis greater than zero, then the current thread becomes disabled for thread scheduling purposes and lies dormant until either:- The
countreaches zero due to an invocation ofcountDown(),countDown(int}, orsetCount(int)setCount(int)} - Some other thread interrupts the current thread
- The specified waiting time elapses.
true.If the current thread:
- has its interrupted status set on entry to this method; or
- is interrupted while waiting,
InterruptedExceptionis thrown and the current thread's interrupted status is cleared.If the specified waiting time elapses then the value
falseis returned. If the time is less than or equal to zero, the method will not wait at all.- Parameters:
timeout- the maximum time to waitunit- the time unit of thetimeoutargument- Returns:
trueif the count reached zero andfalseif the waiting time elapsed before the count reached zero- Throws:
java.lang.InterruptedException- if the current thread is interrupted while waiting
- The
-
countUp
public boolean countUp()
Increments the count of the latch.- Returns:
trueifcounttransitioned from zero to a new value- Throws:
java.lang.ArithmeticException- when the operation would otherwise cause a silent numeric overflow, resulting in a negativecount.
-
countUp
public boolean countUp(int amount)
Increments the count of the latch by the givenamount.- Parameters:
amount- by which to incrementcount- Returns:
trueifcounttransitioned from zero to a new value- Throws:
java.lang.ArithmeticException- when the operation would otherwise cause a silent numeric overflow, resulting in a negativecount.java.lang.IllegalArgumentException- ifamountis less than one.
-
countDown
public boolean countDown()
Decrements the count of the latch, releasing all waiting threads if the count transitions to zero.If the current count is zero, no action occurs and false is returned immediately;
- Returns:
trueifcounttransitions to zero
-
countDown
public boolean countDown(int amount)
Decrements thecountof the latch by the givenamount, releasing all waiting threads ifcounttransitions to zero.If the current
countis zero, no action occurs and false is returned immediately; otherwise,countis decremented by the lesser ofamountand currentcounti.e. ifamountis greater than currentcount, then newcountis zero, else newcountis currentcountminusamount.- Parameters:
amount- by which to decrement thecount- Returns:
trueifcounttransitions to zero- Throws:
java.lang.IllegalArgumentException- whenamountis non-positive
-
getCount
public int getCount()
Returns the current count.Because another thread may update
countat any time, typically this should not be used to compute input values for any of the @{code count} mutating methods and instead should be reserved for debugging and testing purposes (e.g. to assert that the current count is the expected count, given a set of know operations has occurred and given that it is known no other threads could be updating the count)- Returns:
- the current count
-
setCount
public boolean setCount(int newCount)
Updatescountto the requestednewCount, returningtrueon transition to zero.If
newCountis zero and the currentcountis zero, no action occurs and false is returned immediately. immediately;- Parameters:
newCount- to which to updatecount; must be non-negative.- Returns:
trueifcounttransitions to zero.- Throws:
java.lang.IllegalArgumentException- whennewCountis negative
-
toString
public java.lang.String toString()
Returns a string representation of this object.- Overrides:
toStringin classjava.lang.Object- Returns:
- a string identifying this latch, as well as its current
count.
-
hashCode
public int hashCode()
As much as is reasonably practical, returns distinct integers for distinct objects.- Overrides:
hashCodein classjava.lang.Object- Returns:
- a hash code value for this latch. This method is supported for the benefit of hash tables
-
equals
public boolean equals(CountUpDownLatch other)
Returns true if and only ifthisandobjrefer to the same object (this == objhas the valuetrue).- Parameters:
other- to test.- Returns:
- if and only if
this == obj
-
equals
public boolean equals(java.lang.Object obj)
Returns true if and only ifthisandobjrefer to the same object (this == objhas the valuetrue).- Overrides:
equalsin classjava.lang.Object- Parameters:
obj- to test.- Returns:
- if and only if
this == obj
-
-