ForkJoinPool 分支/合并框架

ForkJoinPool 分支/合并框架

一、Fork/Join框架简介

  Fork/Join 框架就是在必要的情况下,将一个大任务,进行拆分(fork)成若干个小任务(拆到不可再拆时),再将一个个的小任务运算的结果进行join 汇总。

  如下图所示:

二、Fork/Join 框架与线程池的区别

  采用“工作窃取”模式(work-stealing):
  当执行新的任务时它可以将其拆分分成更小的任务执行,并将小任务加到线程队列中,然后再从一个随机线程的队列中偷一个并把它放在自己的队列中。

  相对于一般的线程池实现,fork/join框架的优势体现在对其中包含的任务的处理方式上.在一般的线程池中,如果一个线程正在执行的任务由于某些原因无法继续运行,那么该线程会处于等待状态。而在fork/join框架实现中,如果某个子问题由于等待另外一个子问题的完成而无法继续运行。那么处理该子问题的线程会主动寻找其他尚未运行的子问题来执行.这种方式减少了线程的等待时间,提高了性能。

三、示例代码

  对0L,到50000000000L进行求和

  1 package me.concurrent.fjp;
  2 
  3 import java.time.Duration;
  4 import java.time.Instant;
  5 import java.util.concurrent.ForkJoinPool;
  6 import java.util.concurrent.ForkJoinTask;
  7 import java.util.concurrent.RecursiveTask;
  8 import java.util.stream.LongStream;
  9 
 10 import org.junit.Test;
 11 
 12 public class TestForkJoinPool {
 13 
 14     public static void main(String[] args) {
 15     Instant start = Instant.now();
 16 
 17     ForkJoinPool pool = new ForkJoinPool();
 18 
 19     ForkJoinTask<Long> task = new ForkJoinSumCalculate(0L, 50000000000L);
 20 
 21     Long sum = pool.invoke(task);
 22 
 23     System.out.println(sum);
 24 
 25     Instant end = Instant.now();
 26 
 27     System.out.println("耗费时间为:" + Duration.between(start, end).toMillis());// 166-1996-10590
 28     }
 29 
 30     @Test
 31     public void test1() {
 32     Instant start = Instant.now();
 33 
 34     long sum = 0L;
 35 
 36     for (long i = 0L; i <= 50000000000L; i++) {
 37         sum += i;
 38     }
 39 
 40     System.out.println(sum);
 41 
 42     Instant end = Instant.now();
 43 
 44     System.out.println("耗费时间为:" + Duration.between(start, end).toMillis());// 35-3142-15704
 45     }
 46 
 47     // java8 新特性
 48     @Test
 49     public void test2() {
 50     Instant start = Instant.now();
 51 
 52     Long sum = LongStream.rangeClosed(0L, 50000000000L).parallel().reduce(0L, Long::sum);
 53 
 54     System.out.println(sum);
 55 
 56     Instant end = Instant.now();
 57 
 58     System.out.println("耗费时间为:" + Duration.between(start, end).toMillis());// 1536-8118
 59     }
 60 
 61 }
 62 
 63 class ForkJoinSumCalculate extends RecursiveTask<Long> {
 64 
 65     private static final long serialVersionUID = -259195479995561737L;
 66 
 67     private long start;
 68     private long end;
 69 
 70     private static final long THURSHOLD = 10000L; // 临界值
 71 
 72     public ForkJoinSumCalculate(long start, long end) {
 73     this.start = start;
 74     this.end = end;
 75     }
 76 
 77     @Override
 78     protected Long compute() {
 79     long length = end - start;
 80 
 81     if (length <= THURSHOLD) {
 82         long sum = 0L;
 83 
 84         for (long i = start; i <= end; i++) {
 85         sum += i;
 86         }
 87 
 88         return sum;
 89     } else {
 90         long middle = (start + end) / 2;
 91 
 92         ForkJoinSumCalculate left = new ForkJoinSumCalculate(start, middle);
 93         left.fork(); // 进行拆分,同时压入线程队列
 94 
 95         ForkJoinSumCalculate right = new ForkJoinSumCalculate(middle + 1, end);
 96         right.fork(); //
 97 
 98         return left.join() + right.join();
 99     }
100     }
101 
102 }
View Code

  main方法运行结果

  -4378596987249509888
  耗费时间为:36950(毫秒)

  CPU利用率几乎100%!!!

如果,您对我的这篇博文有什么疑问,欢迎评论区留言,大家互相讨论学习。
如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】。
如果,您希望更容易地发现我的新博客,不妨点击一下左下角的【关注我】。
如果,您对我的博文感兴趣,可以关注我的后续博客,我是【AlbertRui】。

转载请注明出处和链接地址,欢迎转载,谢谢!

 

posted on 2018-02-02 20:07  Ryan520  阅读(407)  评论(0编辑  收藏  举报

导航