0

this is how I can find sum of array. p[i] - array of random integers, size 1000

sum = 0;
for (int j = 1; j < p.length; j++ ) 
    {
            sum = sum + p[j];
    }

my question is how can I use multiple threads to perform it faster?

  • 1
    You think it will be really faster? – tilz0R May 29 '18 at 19:54
  • 2
    Possible duplicate of [What is the easiest way to parallelize a task in java?](https://stackoverflow.com/questions/2016083/what-is-the-easiest-way-to-parallelize-a-task-in-java) – Daniele May 29 '18 at 20:00

1 Answers1

1

Simply:

int sum = Arrays.stream(p).parallel().sum();
Jean Logeart
  • 50,693
  • 11
  • 81
  • 116