public class Parallelizer
extends java.lang.Object
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.
Modifier and Type | Field and Description |
---|---|
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 and Description |
---|
Parallelizer()
Create a new Parallelizer with no limit on the number
of threads that will be allowed to be run concurrently.
|
Parallelizer(int concurrentThreadLimit)
Create a new Parallelizer with the specified limit on the number
of threads that will be allowed to be run concurrently.
|
Modifier and Type | Method and Description |
---|---|
boolean |
done()
Return true iff all jobs that have been requested to run
in this Parallelizer have completed.
|
void |
dumpStack()
Dump the stack of each running thread.
|
java.lang.Thread[] |
getRunningThreads()
Gets a list of all running threads.
|
void |
interrupt()
All currently running threads will be interrupted.
|
void |
join()
Block until all the jobs in this Parallelizer have run
and then return.
|
void |
run(java.lang.Runnable job)
Run the given job.
|
void |
run(java.lang.Runnable job,
java.lang.String threadName)
Run the given job.
|
void |
run(java.lang.ThreadGroup threadGroup,
java.lang.Runnable job)
Run the given job.
|
void |
run(java.lang.ThreadGroup threadGroup,
java.lang.Runnable job,
java.lang.String threadName)
Run the given job.
|
void |
run(java.lang.ThreadGroup threadGroup,
java.lang.Runnable job,
java.lang.String threadName,
long stackSize)
Run the given job.
|
public static final int INFINITE_THREAD_LIMIT
public Parallelizer()
public Parallelizer(int concurrentThreadLimit)
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.
concurrentThreadLimit
- number of threads that will be allowed
to run simultaneously or INFINITE_THREAD_LIMIT for no limit.java.lang.IllegalArgumentException
- if concurrentThreadLimit not a whole
number or INFINITE_THREAD_LIMITpublic void run(java.lang.Runnable job)
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.
job
- job which is to be run in parallel with other jobs.java.lang.Error
- if any thread that is already running has thrown an Error.java.lang.NullPointerException
- if job is null.public void run(java.lang.Runnable job, java.lang.String threadName)
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.
job
- job which is to be run in parallel with other jobs.threadName
- name for the thread that will be created to run the job (null for auto generated thread name)java.lang.Error
- if any thread that is already running has thrown an Error.java.lang.NullPointerException
- if job is null.public void run(java.lang.ThreadGroup threadGroup, java.lang.Runnable job)
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.
threadGroup
- group in which this job should be run (null for default group).job
- job which is to be run in parallel with other jobs.java.lang.Error
- if any thread that is already running has thrown an Error.java.lang.NullPointerException
- if job is null.public void run(java.lang.ThreadGroup threadGroup, java.lang.Runnable job, java.lang.String threadName)
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.
threadGroup
- group in which this job should be run (null for default group).job
- job which is to be run in parallel with other jobs.threadName
- name for the thread that will be created to run the job (null for auto generated thread name)java.lang.Error
- if any thread that is already running has thrown an Error.java.lang.NullPointerException
- if job is null.public void run(java.lang.ThreadGroup threadGroup, java.lang.Runnable job, java.lang.String threadName, long stackSize)
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.
threadGroup
- group in which this job should be run (null for default group).job
- job which is to be run in parallel with other jobs.threadName
- name for the thread that will be created to run the job (null for auto generated thread name)stackSize
- system dependent stack size suggestion for thread creation (0 for default stack size).java.lang.Error
- if any thread that is already running has thrown an Error.java.lang.NullPointerException
- if job is null.public boolean done()
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.
java.lang.Error
- if any of the running threads has thrown an Error.public void interrupt()
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.
java.lang.Error
- if any of the running threads has thrown an Error.public void dumpStack()
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.
java.lang.Error
- if any of the running threads has thrown an Error.public java.lang.Thread[] getRunningThreads()
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.
java.lang.Error
- if any of the running threads has thrown an Error.public void join() throws java.lang.InterruptedException
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.
java.lang.InterruptedException
- if interrupted while waiting.java.lang.RuntimeException
- any running thread throws or has thrown a runtime exception.java.lang.Error
- if any of the running threads throws or has thrown an Error.Copyright (c) 2001-2020 by Stephen Ostermiller