Class Parallelizer
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.- Since:
- ostermillerutils 1.05.00
- Author:
- Matt Conway, Stephen Ostermiller - https://ostermiller.org/contact.pl?regarding=Java+Utilities
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intConstant 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
ConstructorsConstructorDescriptionCreate 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. -
Method Summary
Modifier and TypeMethodDescriptionbooleandone()Return true iff all jobs that have been requested to run in this Parallelizer have completed.voidDump the stack of each running thread.Thread[]Gets a list of all running threads.voidAll currently running threads will be interrupted.voidjoin()Block until all the jobs in this Parallelizer have run and then return.voidRun the given job.voidRun the given job.voidrun(ThreadGroup threadGroup, Runnable job) Run the given job.voidrun(ThreadGroup threadGroup, Runnable job, String threadName) Run the given job.voidrun(ThreadGroup threadGroup, Runnable job, String threadName, long stackSize) Run the given job.
-
Field Details
-
INFINITE_THREAD_LIMIT
public static final int INFINITE_THREAD_LIMITConstant 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
- See Also:
-
-
Constructor Details
-
Parallelizer
public Parallelizer()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) 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 allowed to run simultaneously or INFINITE_THREAD_LIMIT for no limit.- Throws:
IllegalArgumentException- if concurrentThreadLimit not a whole number or INFINITE_THREAD_LIMIT- Since:
- ostermillerutils 1.05.00
-
-
Method Details
-
run
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.NullPointerException- if job is null.- Since:
- ostermillerutils 1.05.00
-
run
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.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.NullPointerException- if job is null.- Since:
- ostermillerutils 1.05.00
-
run
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).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.NullPointerException- if job is null.- Since:
- ostermillerutils 1.05.00
-
run
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).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)- Throws:
Error- if any thread that is already running has thrown an Error.NullPointerException- if job is null.- Since:
- ostermillerutils 1.05.00
-
run
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).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).- Throws:
Error- if any thread that is already running has thrown an Error.NullPointerException- if job is null.- Since:
- ostermillerutils 1.05.00
-
done
public boolean done()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.
- Returns:
- Whether all jobs are done or not.
- Throws:
Error- if any of the running threads has thrown an Error.- Since:
- ostermillerutils 1.05.00
-
interrupt
public void interrupt()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
-
dumpStack
public void dumpStack()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
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.
- Returns:
- an array of all currently running threads.
- Throws:
Error- if any of the running threads has thrown an Error.- Since:
- ostermillerutils 1.05.00
-
join
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.RuntimeException- any running thread throws or has thrown a runtime exception.Error- if any of the running threads throws or has thrown an Error.- Since:
- ostermillerutils 1.05.00
-