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?
2 Answers
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
}
}
Comments
- Write a benchmark (so you can prove it's really faster with parallel processing), use JMH.
- Rewrite the code to be a
produce_inputs->process->collect_or_summarizepipeline - Use parallel
Streams API (it uses an internal fork-join pool tuned to the number of CPUs on the box). - 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
giveHeat()make use of the results from the previous iteration? (I suspect it might do as you are passingx - 1.) If so, then you can't use parallel processing.