| java.lang.Object com.Ostermiller.util.Parallelizer
Parallelizer | public class Parallelizer (Code) | | Runs multiple jobs in parallel, n threads at a time, and waits
until all threads are complete before continuing.
Typically, Parallelizer would be used to run each of the items-
in a for loop at the same time. For example the following for
loop:
for (int i=0; i<10; i++){
System.out.println("Hello World " + i);
}
System.out.println("done");
To this:
Parallelizer parallelizer = new Parallelizer();
for (int i=0; i<10; i++){
final int j = i;
parallelizer.run(
new Runnable(){
System.out.println("Hello World " + j);
}
);
}
parallelizer.join();
System.out.println("done");
More information about this class is available from ostermiller.org.
author: Matt Conway - http://simplygenius.com/ author: Stephen Ostermiller - http://ostermiller.org/contact.pl?regarding=Java+Utilities since: ostermillerutils 1.05.00 |
Field Summary | |
final public static int | INFINITE_THREAD_LIMIT Constant that may be passed concurrentThreadLimit argument
of the constructor indicating that no limit should be placed
on the number of threads that are allowed to run concurrently. |
Constructor Summary | |
public | Parallelizer() Create a new Parallelizer with no limit on the number
of threads that will be allowed to be run concurrently. | public | Parallelizer(int concurrentThreadLimit) Create a new Parallelizer with the specified limit on the number
of threads that will be allowed to be run concurrently. |
Method Summary | |
public boolean | done() Return true iff all jobs that have been requested to run
in this Parallelizer have completed. | public void | dumpStack() Dump the stack of each running thread. | public Thread[] | getRunningThreads() Gets a list of all running threads. | public void | interrupt() All currently running threads will be interrupted. | public void | join() Block until all the jobs in this Parallelizer have run
and then return. | public void | run(Runnable job) Run the given job. | public void | run(Runnable job, String threadName) Run the given job. | public void | run(ThreadGroup threadGroup, Runnable job) Run the given job. | public void | run(ThreadGroup threadGroup, Runnable job, String threadName) Run the given job. | public void | run(ThreadGroup threadGroup, Runnable job, String threadName, long stackSize) Run the given job. |
INFINITE_THREAD_LIMIT | final public static int INFINITE_THREAD_LIMIT(Code) | | Constant that may be passed concurrentThreadLimit argument
of the constructor indicating that no limit should be placed
on the number of threads that are allowed to run concurrently.
since: ostermillerutils 1.05.00 |
Parallelizer | public Parallelizer()(Code) | | Create a new Parallelizer with no limit on the number
of threads that will be allowed to be run concurrently.
since: ostermillerutils 1.05.00 |
Parallelizer | public Parallelizer(int concurrentThreadLimit)(Code) | | Create a new Parallelizer with the specified limit on the number
of threads that will be allowed to be run concurrently.
When the concurrent thread limit is reached and the parallelizer
gets a new thread to run, the new thread will be queued until
a thread finishes.
Parameters: concurrentThreadLimit - number of threads that will be allowedto run simultaneously or INFINITE_THREAD_LIMIT for no limit. throws: IllegalArgumentException - if concurrentThreadLimit not a wholenumber or INFINITE_THREAD_LIMIT since: ostermillerutils 1.05.00 |
done | public boolean done()(Code) | | Return true iff all jobs that have been requested to run
in this Parallelizer have completed.
If this method throws an error, that
error may be handled and this method
may be called again as it will not re-throw the same
instance of the error.
Whether all jobs are done or not. throws: Error - if any of the running threads has thrown an Error. since: ostermillerutils 1.05.00 |
dumpStack | public void dumpStack()(Code) | | Dump the stack of each running thread.
If this method throws an error, that
error may be handled and this method
may be called again as it will not re-throw the same
instance of the error.
throws: Error - if any of the running threads has thrown an Error. since: ostermillerutils 1.05.00 |
getRunningThreads | public Thread[] getRunningThreads()(Code) | | Gets a list of all running threads. There may be jobs that
are queued and do not yet have threads. These job are not
returned.
If this method throws an error, that
error may be handled and this method
may be called again as it will not re-throw the same
instance of the error.
throws: Error - if any of the running threads has thrown an Error. an array of all currently running threads. since: ostermillerutils 1.05.00 |
interrupt | public void interrupt()(Code) | | All currently running threads will be interrupted.
The threads interrupted threads may die, causing
jobs that were queued but not yet started, to start.
If this method throws an error, that
error may be handled and this method
may be called again as it will not re-throw the same
instance of the error.
throws: Error - if any of the running threads has thrown an Error. since: ostermillerutils 1.05.00 |
join | public void join() throws InterruptedException(Code) | | Block until all the jobs in this Parallelizer have run
and then return.
If this method throws an exception or an error, that
exception or error may be handled and this method
may be called again as it will not re-throw the same
instance of the exception or error.
throws: InterruptedException - if interrupted while waiting. throws: RuntimeException - any running thread throws or has thrown a runtime exception. throws: Error - if any of the running threads throws or has thrown an Error. since: ostermillerutils 1.05.00 |
run | public void run(Runnable job)(Code) | | Run the given job. The given job is either run
immediately or if the max number of concurrent jobs are already
running, it is queued to be run when some job is finished.
If this method throws an error, that
error may be handled and this method
may be called again as it will not re-throw the same
instance of the error.
Parameters: job - job which is to be run in parallel with other jobs. throws: Error - if any thread that is already running has thrown an Error. throws: NullPointerException - if job is null. since: ostermillerutils 1.05.00 |
run | public void run(Runnable job, String threadName)(Code) | | Run the given job. The given job is either run
immediately or if the max number of concurrent jobs are already
running, it is queued to be run when some job is finished.
If this method throws an error, that
error may be handled and this method
may be called again as it will not re-throw the same
instance of the error.
Parameters: job - job which is to be run in parallel with other jobs. Parameters: threadName - name for the thread that will be created to run the job (null for auto generated thread name) throws: Error - if any thread that is already running has thrown an Error. throws: NullPointerException - if job is null. since: ostermillerutils 1.05.00 |
run | public void run(ThreadGroup threadGroup, Runnable job)(Code) | | Run the given job. The given job is either run
immediately or if the max number of concurrent jobs are already
running, it is queued to be run when some job is finished.
If this method throws an error, that
error may be handled and this method
may be called again as it will not re-throw the same
instance of the error.
Parameters: threadGroup - group in which this job should be run (null for default group). Parameters: job - job which is to be run in parallel with other jobs. throws: Error - if any thread that is already running has thrown an Error. throws: NullPointerException - if job is null. since: ostermillerutils 1.05.00 |
run | public void run(ThreadGroup threadGroup, Runnable job, String threadName)(Code) | | Run the given job. The given job is either run
immediately or if the max number of concurrent jobs are already
running, it is queued to be run when some job is finished.
If this method throws an error, that
error may be handled and this method
may be called again as it will not re-throw the same
instance of the error.
Parameters: threadGroup - group in which this job should be run (null for default group). Parameters: job - job which is to be run in parallel with other jobs. Parameters: threadName - name for the thread that will be created to run the job (null for auto generated thread name) throws: Error - if any thread that is already running has thrown an Error. throws: NullPointerException - if job is null. since: ostermillerutils 1.05.00 |
run | public void run(ThreadGroup threadGroup, Runnable job, String threadName, long stackSize)(Code) | | Run the given job. The given job is either run
immediately or if the max number of concurrent jobs are already
running, it is queued to be run when some job is finished.
If this method throws an error, that
error may be handled and this method
may be called again as it will not re-throw the same
instance of the error.
Parameters: threadGroup - group in which this job should be run (null for default group). Parameters: job - job which is to be run in parallel with other jobs. Parameters: threadName - name for the thread that will be created to run the job (null for auto generated thread name) Parameters: stackSize - system dependent stack size suggestion for thread creation (0 for default stack size). throws: Error - if any thread that is already running has thrown an Error. throws: NullPointerException - if job is null. since: ostermillerutils 1.05.00 |
|
|