0

I have big working on a different problem now with JAVA. I am making a array with 100 million random numbers between 1 and 10. Then I am Computing the sum in parallel using multiple threads. Then computing the sum with only one thread. Since JAVA 8 I have noticed the Array.stream() method has a built in thread utilization process. I am trying to utilize it based on the documentation however the timing is way off with the parallel array stream answer compared to the sequencing answer. I am just curious at this point what you may think I am missing in my logic. My code is posted below:

import java.util.Arrays;
import java.util.Random;

public class MultithreadThing {

public static int findSum(int[] arr) {
    int sum = 0;
    for (int value : arr) {
        sum += value;
    }
    return sum;
}

// Driver method
public static void main(String[] args) {
    
    Random rand = new Random();
    
    int arr[] = new int[100000000];
    
    for(int i=0; i < arr.length; i++) {
        arr[i] = rand.nextInt(10) + 1;
    
    }   
    
    int sum = 0;
    long startTime2 = System.nanoTime();
    
    sum = Arrays.stream(arr).parallel().sum();
    System.out.println("Parallel: " + (System.nanoTime() - startTime2));
            
    long startTime = System.nanoTime();
    MultithreadThing.findSum(arr);
    System.out.println("Single: " + (System.nanoTime() - startTime));
            
}
}
  • 1
    Neither of your examples use concurrency. Your Stream is sequential. – Hulk Oct 26 '22 at 17:46
  • 1
    `Arrays.stream()` does not provide parallel streams and streams have more overhead thus not very efficient for simple work. – Wilderness Ranger Oct 26 '22 at 17:46
  • `Arrays.stream(arr).sum()` is single-threaded. You can try `Arrays.stream(arr).parallel().sum()` (no guarantees). – knittl Oct 26 '22 at 17:46
  • 1
    btw, `Arrays.stream(arr);` in your initialisation loop does nothing and should be deleted. – Bohemian Oct 26 '22 at 17:55
  • As you are interested in performance, [this related question](https://stackoverflow.com/q/54580839/2513200) may also be worth reading. – Hulk Oct 26 '22 at 17:58
  • You can refer to [the document](https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#stream-int:A-): Streams returned by `Arrays.stream(arr)` is *sequential*. BTW you can also consider using a `List` and try `list.parallelStream()`. – Wilderness Ranger Oct 26 '22 at 18:02
  • I took out my previous code for parallel and used: sum = Arrays.stream(arr).parallel().sum(); but the timing problem still seems to be evident. I timed both methods separately and verified I still have some type of logic error with my code. – John Hendricks Oct 26 '22 at 18:29
  • I just changed the code in my original post to reflect updates given. – John Hendricks Oct 26 '22 at 18:35
  • I will attempt to look into the list solution now. Thank you! – John Hendricks Oct 26 '22 at 18:36
  • I very strongly recommend reading all of [this answer](https://stackoverflow.com/q/23170832/869736) and the answers to [this question](https://stackoverflow.com/q/20375176/869736). – Louis Wasserman Oct 26 '22 at 18:37
  • I thought at one point it may be an issue with my Macbook M1 pc and the available processors while using Eclipse but I have attempted this solution documented here: https://eddmann.com/posts/parallel-summation-in-java/ and the timing is perfect. I want to implement this new method while using parallel array stream solution. – John Hendricks Oct 26 '22 at 18:42

0 Answers0