0

So what I am trying to do is sort an ArrayList using Fork/Join pool. The algorithm that I will use to sort doesn't matter: I just choosed a random one for here. What matters is how I am supposed to use the Recursive task with fork/join pool so that the ArrayList will keep on splitting until the ArrayList size reaches a certain number (like 1000) then it will perform the sorting and then it will join back into one ArrayList. Here is my code:

assignment5

public class assignment5 {

    ArrayList<Integer> numbers = new ArrayList<>();
//lets say this arraylist is full with random numbers

    public void run(){

        Instant start = Instant.now();

        MyRecursiveTask myRecursiveTask = new MyRecursiveTask(numbers);
        ArrayList<Integer> mergedResult = ForkJoinPool.invoke(myRecursiveTask);

        Instant end = Instant.now();

        Duration duration = Duration.between(start, end);
        System.out.println("Seconds: " + duration.getSeconds());

    }

}

over here in assignment5 gives me error on ForkJoinPool.invoke(myRecursiveTask); it says non static method invoke connot be reffereced from a static content

MyRecursiveTask

    private List<Integer> numbers;

    protected List<Integer> compute() {
        //numbers here is the same as in assignment 5
        //if work is above threshold, break tasks up into smaller tasks
        if(this.numbers.size() > 1000) {
            System.out.println("Splitting workLoad : " + this.numbers.size());

            List<MyRecursiveTask> subtasks = new ArrayList<MyRecursiveTask>();
            subtasks.addAll(createSubtasks());

            for(MyRecursiveTask subtask : subtasks){
                subtask.fork();
            }

            for(MyRecursiveTask subtask : subtasks) {
                subtask.join();
            }
            return numbers;

        } else {
            System.out.println("Doing workLoad myself: " + this.numbers.size());
            bubbleSort(numbers);
        }
        return numbers;
    }

    private List<MyRecursiveTask> createSubtasks() {
        List<MyRecursiveTask> subtasks = new ArrayList<MyRecursiveTask>();

        List<Integer> list1 = numbers.subList(0,numbers.size()/2);
        List<Integer> list2 = numbers.subList(numbers.size()/2, numbers.size());

        MyRecursiveTask subtask1 = new MyRecursiveTask(list1);
        MyRecursiveTask subtask2 = new MyRecursiveTask(list2);

        subtasks.add(subtask1);
        subtasks.add(subtask2);

        return subtasks;
    }
    public void bubbleSort(List<Integer> numbers){//bubble sort alg here}
}
  • 1
    _"over here on invoke it gives me error"_ - If you're getting an error, please include details about that error in your question. – Jordan Sep 27 '19 at 12:54
  • Please read "How to create a [mcve]". Then use the [edit] link to improve your question (do not add more information via comments). Otherwise we are not able to answer your question and help you. – GhostCat Sep 27 '19 at 13:33
  • And hint: read https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java ... you **absolutely** do not want to have any **print** statements happening when measuring performance. These prints are "pretty" expensive, and can greatly vary in duration. It might be a good idea to somehow "use" the result of that operation (so: printing it), but that should happen **after** you acquired that second timestamp! – GhostCat Sep 27 '19 at 13:35

1 Answers1

0

ForkJoinPool.invoke() is not a static method, but you are using as it was.

You need to create an instance of ForkJoinPool and call the invoke of that instance. Also you will need an explicit cast to make things work as you wanted (about that ArrayList).

Replace the line that gives you "error" with these ones and it'll work!

ForkJoinPool forkJoinPool = new ForkJoinPool();
ArrayList<Integer> mergedResult = (ArrayList<Integer>) forkJoinPool.invoke(myRecursiveTask);

Hope I helped!

PS: If you haven't done it already, I invite you to read the comments of the other users, you may find them helpful.

Sterconium
  • 559
  • 4
  • 20