- java.lang.Object
-
- org.hsqldb.lib.CountUpDownLatch
-
public class CountUpDownLatch extends java.lang.Object
A variation onCountDownLatch
to 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 newCountUpDownLatch
initialized with the giveninitialCount
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
await()
Causes the current thread to wait untilcount
reaches zero, unless the thread is interrupted.boolean
await(long timeout, java.util.concurrent.TimeUnit unit)
Causes the current thread to wait untilcount
reaches zero, unless the thread is interrupted, or the specified waiting time elapses.boolean
countDown()
Decrements the count of the latch, releasing all waiting threads if the count transitions to zero.boolean
countDown(int amount)
Decrements thecount
of the latch by the givenamount
, releasing all waiting threads ifcount
transitions to zero.boolean
countUp()
Increments the count of the latch.boolean
countUp(int amount)
Increments the count of the latch by the givenamount
.boolean
equals(java.lang.Object obj)
Returns true if and only ifthis
andobj
refer to the same object (this == obj
has the valuetrue
).boolean
equals(CountUpDownLatch other)
Returns true if and only ifthis
andobj
refer to the same object (this == obj
has the valuetrue
).int
getCount()
Returns the current count.int
hashCode()
As much as is reasonably practical, returns distinct integers for distinct objects.boolean
setCount(int newCount)
Updatescount
to the requestednewCount
, returningtrue
on transition to zero.java.lang.String
toString()
Returns a string representation of this object.
-
-
-
Constructor Detail
-
CountUpDownLatch
public CountUpDownLatch()
Default constructor.Equivalent to
new
CountUpDownLatch
(0)
-
CountUpDownLatch
public CountUpDownLatch(int initialCount)
Constructs a newCountUpDownLatch
initialized with the giveninitialCount
.- Parameters:
initialCount
- the initialcount
- Throws:
java.lang.IllegalArgumentException
- ifinitialCount
is negative
-
-
Method Detail
-
await
public void await() throws java.lang.InterruptedException
Causes the current thread to wait untilcount
reaches zero, unless the thread is interrupted.If the current
count
is already zero, then this method returns immediately.If the current
count
is 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,
InterruptedException
is 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.InterruptedException
Causes the current thread to wait untilcount
reaches zero, unless the thread is interrupted, or the specified waiting time elapses.If the current
count
is zero, then this method returns immediately with the valuetrue
.If the current
count
is greater than zero, then the current thread becomes disabled for thread scheduling purposes and lies dormant until either:- The
count
reaches 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,
InterruptedException
is thrown and the current thread's interrupted status is cleared.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.- Parameters:
timeout
- the maximum time to waitunit
- the time unit of thetimeout
argument- Returns:
true
if the count reached zero andfalse
if 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:
true
ifcount
transitioned 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:
true
ifcount
transitioned 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
- ifamount
is 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:
true
ifcount
transitions to zero
-
countDown
public boolean countDown(int amount)
Decrements thecount
of the latch by the givenamount
, releasing all waiting threads ifcount
transitions to zero.If the current
count
is zero, no action occurs and false is returned immediately; otherwise,count
is decremented by the lesser ofamount
and currentcount
i.e. ifamount
is greater than currentcount
, then newcount
is zero, else newcount
is currentcount
minusamount
.- Parameters:
amount
- by which to decrement thecount
- Returns:
true
ifcount
transitions to zero- Throws:
java.lang.IllegalArgumentException
- whenamount
is non-positive
-
getCount
public int getCount()
Returns the current count.Because another thread may update
count
at 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)
Updatescount
to the requestednewCount
, returningtrue
on transition to zero.If
newCount
is zero and the currentcount
is zero, no action occurs and false is returned immediately. immediately;- Parameters:
newCount
- to which to updatecount
; must be non-negative.- Returns:
true
ifcount
transitions to zero.- Throws:
java.lang.IllegalArgumentException
- whennewCount
is negative
-
toString
public java.lang.String toString()
Returns a string representation of this object.- Overrides:
toString
in 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:
hashCode
in 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 ifthis
andobj
refer to the same object (this == obj
has 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 ifthis
andobj
refer to the same object (this == obj
has the valuetrue
).- Overrides:
equals
in classjava.lang.Object
- Parameters:
obj
- to test.- Returns:
- if and only if
this == obj
-
-