0

I would like to start to parallelize my Java code. This is my toy-problem. I have two vectors (double []) containing respectively the height and the base of several triangles. I have a class, Triangle, containing a method, computeArea, that computes the area of a triangle (height*base/2). For each triangle I have I want to store the area in a third vector (double []). Currently I have a for loop like this

for(int i=0; i<height.length; i++){
    
    area[i] = triangle.computeArea(height[i], base[i]);

}

Some suggestion on how to parallelize the code? Could you kindly provide me a simple example? I am using jdk 1.8. Please note that the values in the vector area must correspond to values in vectors height and base.

Thanks in advance

3
  • Does this answer your question? Java 8: Parallel FOR loop Commented Mar 1, 2022 at 7:50
  • 3
    Arrays.parallelSetAll(area, i -> triangle.computeArea(height[i], base[i])); Commented Mar 1, 2022 at 7:57
  • Thanks for your suggestions. I tried, but it seems to be slower compare to the for loop. I measured the time with System.currentTimeMillis(). Is there a better solution or what I am doing wrong? ``` double t = System.currentTimeMillis(); for(int k=0; k<KMAX; k++) { area[k] = tr.computeArea(height[k], base[k]); } double seq = System.currentTimeMillis() - t; t = System.currentTimeMillis(); Arrays.parallelSetAll(area, i -> tr.computeArea(height[i], base[i])); seq = System.currentTimeMillis() - t; with the for loop I get 170 while 230 with Arrays.parallelSetAll Commented Mar 1, 2022 at 11:27

0

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.