Apache Tomcat 7.0.11

org.apache.tomcat.util.threads
Class CounterLatch

java.lang.Object
  extended by org.apache.tomcat.util.threads.CounterLatch

public class CounterLatch
extends Object

Simple counter latch that allows code to keep an up and down counter, and waits while the latch holds a certain wait value. and threads using the latch to wait if the count has reached a certain value. The counter latch can be used to keep track of an atomic counter, since the operations countDown() and countUp() are atomic. When the latch reaches the wait value, threads will block. The counter latch can hence act like a count down latch or a count up latch, while letting you keep track of the counter as well. This counter latch works opposite as the java.util.concurrent.CountDownLatch, since the CounterLatch only blocks on a single value and releases the threads on all other values.

Author:
fhanik
See Also:
CountDownLatch

Constructor Summary
CounterLatch(long initial, long waitValue)
          Instantiates a CounterLatch object with an initial value and a wait value.
 
Method Summary
 void await()
          Causes the calling thread to wait if the counter holds the waitValue.
 boolean await(long timeout, TimeUnit unit)
          Causes the calling thread to wait if the counter holds the waitValue.
 boolean compareAndSet(long expect, long update)
          Performs an atomic update of the counter If the operation is successful and {@code expect==waitValue && expect!
 long countDown()
          Decrements the counter
 long countUp()
          Increments the counter
 long getCount()
          Returns the current counter value
 Collection<Thread> getQueuedThreads()
          Returns a collection of the blocked threads
 boolean hasQueuedThreads()
          returns true if there are threads blocked by this latch
 boolean releaseAll()
          releases all waiting threads.
 void reset(long value)
          Resets the latch and initializes the counter with the new value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CounterLatch

public CounterLatch(long initial,
                    long waitValue)
Instantiates a CounterLatch object with an initial value and a wait value.

Parameters:
initial - - initial value of the counter
waitValue - - when the counter holds this value, threads calling await() or await(long, TimeUnit) will wait until the counter changes value or until they are interrupted.
Method Detail

await

public void await()
           throws InterruptedException
Causes the calling thread to wait if the counter holds the waitValue. If the counter holds any other value, the thread will return If the thread is interrupted or becomes interrupted an InterruptedException is thrown

Throws:
InterruptedException

await

public boolean await(long timeout,
                     TimeUnit unit)
              throws InterruptedException
Causes the calling thread to wait if the counter holds the waitValue. If the counter holds any other value, the thread will return If the thread is interrupted or becomes interrupted an InterruptedException is thrown

Returns:
true if the value changed, false if the timeout has elapsed
Throws:
InterruptedException

countUp

public long countUp()
Increments the counter

Returns:
the previous counter value

countDown

public long countDown()
Decrements the counter

Returns:
the previous counter value

getCount

public long getCount()
Returns the current counter value

Returns:
the current counter value

compareAndSet

public boolean compareAndSet(long expect,
                             long update)
Performs an atomic update of the counter If the operation is successful and expect==waitValue && expect!=update waiting threads will be released.

Parameters:
expect - - the expected counter value
update - - the new counter value
Returns:
true if successful, false if the current value wasn't as expected

hasQueuedThreads

public boolean hasQueuedThreads()
returns true if there are threads blocked by this latch

Returns:
true if there are threads blocked by this latch

getQueuedThreads

public Collection<Thread> getQueuedThreads()
Returns a collection of the blocked threads

Returns:
a collection of the blocked threads

releaseAll

public boolean releaseAll()
releases all waiting threads. This operation is permanent, and no threads will block, even if the counter hits the waitValue until reset(long) has been called.

Returns:
true if this release of shared mode may permit a waiting acquire (shared or exclusive) to succeed; and false otherwise

reset

public void reset(long value)
Resets the latch and initializes the counter with the new value.

Parameters:
value - the new counter value
See Also:
releaseAll()

Apache Tomcat 7.0.11

Copyright © 2000-2011 Apache Software Foundation. All Rights Reserved.