Ostermillerutil Java Utilities Parallelizer - com.Ostermiller.util Java Utilities

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.



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();

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

License

OstermillerUtil Java Utilities Copyright (c) 2001-2020 by Stephen Ostermiller and other contributors

The OstermillerUtils library is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

License FAQs - Why GPL? How about the LGPL or something else?