java - ForkJoinTask understanding -


what happens after subtask.fork() returns forkjointask<int> object? compute() method invoked many times after , how?

i'm new java , i'm trying learn concept of fork/join framework. saw code below online. know after reading java api subtask.fork() returns object of forkjointask<v>, forkjointask<int> in case. couldn't understand happens after that? output indicate compute() method have been invoked many times after , how?

public class myrecursiveaction extends recursiveaction {      public static void main(string[] args) {         myrecursiveaction mra1 = new myrecursiveaction(100);         forkjoinpool fjp1 = new forkjoinpool();         fjp1.invoke(mra1);     }      private long workload = 0;      public myrecursiveaction(long workload) {         this.workload = workload;     }      @override     protected void compute() {          //if work above threshold, break tasks smaller tasks         if(this.workload > 16) {             system.out.println("splitting workload : " + this.workload);              list<myrecursiveaction> subtasks =                 new arraylist<myrecursiveaction>();              subtasks.addall(createsubtasks());              for(recursiveaction subtask : subtasks){                 subtask.fork();             }          } else {             system.out.println("doing workload myself: " + this.workload);         }     }      private list<myrecursiveaction> createsubtasks() {         list<myrecursiveaction> subtasks =             new arraylist<myrecursiveaction>();          myrecursiveaction subtask1 = new myrecursiveaction(this.workload / 2);         myrecursiveaction subtask2 = new myrecursiveaction(this.workload / 2);          subtasks.add(subtask1);         subtasks.add(subtask2);          return subtasks;     }  } 

output:

splitting workload : 100 splitting workload : 50 splitting workload : 50 splitting workload : 25 doing workload myself: 12 doing workload myself: 12 splitting workload : 25 doing workload myself: 12 doing workload myself: 12 splitting workload : 25 doing workload myself: 12 splitting workload : 25 doing workload myself: 12 doing workload myself: 12 doing workload myself: 12 

you need call invokeall in order split work in 2 pieces instead of doing manually, 2 new threads created 'compute' result, , if it's not small enough 2 new threads , on..,

you can't predict in order thread executed messages order vary every time run code.

public class myrecursiveaction extends recursiveaction {      public static void main(string[] args) {         myrecursiveaction mra1 = new myrecursiveaction(100);         forkjoinpool fjp1 = new forkjoinpool();         fjp1.invoke(mra1);     }      private long workload = 0;      public myrecursiveaction(long workload) {         this.workload = workload;     }      @override     protected void compute() {          //if work above threshold, break tasks smaller tasks         if(this.workload > 16) {             system.out.println("splitting workload : " + this.workload);              invokeall(new myrecursiveaction(this.workload / 2), new myrecursiveaction(this.workload / 2));          } else {             system.out.println("doing workload myself: " + this.workload);         }     }  } 

java doc of forkjointask


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -