java8 새로운 기능 -------------stream의 병렬 흐름 조작

7858 단어 java8
대상이 없으면 저의java8의 분류를 보십시오
병행하기 전에 파라엘 ()의 밑바닥 포크-join 모드를 알아보세요.
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;

/**
 * create gl 
 **/
public class ForkJoinCalculate extends RecursiveTask {

    private long start;
    private long end;
    private static final long THRESHOLD = 1000000;

    public ForkJoinCalculate(long start, long end) {
        this.start = start;
        this.end = end;
    }

    @Override
    protected Long compute() {
        long length = start - end;
        // 
        if(length <= THRESHOLD){
            long sum = 0;
            for (int i = 0;i <= end;i++){
                sum +=i;
            }
            return sum;
        }else {
             long middle = (start + end)/2;  // 
             ForkJoinCalculate left = new ForkJoinCalculate(start,middle);
             left.fork(); // , 

             ForkJoinCalculate right = new ForkJoinCalculate(middle+1,end);
             right.fork();
             return left.join() + right.join();
        }
    }

    public static void main(String[] args) {

        // fork - join   , 
        Instant start = Instant.now();// 

        ForkJoinPool forkJoinPool = new ForkJoinPool();
        ForkJoinTask forkJoinTask = new ForkJoinCalculate(0,1000000000L); //  , 
        Long sum = forkJoinPool.invoke(forkJoinTask);
        System.out.println(sum);

        Instant end = Instant.now();// 
        System.out.println(Duration.between(start,end).getSeconds()); //     getNano() 

    }
}
import java.util.stream.LongStream;

/**
 * create gl 
 **/
public class Test04 {



    public static void test1(){
        long reduce = LongStream.rangeClosed(0, 10000000000L)
                .parallel()
                .reduce(0, Long::sum);
        System.out.println(reduce);
    }


    public static void main(String[] args) {
        test1();
    }


}

좋은 웹페이지 즐겨찾기