I wanted to learn parallel programming to speed up algorithms and choose Java.
I wrote two functions for summing the long integers in an array - one simple iterative array, the second - dividing the array into parts and summing the parts in separate streams.
I expected the logic to be about twice as high using two threads. However, I only have 24% speed. Moreover, using more threads, I do not get an improvement (possibly less than 1%) for two threads. I know that there should be thread creation / overhead pooling, but I think it shouldn't be that big.
Could you explain what I am missing or where is the error in the code? Here is the code:
import java.util.concurrent.ThreadLocalRandom; public class ParallelTest { public static long sum1 (long[] num, int a, int b) { long r = 0; while (a < b) { r += num[a]; ++a; } return r; } public static class SumThread extends Thread { private long num[]; private long r; private int a, b; public SumThread (long[] num, int a, int b) { super(); this.num = num; this.a = a; this.b = b; } @Override public void run () { r = ParallelTest.sum1(num, a, b); } public long getSum () { return r; } } public static long sum2 (long[] num, int a, int b, int threadCnt) throws InterruptedException { SumThread[] th = new SumThread[threadCnt]; int i = 0, c = (b - a + threadCnt - 1) / threadCnt; for (;;) { int a2 = a + c; if (a2 > b) { a2 = b; } th[i] = new SumThread(num, a, a2); th[i].start(); if (a2 == b) { break; } a = a2; ++i; } for (i = 0; i < threadCnt; ++i) { th[i].join(); } long r = 0; for (i = 0; i < threadCnt; ++i) { r += th[i].getSum(); } return r; } public static void main(String[] args) throws InterruptedException { final int N = 230000000; long[] num = new long[N]; for (int i = 0; i < N; ++i) { num[i] = ThreadLocalRandom.current().nextLong(1, 9999); }
EDIT: I have an i7 processor with 4 cores (8 threads). The output given by the code is:
1149914787860 175689196 1149914787860 149224086 1149914787860 147709988 1149914787860 138243999
source share