%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%>
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: