Migrate from GitHub to SourceForge with this tool. Check out all of SourceForge's recent improvements.
Close

Parallel.ForEach

Parallel.ForEach

Parallel.ForEach loop is allmost the same as the sequantial foreach-loop, it executes all iterations in diffrent threads. In this article you can read about all features provided by Parallel.ForEach construct. In our example section you can find many examples how to use the ParallelForEach.

Comparison of parallel and sequential foreach-loops

Parallel.ForEach loop iterates over a collection parallel and thus executes the iteration-code for many items at the same time, in diffrent threads. Below is presented a comparisonbetween sequential and parallel foreach-loops.

//create collection
Collection c = 
    Arrays.asList(new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9});

//execute sequantial-loop
System.out.println("simple foreach-loop: ");
for(Integer i:c){
    System.out.print(i + ", ");
}

//execute parallel-loop
System.out.println("\nparallel foreach-loop: ");
Parallel_1x0.ForEach(c, new ForEachTask_1x0()
{
    public void iteration(Integer i)
    {
        System.out.print(i + ", ");
    }
});
}

The output of the code above is:

simple foreach-loop:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
parallel foreach-loop:
0, 1, 5, 6, 9, 4, 2, 3, 7, 8,

The array and the collection in java are diffrent things, but with Parallel.ForEach you can iterate in the same manner over any collection or array. The example below shows how to iterate over an array.

//create collection
Integer[] a = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

Parallel_1x0.ForEach(a, new ForEachTask_1x0()
{
    public void iteration(Integer i)
    {
        System.out.print(i + ", ");
    }
});

User controls the execution of the Parallel.ForEach-loop

We decided to improve the often implemented API of the Parallel.ForEach construct by giving more control to the user. For example if you create a user-interface where the user is able to solve complex mathematical formulas. The formulas can be parallelized by using Parallel.ForEach and be solved a lot faster. To enable the user to cancel the solving-process, a cancel-button was added to the user-interface.

Before the solving of the formulas can be started the programmer has to create an Parallel.ForEach loop, start it and register an ActionListener on the cancel-button. If the cancel button is pushed the Parallel.ForEach will be canceled. The canceling of the Parallel.ForEach means, that the Parallel.ForEach doesn't produce new tasks any more. For example if the Parallel.ForEach has to execute 1000 tasks and the user decide to cancel it after first 200 tasks were executed, the Parallel.ForEach don't start left 800 tasks and finishes.

Bellow is example presented how a Parallel.ForEach loop can be canceled. Here you can find the whole example-code.

//create formulas array
String[] formulas = new String[numberOfTasks];
...

//create Parallel.ForEach
final Loop_1x0 loop = Parallel_1x0.createForEach(    
        formulas,//array
        new ForEachTask_1x0(){
                public void iteration(final String formula) {
                    //solve complex formula here
                }
        });
//register start event
buttonStart.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        //start ParallelFor loop
        loop.start();
    }
});

//register cancel event
buttonCancel.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        //cancel ParallelFor loop
        loop.cancel();
        System.out.println("canceled");
    }
});

Grain - execute many iterations in a single task

Sometimes you need to execute many iterations, but you don't want to start many tasks to avoid unnecessary overhead. The implementation of Parallel.ForEach supports the grain-option where you can define how many iterations should be executed in a single task.

...
int grain=5;
//create array
Integer[] a = ...;
...
//execute parallel-foreach loop
System.out.println("\nparallel foreach-loop: ");
Parallel_1x0.ForEach(    a,//array 
                        grain,//grain
                        new ForEachTask_1x0()
                            {
                                public void iteration(Integer i)
                                {
                                    System.out.print(i + ", ");
                                }
                            });
                    }

Parallel.ForEach examples