Search code examples
javathresholdfork-joinforkjoinpool

Which number of threshold will be better in Java7 ForkJoinTask


I was trying out the Java ForkJoin framework and wrote a program that to process a large data list.

It is well known that the field threshold is always set in ForkJoinTask to point out the minimum number for the partition of data list.

The question is, how big or small of threshold will make better performance, or is flexible and only associated with the core number of CPU or threads support?

Is there a best practice for threshold in parallel compute framework such as Forkjointask?


Solution

  • There is no set rule for threshold. A good number depends on the number of elements in the array (N), the type of processing for each element (Q) (doing a simple compare of two number is a low Q, doing an intricate calculation is a high Q.)

    I use a general formula that works fairly well most of the time when I don't always know Q: I want to generate about 8 times as many tasks as threads or a minimum threshold of 32k (depending on N of course.)

    int temp = count / (threads << 3);
    threshold = (temp < 32768) ? 32768 : temp;
    

    Where count is N and threads is the number of threads.