|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.sadun.util.ThreadBlockRunner
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();
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 |
public ThreadBlockRunner(java.lang.Thread[] threads, int maxThreads)
threads
- the array of threads to runmaxThreads
- the maximum number of concurrent arrays to runpublic ThreadBlockRunner(java.lang.Thread[] threads)
runAll()
or runBlock(int, int)
are invoked.
threads
- the array of threads to runMethod Detail |
public void setSynchronizedWithPrevious(java.lang.Thread thread, boolean value)
If at execution time the thread results the first in a block, the synchronization will be ignored.
thread
- one of the threads passed at construction
.value
- if true, the thread will wait until the previous one has terminated before starting.public boolean isSynchronizedWithPrevious(java.lang.Thread thread)
setSynchronizeWithPrevious()
).
thread
- one of the threads passed at construction
.
public void setSynchronizedWithPrevious(int i, boolean value)
If at execution time the thread results the first in a block, the synchronization will be ignored.
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.public boolean isSynchronizedWithPrevious(int i)
setSynchronizeWithPrevious()
).
i
- a valid index in the thread array passed at construction
.
public void runAll()
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.
public void runBlock(int blockStart, int blockEnd)
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).
blockStart
- the index of the first thread to run, inclusiveblockEnd
- the index of the last thread to run, exclusivepublic static void main2(java.lang.String[] args)
public static void main(java.lang.String[] args)
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |