zoukankan      html  css  js  c++  java
  • 【转】Fork/Join框架测试

    Fork/Join框架介绍

    下面使用该框架计算0-50000000000的和,并比较普通计算方法、Fork/Join框架、Java8新特性三种计算方式的计算时间:

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

     转载自:http://blog.csdn.net/xiangwanpeng/article/details/54977709

  • 相关阅读:
    摄像头标定GML Camera Calibration
    joda.money.Money
    aop the less note 1
    good notes
    yingyu biji 1
    flowable note1
    activiti note1
    CET4/6
    Web应用界面好帮手!DevExtreme React和Vue组件全新功能上线
    Web UI开发推荐!Kendo UI for jQuery自定义小部件——处理事件
  • 原文地址:https://www.cnblogs.com/ccfdod/p/6414676.html
Copyright © 2011-2022 走看看