0

I would like to make my code more implementable for others applications.

I don't have problems with the code, it works perfectly. I would just like to rewrite it to use it in other contexts.

My original code:

import java.util.concurrent.*;  
 class Sum extends RecursiveTask<Double> { 
 final int seqThresHold = 500; 
 double[] data;  
 int start, end; 
 Sum(double[] vals, int s, int e ) { 
    data = vals; 
    start = s; 
    end = e; 
  } 
 protected Double compute() { 
    double sum = 0; 
 if((end - start) < seqThresHold) { 
      for(int i = start; i < end; i++) sum += data[i]; 
    } 
    else { 
      int middle = (start + end) / 2; 
 Sum subTaskA = new Sum(data, start, middle); 
      Sum subTaskB = new Sum(data, middle, end); 
 subTaskA.fork(); 
      subTaskB.fork(); 
 sum = subTaskA.join() + subTaskB.join(); 
    } 
      return sum; 
  } 
} 
 class RecurTaskDemo {  
  public static void main(String args[]) {  
    ForkJoinPool fjp = new ForkJoinPool(); 
 double[] nums = new double[5000]; 
 for(int i=0; i < nums.length; i++) 
      nums[i] = (double) (((i%2) == 0) ? i : -i) ; 
 Sum task = new Sum(nums, 0, nums.length); 
 double summation = fjp.invoke(task); 
 System.out.println("Summation " + summation); 
  }  
}

The new code I would like to get:

public class RecurTaskDemo {

    private static double Sum(double[] data, int start, int end) {
        double sum = 0;
        for (int i = start; i < end; i++) {
            sum += data[i];
        }
        return sum;
    }

    // ???

    public static void main(String args[]) {

        double[] nums = new double[4000];
        for (int i = 0; i < nums.length; i++)
            nums[i] = (double) (((i % 2) == 0) ? i : -i);

        double sum = Sum(nums, 0, 4000);

        // double sum = ... I would like to calculate 'sum' using 'Sum(...)' + '???'

        System.out.println(sum);

    }

}
nhgrif
  • 61,578
  • 25
  • 134
  • 173
user3212453
  • 11
  • 1
  • 6
  • This question appears to be off-topic because _it works perfectly_. Try Code Review. – Boris the Spider Jan 19 '14 at 15:43
  • 1
    I've read the "new code" a couple of times, and I cannot figure out what you are trying to do here. – Stephen C Jan 19 '14 at 15:46
  • Thanks for your answer. I would like to convert the content of 'if' in a method and is taken off from 'Sum' in such a way as to exploit the parallel programming for each method without having to rewrite all of the code twice. If I'm wrong section of the forum tell me how I can fix it. If my request is not appreciated I delete all and I am sorry. – user3212453 Jan 19 '14 at 18:06
  • Here's an example: If I have to create a method to reverse strings contained in a 10x10 matrix should I replace the content of 'if' loop that calculates the sum and copy new code to reverse strings. I would like to create a method that uses all cores of my pc and that is adaptable to any type of method (sum, reverse strings, square roots, etc, ...). With my first code, I have too many lines that they repeat. – user3212453 Jan 19 '14 at 18:22

0 Answers0