Search code examples
javaarraysparallel-processingexecutorservicecallable

Java: Processing an array in parallel, find which position an exception occurred


First, I have an array which is filled with 0's except for position 5, which is filled with "a" (put there intentionally to throw an NumberFormatException).

And then I call testMethod passing the array, the size of the array, and how many Callables there will be.

In this example the array has a size of 10, and there are 4 callables.. the array is processed in chunks:

First chunk is positions 0 and 1 Second chunk is positions 2 and 3 Third chunk is positions 4 and 5 Fourth chunk is positions 6 and 7 Fifth chunk is positions 8 and 9 Sixth chunk is position 10

I need to find out which position the NumberFormatException occurs, or in a more general sense: I need to know what the position is when any exception occurs.

So I can print out in the message "Execution exception occurred at position 5"

I'm quite new to using ExceutorService/Callables so I'm not quite sure how to achieve this...

And if it's impossible to achieve using my current set up... is there a similar way to do this parallel processing that will also give me the ability to find the position where the exception occurred?

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class ThreadTest {

    private final static ArrayList<Callable<Boolean>> mCallables = new ArrayList<>();
    private final static ExecutorService mExecutor = Executors.newFixedThreadPool(4);

    public static void main(String[] args) throws Exception {
        /*Fill the array with 0's, except for position 5 which is a letter and will throw number format exception*/
        final String[] nums = new String[10];
        for (int i = 0; i < 5; i++) {
            nums[i] = "0";
        }
        nums[5] = "a";
        for (int i = 6; i < nums.length; i++) {
            nums[i] = "0";
        }

        testMethod(nums, 10, 4);
    }

    static void testMethod(String[] nums, int size, int processors) throws Exception {

        mCallables.clear();

        int chunk = (size / processors) == 0 ? size : size / processors;
        System.out.println("Chunk size: "+chunk);

        for (int low = 0; low < size; low += chunk) {

            final int start = low;
            final int end = Math.min(size, low + chunk);

            mCallables.add(new Callable<Boolean>() {

                @Override
                public Boolean call() throws Exception {

                    System.out.println("New call");
                    for (int pos = start; pos < end; pos++) {
                        System.out.println("Pos is " + pos);

                        System.out.println("Num is " + nums[pos]);
                        double d = Double.parseDouble(nums[pos]);

                    } //end inner loop


                    return true;
                } //end call method

            }); //end callable anonymous class
        }

        try {
            List<Future<Boolean>> f = mExecutor.invokeAll(mCallables);

            for (int i = 0; i < f.size(); i++) {
                f.get(i).get();
            }


        } catch (ExecutionException e) {
            String s = e.toString();
            System.out.println(s);
            System.out.println("Execution exception"); //need to write here which pos the numberFormat exception occurred 
        }


        mExecutor.shutdown();
    }
}

Solution

  • Can't you add a try/catch over the Double.parseDouble line and throw an exception which includes the position ?