com.Ostermiller.util Java Utilities


com.Ostermiller.util
Class Parallelizer

java.lang.Object
  extended by com.Ostermiller.util.Parallelizer

public class Parallelizer
extends Object

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.

Since:
ostermillerutils 1.05.00
Author:
Matt Conway - http://simplygenius.com/, Stephen Ostermiller - http://ostermiller.org/contact.pl?regarding=Java+Utilities

Field Summary
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
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.
 
Method Summary
 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.
 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(Runnable job)
          Run the given job.
 void run(Runnable job, String threadName)
          Run the given job.
 void run(ThreadGroup threadGroup, Runnable job)
          Run the given job.
 void run(ThreadGroup threadGroup, Runnable job, String threadName)
          Run the given job.
 void run(ThreadGroup threadGroup, Runnable job, String threadName, long stackSize)
          Run the given job.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INFINITE_THREAD_LIMIT

public static final 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.

Since:
ostermillerutils 1.05.00
See Also:
Constant Field Values
Constructor Detail

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 Detail

run

public void run(Runnable job)
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

public void run(Runnable job,
                String threadName)
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

public void run(ThreadGroup threadGroup,
                Runnable job)
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

public void run(ThreadGroup threadGroup,
                Runnable job,
                String threadName)
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

public void run(ThreadGroup threadGroup,
                Runnable job,
                String threadName,
                long stackSize)
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

public Thread[] 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

public void join()
          throws InterruptedException
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

com.Ostermiller.util Java Utilities


Copyright © 2001-2012 by Stephen Ostermiller