zoukankan      html  css  js  c++  java
  • GUC-14 ForkJoin

    import java.time.Duration;
    import java.time.Instant;
    import java.util.concurrent.ForkJoinPool;
    import java.util.concurrent.ForkJoinTask;
    import java.util.concurrent.RecursiveTask;
    import java.util.stream.LongStream;
    
    import org.junit.Test;
    
    public class TestForkJoinPool {
        
        public static void main(String[] args) {
            Instant start = Instant.now();
            
            ForkJoinPool pool = new ForkJoinPool();
            
            ForkJoinTask<Long> task = new ForkJoinSumCalculate(0L, 50000000000L);
            
            Long sum = pool.invoke(task);
            
            System.out.println(sum);
            
            Instant end = Instant.now();
            
            System.out.println("耗费时间为:" + Duration.between(start, end).toMillis());//166-1996-10590
        }
        
        @Test
        public void test1(){
            Instant start = Instant.now();
            
            long sum = 0L;
            
            for (long i = 0L; i <= 50000000000L; i++) {
                sum += i;
            }
            
            System.out.println(sum);
            
            Instant end = Instant.now();
            
            System.out.println("耗费时间为:" + Duration.between(start, end).toMillis());//35-3142-15704
        }
        
        //java8 新特性
        @Test
        public void test2(){
            Instant start = Instant.now();
            
            Long sum = LongStream.rangeClosed(0L, 50000000000L)
                                 .parallel()
                                 .reduce(0L, Long::sum);
            
            System.out.println(sum);
            
            Instant end = Instant.now();
            
            System.out.println("耗费时间为:" + Duration.between(start, end).toMillis());//1536-8118
        }
    
    }
    
    class ForkJoinSumCalculate extends RecursiveTask<Long>{
    
        /**
         * 
         */
        private static final long serialVersionUID = -259195479995561737L;
        
        private long start;
        private long end;
        
        private static final long THURSHOLD = 10000L;  //临界值
        
        public ForkJoinSumCalculate(long start, long end) {
            this.start = start;
            this.end = end;
        }
    
        @Override
        protected Long compute() {
            long length = end - start;
            
            if(length <= THURSHOLD){
                long sum = 0L;
                
                for (long i = start; i <= end; i++) {
                    sum += i;
                }
                
                return sum;
            }else{
                long middle = (start + end) / 2;
                
                ForkJoinSumCalculate left = new ForkJoinSumCalculate(start, middle); 
                left.fork(); //进行拆分,同时压入线程队列
                
                ForkJoinSumCalculate right = new ForkJoinSumCalculate(middle+1, end);
                right.fork(); //
                
                return left.join() + right.join();
            }
        }
        
    }
  • 相关阅读:
    不用第三个变量互换两变量的值的两种方法。
    struts2截取字符串
    js控制表格单双行颜色交替显示
    Shell字符串
    Shell数组:shell数组的定义、数组长度
    Shell注释
    Shell运算符:Shell算数运算符、关系运算符、布尔运算符、字符串运算符等
    Shell替换:Shell变量替换,命令替换,转义字符
    Shell特殊变量:Shell $0, $#, $*, $@, $?, $$和命令行参数
    Shell变量:Shell变量的定义、删除变量、只读变量、变量类型
  • 原文地址:https://www.cnblogs.com/surge/p/10476307.html
Copyright © 2011-2022 走看看