ProgramixGenericLib v5.0.1

com.programix.thread
Class Counter

java.lang.Object
  extended by com.programix.thread.Counter
All Implemented Interfaces:
ThreadSafe

public class Counter
extends Object
implements ThreadSafe

This class serves as a thread-safe integer counter. In fact, there's no requirement that the value only increment or decrement—the value can hop around to any value by using setCount(int).

Author:
Paul Hyde

Constructor Summary
Counter()
          Creates a counter that starts off with a value of 0.
Counter(int initialCount)
          Creates a counter that starts off with the specified initialCount.
Counter(int initialCount, Object lockObject)
          Creates a counter that starts off with the specified initialCount.
Counter(int initialCount, Waiter waiter)
          Creates a counter that starts off with the specified initialCount.
Counter(Object lockObject)
          Creates a counter that starts off with a value of 0.
 
Method Summary
 Waiter.Condition createAtLeastCondition(int minCount)
          Returns a Waiter.Condition that can be used to wait while or wait until the count is greater than or equal to the specified minCount.
 Waiter.Condition createAtMostCondition(int maxCount)
          Returns a Waiter.Condition that can be used to wait while or wait until the count is less than or equal to the specified maxCount.
 Waiter.Condition createCondition(Waiter.Expression expression)
          Returns a Waiter.Condition that can be used to wait while or wait until the the specified expression is true.
 Waiter.Condition createRangeCondition(int min, int max)
          Returns a Waiter.Condition that can be used to wait while or wait until the count is within the specified range (inclusive of the min and max).
 void decrement()
           
 boolean decrementIfNotShutdown()
          Potentially decrements the counter returning true if the counter was decremented or false is the counter was not changed because it's already been shutdown.
 int getCount()
           
 Object getLockObject()
           
 Waiter getWaiter()
           
 void increment()
           
 boolean incrementIfNotShutdown()
          Potentially increments the counter returning true if the counter was incremented or false is the counter was not changed because it's already been shutdown.
 boolean isNotZero()
           
 boolean isShutdown()
           
 boolean isZero()
           
 void setCount(int newCount)
           
 void shutdown()
          Called to shutdown all access to this counter and to dislodge any threads waiting on conditions.
 void waitUntilNegative()
          Waits until the count is a negative number (less than zero).
 void waitUntilNegative(long msTimeout)
          Waits until the count is a negative number (less than zero).
 void waitUntilPositive()
          Waits until the count is a positive number (greater than zero).
 void waitUntilPositive(long msTimeout)
          Waits until the count is a positive number (greater than zero).
 void waitUntilZero()
          Waits until the count is exactly zero.
 void waitUntilZero(long msTimeout)
          Waits until the count is exactly zero.
 void waitWhileNegative()
          Waits while the count is a negative number (less than zero).
 void waitWhileNegative(long msTimeout)
          Waits while the count is a negative number (less than zero).
 void waitWhilePositive()
          Waits while the count is a positive number (greater than zero).
 void waitWhilePositive(long msTimeout)
          Waits while the count is a positive number (greater than zero).
 void waitWhileZero()
          Waits while the count is exactly zero.
 void waitWhileZero(long msTimeout)
          Waits while the count is exactly zero.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Counter

public Counter(int initialCount,
               Waiter waiter)
Creates a counter that starts off with the specified initialCount.

Parameters:
initialCount - the starting value.
waiter - the Waiter to use. If null, then a new Waiter is automatically created.

Counter

public Counter(int initialCount,
               Object lockObject)
Creates a counter that starts off with the specified initialCount.

Parameters:
initialCount - the starting value.
lockObject - the object to synchronize on. If null, the the synchronization will be on this instance.

Counter

public Counter(int initialCount)
Creates a counter that starts off with the specified initialCount.

Parameters:
initialCount - the starting value.

Counter

public Counter(Object lockObject)
Creates a counter that starts off with a value of 0.

Parameters:
lockObject - the object to synchronize on. If null, the the synchronization will be on this instance.

Counter

public Counter()
Creates a counter that starts off with a value of 0.

Method Detail

getCount

public int getCount()
             throws ShutdownException
Throws:
ShutdownException

setCount

public void setCount(int newCount)
              throws ShutdownException
Throws:
ShutdownException

increment

public void increment()
               throws ShutdownException
Throws:
ShutdownException

incrementIfNotShutdown

