org.progeeks.util.thread
Class StateCollector

java.lang.Object
  extended by org.progeeks.util.thread.StateCollector

public class StateCollector
extends java.lang.Object

Can track state among multiple threads and wait on consensus or for a certain number of participants to have set their state. This is useful for loose coordination between separate processes where majority votes and such can be sufficient. Actors can leave and join and the overall state calculation will adjusted accordingly.

Version:
$Revision: 1.9 $
Author:
Paul Speed

Constructor Summary
StateCollector()
           
 
Method Summary
 void addActor(java.lang.Object actor)
          Adds the specified actor to the state collector.
 void clearAllActors()
          Removes all of the actors from the map.
 void clearAllStates()
          Clears all of the states for all of the actors.
 void clearState(java.lang.Object actor)
           
 boolean containsActor(java.lang.Object actor)
          Returns true if this collector is monitoring the specified actor.
 int getActorCount()
          Returns the total number of actors.
 java.util.Collection getActors()
          Returns a new collection containing all of the current actor objects.
 java.lang.Object getState(java.lang.Object actor)
          Returns the state for the specified actor.
 int getStateCount()
          Returns the number of actors that have their states set.
 java.util.Collection getStates()
          Returns a new collection containing all of the current state objects.
 boolean hasState(java.lang.Object actor)
           
 void interrupt()
           
 void removeActor(java.lang.Object actor)
          Removes the specified actor from this collector.
 void setState(java.lang.Object actor, java.lang.Object state)
          Sets the state for the specified actor.
 boolean waitForStates(double fraction, long timeout)
          Waits until a certain percentage of states have been collected or until the specified timeout.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StateCollector

public StateCollector()
Method Detail

addActor

public void addActor(java.lang.Object actor)
Adds the specified actor to the state collector. This actor will then be included in any state aggregation calculations.


removeActor

public void removeActor(java.lang.Object actor)
Removes the specified actor from this collector. This actor will no longer be considered in any state aggregation calculations.


containsActor

public boolean containsActor(java.lang.Object actor)
Returns true if this collector is monitoring the specified actor.


setState

public void setState(java.lang.Object actor,
                     java.lang.Object state)
Sets the state for the specified actor. The state object can be null and will still be considered a valid state value.


getState

public java.lang.Object getState(java.lang.Object actor)
Returns the state for the specified actor. The returned state can be null even if hasState() returns true.


hasState

public boolean hasState(java.lang.Object actor)

clearState

public void clearState(java.lang.Object actor)

getStates

public java.util.Collection getStates()
Returns a new collection containing all of the current state objects.


getActors

public java.util.Collection getActors()
Returns a new collection containing all of the current actor objects.


getStateCount

public int getStateCount()
Returns the number of actors that have their states set.


getActorCount

public int getActorCount()
Returns the total number of actors.


clearAllStates

public void clearAllStates()
Clears all of the states for all of the actors.


clearAllActors

public void clearAllActors()
Removes all of the actors from the map. By definition this will cause any waiting checks to return successfully.


interrupt

public void interrupt()

waitForStates

public boolean waitForStates(double fraction,
                             long timeout)
                      throws java.lang.InterruptedException
Waits until a certain percentage of states have been collected or until the specified timeout. A timeout of 0 will wait forever.

Parameters:
fraction - A value of 0 to 1.0 representing the number of states relative to the total number of actors to be collected before the method returns.
timeout - Number of milliseconds to wait. 0 for infinity.
Returns:
True if the wait was successful. False if the wait terminated early due to timeout or was interrupted.
Throws:
java.lang.InterruptedException


Copyright © 2002-2003 Paul Speed. All Rights Reserved.