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));
}
}