Top Description Inners Fields Constructors Methods
org.hsqldb.lib

public Class CountUpDownLatch

extends Object
Class Inheritance
Imports
java.util.concurrent.TimeUnit, java.util.concurrent.locks.AbstractQueuedSynchronizer

A variation on java.util.concurrent.CountDownLatch to allow counting up as well as down.
Author
Campbell Burnet (campbell-burnet@users dot sourceforge.net)
Version
2.7.3

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
CountUpDownLatch.Sync

Synchronization control For CountUpDownLatch.

Field Summary

Modifier and TypeField and Description
private final CountUpDownLatch.Sync

Constructor Summary

AccessConstructor and Description
public
CountUpDownLatch()

Default constructor.

public
CountUpDownLatch(int
the initial count
initialCount
)

Constructs a new CountUpDownLatch initialized with the given initialCount.

Method Summary

Modifier and TypeMethod and Description
public void
await()

Causes the current thread to wait until count reaches zero, unless the thread is interrupted.

public boolean

Returns:

true if the count reached zero and false if the waiting time elapsed before the count reached zero
await
(long
the maximum time to wait
timeout
,
TimeUnit
the time unit of the timeout argument
unit
)

Causes the current thread to wait until count reaches zero, unless the thread is interrupted, or the specified waiting time elapses.

public boolean

Returns:

true if count transitions to zero
countDown
()

Decrements the count of the latch, releasing all waiting threads if the count transitions to zero.

public boolean

Returns:

true if count transitions to zero
countDown
(int
by which to decrement the count
amount
)

Decrements the count of the latch by the given amount, releasing all waiting threads if count transitions to zero.

public boolean

Returns:

true if count transitioned from zero to a new value
countUp
()

Increments the count of the latch.

public boolean

Returns:

true if count transitioned from zero to a new value
countUp
(int
by which to increment count
amount
)

Increments the count of the latch by the given amount.

public boolean

Returns:

if and only if this == obj
equals
(final CountUpDownLatch
to test.
other
)

Returns true if and only if this and obj refer to the same object (this == obj has the value true).

public boolean

Returns:

if and only if this == obj
equals
(Object
to test.
obj
)

Overrides java.lang.Object.equals.

Returns true if and only if this and obj refer to the same object (this == obj has the value true).

public int

Returns:

the current count
getCount
()

Returns the current count.

public int

Returns:

a hash code value for this latch. This method is supported for the benefit of hash tables
hashCode
()

Overrides java.lang.Object.hashCode.

As much as is reasonably practical, returns distinct integers for distinct objects.

public boolean

Returns:

true if count transitions to zero.
setCount
(int
to which to update count; must be non-negative.
newCount
)

Updates count to the requested newCount, returning true on transition to zero.

public String

Returns:

a string identifying this latch, as well as its current count.
toString
()

Overrides java.lang.Object.toString.

Returns a string representation of this object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

syncback to summary
private final CountUpDownLatch.Sync sync

Constructor Detail

CountUpDownLatchback to summary
public CountUpDownLatch()

Default constructor.

Equivalent to new CountUpDownLatch(0)

CountUpDownLatchback to summary
public CountUpDownLatch(int initialCount)

Constructs a new CountUpDownLatch initialized with the given initialCount.

Parameters
initialCount:int

the initial count

Exceptions
IllegalArgumentException:
if initialCount is negative

Method Detail

awaitback to summary
public void await() throws InterruptedException

Causes the current thread to wait until count 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:

If the current thread:
  • has its interrupted status set on entry to this method; or
  • is interrupted while waiting,
then InterruptedException is thrown and the current thread's interrupted status is cleared.
Exceptions
InterruptedException:
if the current thread is interrupted while waiting
awaitback to summary
public boolean await(long timeout, TimeUnit unit) throws InterruptedException

Causes the current thread to wait until count 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 value true.

If the current count is greater than zero, then the current thread becomes disabled for thread scheduling purposes and lies dormant until either:

If the count reaches zero then the method returns with the value true.

If the current thread:

  • has its interrupted status set on entry to this method; or
  • is interrupted while waiting,
then 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:long

the maximum time to wait

unit:TimeUnit

the time unit of the timeout argument

Returns:boolean

true if the count reached zero and false if the waiting time elapsed before the count reached zero

Exceptions
InterruptedException:
if the current thread is interrupted while waiting
countDownback to summary
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:boolean

true if count transitions to zero

countDownback to summary
public boolean countDown(int amount)

Decrements the count of the latch by the given amount, releasing all waiting threads if count transitions to zero.

If the current count is zero, no action occurs and false is returned immediately; otherwise, count is decremented by the lesser of amount and current count i.e. if amount is greater than current count, then new count is zero, else new count is current count minus amount.

Parameters
amount:int

by which to decrement the count

Returns:boolean

true if count transitions to zero

Exceptions
IllegalArgumentException:
when amount is non-positive
countUpback to summary
public boolean countUp()

Increments the count of the latch.

Returns:boolean

true if count transitioned from zero to a new value

Exceptions
ArithmeticException:
when the operation would otherwise cause a silent numeric overflow, resulting in a negative count.
countUpback to summary
public boolean countUp(int amount)

Increments the count of the latch by the given amount.

Parameters
amount:int

by which to increment count

Returns:boolean

true if count transitioned from zero to a new value

Exceptions
ArithmeticException:
when the operation would otherwise cause a silent numeric overflow, resulting in a negative count.
IllegalArgumentException:
if amount is less than one.
equalsback to summary
public boolean equals(final CountUpDownLatch other)

Returns true if and only if this and obj refer to the same object (this == obj has the value true).

Parameters
other:CountUpDownLatch

to test.

Returns:boolean

if and only if this == obj

equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Returns true if and only if this and obj refer to the same object (this == obj has the value true).

Parameters
obj:Object

to test.

Returns:boolean

if and only if this == obj

getCountback to summary
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:int

the current count

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

As much as is reasonably practical, returns distinct integers for distinct objects.

Returns:int

a hash code value for this latch. This method is supported for the benefit of hash tables

setCountback to summary
public boolean setCount(int newCount)

Updates count to the requested newCount, returning true on transition to zero.

If newCount is zero and the current count is zero, no action occurs and false is returned immediately. immediately;

Parameters
newCount:int

to which to update count; must be non-negative.

Returns:boolean

true if count transitions to zero.

Exceptions
IllegalArgumentException:
when newCount is negative
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns a string representation of this object.

Returns:String

a string identifying this latch, as well as its current count.

org.hsqldb.lib back to summary

private Class CountUpDownLatch.Sync

extends AbstractQueuedSynchronizer
Class Inheritance

Synchronization control For CountUpDownLatch.

Uses AbstractQueuedSynchronizer state property to represent count.

Field Summary

Modifier and TypeField and Description
private static final long

Constructor Summary

AccessConstructor and Description
pack-priv
Sync(int count)

Method Summary

Modifier and TypeMethod and Description
pack-priv boolean

Returns:

true if {#code count} transitioned to zero.
countDown
()

by one.

pack-priv boolean

Returns:

true if {#code count} transitioned to zero.
countDown
(int
by which to decrement the count
amount
)

Counts down by amount.

pack-priv boolean

Returns:

true if count transitioned to zero
countUp
()

Counts up by one.

pack-priv boolean

Returns:

true if count transitioned from zero to a new value
countUp
(int
by which to increment count
amount
)

Counts up by amount.

pack-priv int
pack-priv boolean

Returns:

true if count transitions to zero.
setCount
(int
to which to update count; must be non-negative.
newCount
)

Updates count to newCount, returning true on transition to zero.

protected int

Returns:

-1 on failure; 1 if acquisition in shared mode succeeded and subsequent shared-mode acquires might also succeed, in which case a subsequent waiting thread must check availability.
tryAcquireShared
(int
ignored param
ignored
)

Overrides java.util.concurrent.locks.AbstractQueuedSynchronizer.tryAcquireShared.

Queries if the state of this synchronizer permits it to be acquired in the shared mode, and if so to acquire it.

protected boolean
tryReleaseShared(int arg)

Overrides java.util.concurrent.locks.AbstractQueuedSynchronizer.tryReleaseShared.

Attempts to set the state to reflect a release in shared mode.

Inherited from java.util.concurrent.locks.AbstractQueuedSynchronizer:
acquireacquireInterruptiblyacquireSharedacquireSharedInterruptiblycompareAndSetStategetExclusiveQueuedThreadsgetFirstQueuedThreadgetQueuedThreadsgetQueueLengthgetSharedQueuedThreadsgetStategetWaitingThreadsgetWaitQueueLengthhasContendedhasQueuedPredecessorshasQueuedThreadshasWaitersisHeldExclusivelyisQueuedownsreleasereleaseSharedsetStatetoStringtryAcquiretryAcquireNanostryAcquireSharedNanostryRelease

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.util.concurrent.locks.AbstractQueuedSynchronizer.serialVersionUID.

Constructor Detail

Syncback to summary
pack-priv Sync(int count)

Method Detail

countDownback to summary
pack-priv boolean countDown()

by one.

Returns:boolean

true if {#code count} transitioned to zero.

countDownback to summary
pack-priv boolean countDown(int amount)

Counts down by amount.

Parameters
amount:int

by which to decrement the count

Returns:boolean

true if {#code count} transitioned to zero.

Exceptions
IllegalArgumentException:
when amount is non-positive
countUpback to summary
pack-priv boolean countUp()

Counts up by one.

Returns:boolean

true if count transitioned to zero

Exceptions
ArithmeticException:
when the operation would otherwise cause a silent numeric overflow, resulting in a negative count.
countUpback to summary
pack-priv boolean countUp(int amount)

Counts up by amount.

Parameters
amount:int

by which to increment count

Returns:boolean

true if count transitioned from zero to a new value

Exceptions
ArithmeticException:
when the operation would otherwise cause a silent numeric overflow, resulting in a negative count.
IllegalArgumentException:
if amount is less than one
getCountback to summary
pack-priv int getCount()
setCountback to summary
pack-priv boolean setCount(int newCount)

Updates count to newCount, returning true on transition to zero.

If newCount is zero and the current count is zero, no action occurs and false is returned immediately. immediately;

Parameters
newCount:int

to which to update count; must be non-negative.

Returns:boolean

true if count transitions to zero.

Exceptions
IllegalArgumentException:
when newCount is negative
tryAcquireSharedback to summary
protected int tryAcquireShared(int ignored)

Overrides java.util.concurrent.locks.AbstractQueuedSynchronizer.tryAcquireShared.

Queries if the state of this synchronizer permits it to be acquired in the shared mode, and if so to acquire it.

This implementation supports the required semantics of the await(...) methods of the enclosing CountUpDownLatch class.

Parameters
ignored:int

ignored param

Returns:int

-1 on failure; 1 if acquisition in shared mode succeeded and subsequent shared-mode acquires might also succeed, in which case a subsequent waiting thread must check availability.

tryReleaseSharedback to summary
protected boolean tryReleaseShared(int arg)

Overrides java.util.concurrent.locks.AbstractQueuedSynchronizer.tryReleaseShared.

Doc from java.util.concurrent.locks.AbstractQueuedSynchronizer.tryReleaseShared.

Attempts to set the state to reflect a release in shared mode.

This method is always invoked by the thread performing release.

The default implementation throws UnsupportedOperationException.