4

I have a for loop in java with the following structure that I need to parallelize into a fixed number of threads. Lets say that number is in numThreads. The for loop is given below. Note that only the outer loop needs to be parallelized. I think I need to use Executor but I can't figure out how to split the workload into different threads, and if that will change my indexes inside the loop in statements like isInsulator[x][y]. basically I want different threads splitting the value of X and running a for loop for those x values assigned to them. Does that make sense? Can anyone help me achieve this or maybe push me in the right direction please?

5
  • 1
    Unrelated: dont use empty lines just because you can. You use vertical spacing to group things somehow. Just having empty lines all over the place doesnt help your human readers. To the contrary. Empty lines are fine, but as non-empty lines, you use them because it makes sense, not because you can! Commented Oct 4, 2018 at 8:46
  • are the arrays (isInsulator, isHeater, nextArr and currArr) class variable or local variable? Commented Oct 4, 2018 at 8:46
  • 1
    Can you tell a bit more on the method giveHeat do ? Is it thread safe ? Commented Oct 4, 2018 at 9:36
  • Does giveHeat() make use of the results from the previous iteration? (I suspect it might do as you are passing x - 1.) If so, then you can't use parallel processing. Commented Oct 4, 2018 at 11:26
  • The arrays isInsulator, isHeater, nextArr and currArr are class variables. giveHeat() is not thread safe. However, I'm not worried about that at the moment. I will be changing the call to avoid race condition. Commented Oct 4, 2018 at 18:59

2 Answers 2

1

You can declare a fixed thread pool (it will initialize and use only the specified number of threads):

Executor executor = Executors.newFixedThreadPool(numThreads);

You have to submit X tasks to the thread pool:

for (int i = 0; i < X; i++) {
        final int x = i;
        executor.execute(() -> submitTask(x));
}

In the method submitTask you can define your logic:

private void submitTask(final int x) {
    for (int y = 0; y < Y; y++) {
        //do stuff
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1
  1. Write a benchmark (so you can prove it's really faster with parallel processing), use JMH.
  2. Rewrite the code to be a produce_inputs->process->collect_or_summarize pipeline
  3. Use parallel Streams API (it uses an internal fork-join pool tuned to the number of CPUs on the box).
  4. Compare the performance of sequential vs paralle processing.
int result = IntStream.generate(() -> 42) // lazy inputs source
        .limit(100) // limit number of inputs
        .parallel() // use parallel processing
//      .sequential() // ...or sequential processing
        .map(x -> x + 1) // do the processing
        .reduce(0, Math::addExact); // summarize the result
//      .collect(toList()); // ...or just collect it to a container

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.