<>ForkJoin

What is? ForkJoin

ForkJion stay JDK1.7, Parallel execution of tasks ! increase of efficiency . Big data !
big data :Map Reduce( Split big tasks into small ones )

ForkJion characteristic : work stealing
This is a two terminal queue ,A The thread did not finish executing ,B The thread is finished ,B Threads go implement A In ( Red box ) Unfinished task

ForkJion

Code case :
package com.forkjoin; import java.util.concurrent.ExecutionException; import
java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream; /** * 3000 6000(ForkJoin) 9000(Stream Parallel flow ) */
public class Test { public static void main(String[] args) throws
ExecutionException, InterruptedException { //test1();//8039 //test2(); //4369
test3(); //152 } public static void test1(){ Long sum =0L; long start = System.
currentTimeMillis(); for (Long i = 1L; i < 10_0000_0000; i++) { sum+=i; } long
end= System.currentTimeMillis(); System.out.println("sum="+sum+" time :"+(end -start
)); } public static void test2() throws ExecutionException, InterruptedException
{ Long sum =0L; long start = System.currentTimeMillis(); //forkJoin pond
ForkJoinPool forkJoinPool = new ForkJoinPool(); // Instantiate business method ForkJoinTask<Long>
task= new ForkJoinDemo(0L,10_0000_0000L); // Performing business in a pool ForkJoinTask<Long> submit =
forkJoinPool.submit(task); sum = submit.get(); long end = System.
currentTimeMillis(); System.out.println("sum="+sum+" time :"+(end -start)); } public
static void test3() throws ExecutionException, InterruptedException { Long sum =
0L; long start = System.currentTimeMillis(); //Stream Parallel flow range
Does not include the following ,rangeClosed Include the following sum = LongStream.rangeClosed(0L, 10_0000_0000L).
parallel().reduce(0, Long::sum); long end = System.currentTimeMillis(); System.
out.println("sum="+sum+" time :"+(end -start)); } } package com.forkjoin; import
java.util.concurrent.RecursiveTask; /** * * The task of summation ! * * 3000, 6000(ForkJoin)
9000(Stream Parallel flow ) * How to use forkJoin * 1.forkJoinPool Through it *
2. Computing tasks forkjoinPoolexecute * 3. Compute class to inherit ForkJoinTask * * Recursive : pronunciation rekusive *
*/ public class ForkJoinDemo extends RecursiveTask<Long> { private Long start;
private Long end; // critical value private Long temp = 10000L; public ForkJoinDemo(Long
start, Long end) { this.start = start; this.end = end; } public static void main
(String[] args) { // int sum =0; // for (int i = 0; i < 10_0000_0000; i++) { //
sum+=i; // } // System.out.println(sum); // // } public void test(){ //100 -50
>50 if((end -start) >temp){ int sum =0; for (int i = 0; i < 10_0000_0000; i++) {
sum+=i; } System.out.println(sum); } } // The return value of the calculation is RecursiveTask Generics of @Override
protected Long compute() { // If the range is larger than 1 We don't have to merge branches when we're still young if((end -start) <temp){ Long
sum=0L; for (long i =start; i <=end; i++) { sum+=i; } return sum; }else{
//forkjoin: A task is split into multiple tasks , Like recursion long middle = (start + end) / 2; // Median value ForkJoinDemo
task1= new ForkJoinDemo(start, middle); task1.fork(); // Split task , Push the task into the thread queue
ForkJoinDemo task2 = new ForkJoinDemo(middle+1, end); task2.fork(); return task1
.join() + task2 .join(); } } }

Technology