com.limegroup.gnutella.util
Class ThreadWorkGroup

java.lang.Object
  extended bycom.limegroup.gnutella.util.ThreadWorkGroup

public class ThreadWorkGroup
extends java.lang.Object

A reuseable class that allows for a WorkGroup of WorkerInterface threads to perform tasks. All that needs to be done is to implement the WorkerInterface (and optionally the CleanUpInterface) and supply instances as necessary.


Nested Class Summary
static interface ThreadWorkGroup.CleanUpInterface
          In case you want to provide a central cleanup mechanism.
static interface ThreadWorkGroup.WorkerInterface
          The interface that you should provide a implementation for.
 
Constructor Summary
ThreadWorkGroup(ThreadWorkGroup.WorkerInterface[] workers, ThreadWorkGroup.CleanUpInterface cleaner)
          Starts workers.length threads that will execute upon input tasks (see the addTask method).
 
Method Summary
 void addTask(java.lang.Object[] input)
           
 void addWorker(ThreadWorkGroup.WorkerInterface worker)
           
 boolean isActive()
           
 boolean stop(int waitTime)
          Once this is called, the ThreadWorkGroup is useless and all threads are correctly extinguished (though they may finish what they are working on.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ThreadWorkGroup

public ThreadWorkGroup(ThreadWorkGroup.WorkerInterface[] workers,
                       ThreadWorkGroup.CleanUpInterface cleaner)
                throws java.lang.IllegalArgumentException
Starts workers.length threads that will execute upon input tasks (see the addTask method). isActive() is true after this call (and is true until stop() is called).

Parameters:
workers - The initial set of workers you want to be executing.
cleaner - An optional cleaner (to be run after a worker performs a task). Can be null....
Throws:
java.lang.IllegalArgumentException - If workers is malformed.
Method Detail

addTask

public void addTask(java.lang.Object[] input)

addWorker

public void addWorker(ThreadWorkGroup.WorkerInterface worker)
Parameters:
worker - A WorkerInterface instance to add to the mix.

isActive

public boolean isActive()
Returns:
true if stop() has never been called.

stop

public boolean stop(int waitTime)
             throws java.lang.InterruptedException
Once this is called, the ThreadWorkGroup is useless and all threads are correctly extinguished (though they may finish what they are working on.

Parameters:
waitTime - join's for up to waitTime millis, or forever if 0.
Returns:
true if none of the threads were alive upon returning from the method and after waiting waitTime per thread.
Throws:
java.lang.InterruptedException - thrown because this call may block while threads are finishing....