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

  • Does this answer your question? [Java 8: Parallel FOR loop](https://stackoverflow.com/questions/27558605/java-8-parallel-for-loop) – Turamarth Mar 01 '22 at 07:50
  • 3
    `Arrays.parallelSetAll(area, i -> triangle.computeArea(height[i], base[i]));` – shmosel Mar 01 '22 at 07: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 tr.computeArea(height[i], base[i])); seq = System.currentTimeMillis() - t; with the for loop I get 170 while 230 with Arrays.parallelSetAll – Niccolo Tubini Mar 01 '22 at 11:27

0 Answers0