public boolean incrementIfNotShutdown()
Potentially increments the counter returning true if the counter was incremented or false is the counter was not changed because it's already been shutdown.


decrement

public void decrement()
               throws ShutdownException
Throws:
ShutdownException

decrementIfNotShutdown

public boolean decrementIfNotShutdown()
Potentially decrements the counter returning true if the counter was decremented or false is the counter was not changed because it's already been shutdown.


isZero

public boolean isZero()
               throws ShutdownException
Throws:
ShutdownException

isNotZero

public boolean isNotZero()
                  throws ShutdownException
Throws:
ShutdownException

waitWhileZero

public void waitWhileZero(long msTimeout)
                   throws TimedOutException,
                          ShutdownException,
                          InterruptException
Waits while the count is exactly zero.

Throws:
TimedOutException
ShutdownException
InterruptException

waitWhileZero

public void waitWhileZero()
                   throws ShutdownException,
                          InterruptException
Waits while the count is exactly zero.

Throws:
ShutdownException
InterruptException

waitUntilZero

public void waitUntilZero(long msTimeout)
                   throws TimedOutException,
                          ShutdownException,
                          InterruptException
Waits until the count is exactly zero.

Throws:
TimedOutException
ShutdownException
InterruptException

waitUntilZero

public void waitUntilZero()
                   throws ShutdownException,
                          InterruptException
Waits until the count is exactly zero.

Throws:
ShutdownException
InterruptException

waitWhilePositive

public void waitWhilePositive(long msTimeout)
                       throws TimedOutException,
                              ShutdownException,
                              InterruptException
Waits while the count is a positive number (greater than zero).

Throws:
TimedOutException
ShutdownException
InterruptException

waitWhilePositive

public void waitWhilePositive()
                       throws ShutdownException,
                              InterruptException
Waits while the count is a positive number (greater than zero).

Throws:
ShutdownException
InterruptException

waitUntilPositive

public void waitUntilPositive(long msTimeout)
                       throws TimedOutException,
                              ShutdownException,
                              InterruptException
Waits until the count is a positive number (greater than zero).

Throws:
TimedOutException
ShutdownException
InterruptException

waitUntilPositive

public void waitUntilPositive()
                       throws ShutdownException,
                              InterruptException
Waits until the count is a positive number (greater than zero).

Throws:
ShutdownException
InterruptException

waitWhileNegative

public void waitWhileNegative(long msTimeout)
                       throws TimedOutException,
                              ShutdownException,
                              InterruptException
Waits while the count is a negative number (less than zero).

Throws:
TimedOutException
ShutdownException
InterruptException

waitWhileNegative

public void waitWhileNegative()
                       throws ShutdownException,
                              InterruptException
Waits while the count is a negative number (less than zero).

Throws:
ShutdownException
InterruptException

waitUntilNegative

public void waitUntilNegative(long msTimeout)
                       throws TimedOutException,
                              ShutdownException,
                              InterruptException
Waits until the count is a negative number (less than zero).

Throws:
TimedOutException
ShutdownException
InterruptException

waitUntilNegative

public void waitUntilNegative()
                       throws ShutdownException,
                              InterruptException
Waits until the count is a negative number (less than zero).

Throws:
ShutdownException
InterruptException

createAtLeastCondition

public Waiter.Condition createAtLeastCondition(int minCount)
Returns a Waiter.Condition that can be used to wait while or wait until the count is greater than or equal to the specified minCount.


createAtMostCondition

public Waiter.Condition createAtMostCondition(int maxCount)
Returns a Waiter.Condition that can be used to wait while or wait until the count is less than or equal to the specified maxCount.


createRangeCondition

public Waiter.Condition createRangeCondition(int min,
                                             int max)
Returns a Waiter.Condition that can be used to wait while or wait until the count is within the specified range (inclusive of the min and max).


createCondition

public Waiter.Condition createCondition(Waiter.Expression expression)
Returns a Waiter.Condition that can be used to wait while or wait until the the specified expression is true.


getWaiter

public Waiter getWaiter()

shutdown

public void shutdown()
Called to shutdown all access to this counter and to dislodge any threads waiting on conditions.


isShutdown

public boolean isShutdown()

getLockObject

public Object getLockObject()

ProgramixGenericLib v5.0.1

Copyright © 2001-2009 Programix Incorporated. All rights reserved. ProgramixGenericLib is free and is OSI Certified Open Source Software under the BSD license.