Skip to content

Latest commit

 

History

History
44 lines (31 loc) · 999 Bytes

fork-join-pool.md

File metadata and controls

44 lines (31 loc) · 999 Bytes

ForkJoinPool

关键字:

  • work-stealing algorithm

Basice Use

if (my portion of the work is small enough)
  do the work directly
else
  split my work into two pieces
  invoke the two pieces and wait for the results

RecursiveTask

class Fibonacci extends RecursiveTask<Integer> {  
      final int n;    
      Fibonacci(int n) {
         this.n = n; 
      }   
       protected Integer compute() {  
            if (n <= 1)        return n;   

            Fibonacci f1 = new Fibonacci(n - 1);  
            f1.fork();   

            Fibonacci f2 = new Fibonacci(n - 2);  
            return f2.compute() + f1.join();   
        }  
}

Links