artofillusion.util
Class ThreadManager

java.lang.Object
  extended by artofillusion.util.ThreadManager

public class ThreadManager
extends java.lang.Object

This class coordinates threads for multi-threaded operations. The execution model provided by this class is a single "task" (e.g. tracing a ray through a single pixel) which must be executed many times. The task is parameterized by a single index (e.g. the column containing the pixel).

To use this class, pass it an object which implements the Task interface. It automatically creates an appropriate number of threads based on the number of available processors. When you call run(), the task is repeatedly executed by the worker threads, with the index running over the desired range. You may invoke run() any number of times (e.g. once for each row of the image). Finally, call finish() to clean up the worker threads.


Nested Class Summary
static interface ThreadManager.Task
          This interface defines a task to be performed by the worker threads.
 
Constructor Summary
ThreadManager()
          Create a new uninitialized ThreadManager.
ThreadManager(int numIndices, ThreadManager.Task task)
          Create a new ThreadManager.
 
Method Summary
 void cancel()
          Cancel a run which is in progress.
 void finish()
          Dispose of all the worker threads.
 void run()
          Perform the task the specified number of times.
 void setMaxThreads(int maxThreads)
          Set the maximum number of worker threads that should be used for executing the Task.
 void setNumIndices(int numIndices)
          Set the number of values the index should take on.
 void setTask(ThreadManager.Task task)
          Set the Task to be executed by the worker threads.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ThreadManager

public ThreadManager()
Create a new uninitialized ThreadManager. You must invoke setNumIndices() and setTask() to initialize it before calling run().


ThreadManager

public ThreadManager(int numIndices,
                     ThreadManager.Task task)
Create a new ThreadManager.

Parameters:
numIndices - the number of values the index should take on (from 0 to numIndices-1)
task - the task to perform
Method Detail

setNumIndices

public void setNumIndices(int numIndices)
Set the number of values the index should take on. This must be invoked from the same thread that instantiated the ThreadManager and that calls run().


setTask

public void setTask(ThreadManager.Task task)
Set the Task to be executed by the worker threads. If another Task has already been set, that one is discarded immediately and cleanup() will never be invoked on in. This method must be invoked from the same thread that instantiated the ThreadManager and that calls run().


setMaxThreads

public void setMaxThreads(int maxThreads)
Set the maximum number of worker threads that should be used for executing the Task.


run

public void run()
Perform the task the specified number of times. This method blocks until all occurrences of the task are completed. If the current thread is interrupted while this method is in progress, all of the worker threads will be interrupted and disposed of.


cancel

public void cancel()
Cancel a run which is in progress. Calling this method does not interrupt any tasks that are currently executing, but it prevents any more from being started until the next time run() is called.


finish

public void finish()
Dispose of all the worker threads. Once this has been called, do not call run() again.



Copyright © 1999-2011 by Peter Eastman.