Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.www;
- /**
- * Created by shsethi on 9/20/17.
- */
- import java.util.Arrays;
- import java.util.List;
- import java.util.concurrent.ForkJoinPool;
- public class Java8Func {
- private static List<String> list = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8");
- public static void main(String[] args) throws Exception {
- final int[] sum = {0};
- ForkJoinPool forkJoinPool = new ForkJoinPool(4);
- long startTime = System.currentTimeMillis();
- forkJoinPool.submit(() -> {
- {
- sum[0] = list.parallelStream()
- .map(Integer::parseInt)
- .map(x -> {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("Hello " + x + " : " + Thread.currentThread().getName());
- return x;
- })
- .reduce(Integer::sum).get();
- System.out.println("Time taken = " + (System.currentTimeMillis() - startTime));
- System.out.println(sum[0]);
- }
- });
- // System.out.println("Time taken = " + (System.currentTimeMillis() - startTime));
- Thread.sleep(10000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement