<%bte.doc super="item.bte" %> <%bte.tpl name=pageTitle%>Parallelizer<%/bte.tpl%> <%bte.tpl name=description%>Runs multiple jobs in parallel, n threads at a time, and waits until all threads are complete before continuing.<%/bte.tpl%> <%bte.tpl name=keywords%>parallel execution, parallel threads, thread utility<%/bte.tpl%> <%bte.tpl name=content%>

Example

Parallelizer pll = new Parallelizer();
for (int i=0; i<10; i++){
    final int j = i;
    pll.run(
        new Runnable(){
            System.out.println("Hello World " + j);
        }
    );
}
pll.join();

The Parallelizer class is a Java thread utility that allows one to easily convert their serial code to parallel code. The class would typically be used to execute each iteration of a loop at once rather than one after another. Good candidates for such an optimization would be when the order of execution does not matter and each iteration does slow operations such as sleeping or making network connections.

To use the Parallelizer, a developer would typically:

  1. Create a new Parallelizer before the loop.
  2. Put the contents of the loop inside an in-line Runnable instance.
  3. Copy all loop variables used inside the Runnable instance into finals
  4. Call join() after the loop to wait until the threads are done and it is safe to proceed

[Download /w Source | Browse Source | Documentation]

<%/bte.tpl%> <%/bte.doc%>