org.sadun.util
Class ThreadBlockRunner

java.lang.Object
  extended byorg.sadun.util.ThreadBlockRunner

public class ThreadBlockRunner
extends java.lang.Object

Runs a block of threads within an array, with a maximum number of concurrent threads, and returning only when all the threads in the block have finished.

Optionally, each thread (but the first in each block) can be synchronized with the previous one, that is, it waits to start for the previous one is terminated.

To just run a set of thread concurrently and wait for all of them to be terminated, simply use the ThreadBlockRunner(Thread[]) constructor and runAll() as in the following snippet:

  ThreadBlockRunner tbr = new ThreadBlocRunner( threads ); // threads is an array of thread objects
  tbr.runAll();
 

Version:
2.1
Author:
cris sadun

Constructor Summary
ThreadBlockRunner(java.lang.Thread[] threads)
          Create a runner for the given array of threads.
ThreadBlockRunner(java.lang.Thread[] threads, int maxThreads)
          Create a runner for the given array of threads, which will run a maximum of maxThreads threads concurrently.
 
Method Summary
 boolean isSynchronizedWithPrevious(int i)
          Return whether or not a thread is synchronized with the previous one (see setSynchronizeWithPrevious()).
 boolean isSynchronizedWithPrevious(java.lang.Thread thread)
          Return whether or not a thread is synchronized with the previous one (see setSynchronizeWithPrevious()).
static void main(java.lang.String[] args)
           
static void main2(java.lang.String[] args)
          A test method.
 void runAll()
          Run all threads from the array passed at construction.
 void runBlock(int blockStart, int blockEnd)
          Run a block of consecutive threads from the array passed at construction.
 void setSynchronizedWithPrevious(int i, boolean value)
          Set a the i-th thread (realtive to the thread array passed at construction) to be synchronized with the previous one.
 void setSynchronizedWithPrevious(java.lang.Thread thread, boolean value)
          Set a certain thread in the threads passed at construction to be synchronized with the previous one.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ThreadBlockRunner

public ThreadBlockRunner(java.lang.Thread[] threads,
                         int maxThreads)
Create a runner for the given array of threads, which will run a maximum of maxThreads threads concurrently.

Parameters:
threads - the array of threads to run
maxThreads - the maximum number of concurrent arrays to run

ThreadBlockRunner

public ThreadBlockRunner(java.lang.Thread[] threads)
Create a runner for the given array of threads. The maximum number of threads used is set equal to the size of the array, so all will have a chance to run concurrently when runAll() or runBlock(int, int) are invoked.

Parameters:
threads - the array of threads to run
Method Detail

setSynchronizedWithPrevious

public void setSynchronizedWithPrevious(java.lang.Thread thread,
                                        boolean value)
Set a certain thread in the threads passed at construction to be synchronized with the previous one.

If at execution time the thread results the first in a block, the synchronization will be ignored.

Parameters:
thread - one of the threads passed at construction.
value - if true, the thread will wait until the previous one has terminated before starting.

isSynchronizedWithPrevious

public boolean isSynchronizedWithPrevious(java.lang.Thread thread)
Return whether or not a thread is synchronized with the previous one (see setSynchronizeWithPrevious()).

Parameters:
thread - one of the threads passed at construction.
Returns:
whether or not the thread is synchronized with the previous one.

setSynchronizedWithPrevious

public void setSynchronizedWithPrevious(int i,
                                        boolean value)
Set a the i-th thread (realtive to the thread array passed at construction) to be synchronized with the previous one.

If at execution time the thread results the first in a block, the synchronization will be ignored.

Parameters:
i - a valid index in the thread array passed at construction.
value - if true, the thread will wait until the previous one has terminated before starting.

isSynchronizedWithPrevious

public boolean isSynchronizedWithPrevious(int i)
Return whether or not a thread is synchronized with the previous one (see setSynchronizeWithPrevious()).

Parameters:
i - a valid index in the thread array passed at construction.
Returns:
whether or not the thread in the i-th position is synchronized with the previous one.

runAll

public void runAll()
Run all threads from the array passed at construction.

If any thread's "synchronizeWithPrevious" flag has been set, the thread waits for the previous thread to terminate before starting (the first thread flag state is ignored).

Note that the maximum number of threads running concurrently is the one set at construction.


runBlock

public void runBlock(int blockStart,
                     int blockEnd)
Run a block of consecutive threads from the array passed at construction.

If any thread's "synchronizeWithPrevious" flag has been set, the thread waits for the previous thread to terminate before starting (the first thread flag state is ignored).

Parameters:
blockStart - the index of the first thread to run, inclusive
blockEnd - the index of the last thread to run, exclusive

main2

public static void main2(java.lang.String[] args)
A test method.


main

public static void main(java.lang.String[] args)