Class CountUpDownLatch
- Author:
 - Pieter Martin Date: 2019/01/26
 
- 
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor.CountUpDownLatch(int initialCount) Constructs a newCountUpDownLatchinitialized with the giveninitialCount. - 
Method Summary
Modifier and TypeMethodDescriptionvoidawait()Causes the current thread to wait untilcountreaches zero, unless the thread is interrupted.booleanCauses the current thread to wait untilcountreaches zero, unless the thread is interrupted, or the specified waiting time elapses.booleanDecrements 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.booleanReturns 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.toString()Returns a string representation of this object. 
- 
Constructor Details
- 
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:
 IllegalArgumentException- ifinitialCountis negative
 
 - 
 - 
Method Details
- 
await
Causes the current thread to wait untilcountreaches zero, unless the thread is interrupted.If the current
If the currentcountis already zero, then this method returns immediately.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:
 InterruptedException- if the current thread is interrupted while waiting
 - The count reaches zero due an invocation of
 
 - 
await
Causes the current thread to wait untilcountreaches zero, unless the thread is interrupted, or the specified waiting time elapses.If the current
If the currentcountis zero, then this method returns immediately with the valuetrue.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) - 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 valuefalseis 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:
 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:
 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:
 ArithmeticException- when the operation would otherwise cause a silent numeric overflow, resulting in a negativecount.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:
 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 currentcount(i.e. ifamountis greater than currentcount, then newcountis zero, else newcountis currentcountminusamount.- Parameters:
 amount- by which to decrement thecount- Returns:
 trueifcounttransitions to zero- Throws:
 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 current }@code count is 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:
 IllegalArgumentException- whennewCountis negative
 - 
toString
Returns a string representation of this object. - 
hashCode
public int hashCode()As much as is reasonably practical, returns distinct integers for distinct objects. - 
equals
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
Returns true if and only ifthisandobjrefer to the same object (this == objhas the valuetrue). 
 -