Java线程池之ForkJoinPool

 1 import java.io.IOException;
 2 import java.util.Arrays;
 3 import java.util.Random;
 4 import java.util.concurrent.RecursiveAction;
 5 import java.util.concurrent.RecursiveTask;
 6 
 7 /**
 8  * 分割 合并 线程池
 9  * 类似于递归的归并排序,这个是多线程的递归排序
10  * jdk1.7之后加的
11  */
12 public class ForkJoinPool {
13 
14     static int[] nums = new int[1000000];
15     static final int MAX_NUM = 50000;
16     static Random random = new Random();
17 
18     static {
19         for(int i=0; i<nums.length; i++) {
20             nums[i] = random.nextInt(100);
21         }
22         System.out.println("总和1:" + Arrays.stream(nums).sum());
23     }
24 
25     /**
26      * ForkJoinPool里面必须是ForkJoinTask,
27      * 直接继承ForkJoinTask类写起来比较麻烦,
28      * 可以继承RecursiveAction(无返回值)和RecursiveTask(递归任务,有返回值)
29      */
30 //    static class AddTask extends RecursiveAction {
31 //
32 //        int start, end;
33 //
34 //        public AddTask(int start, int end) {
35 //            this.start = start;
36 //            this.end = end;
37 //        }
38 //
39 //        @Override
40 //        protected void compute() {
41 //            if(end-start <= MAX_NUM) {
42 //                long sum = 0L;
43 //                for(int i=start; i<end; i++) sum += nums[i];
44 //                System.out.println("from:" + start + " to:" + end + " =" + sum);
45 //            }else{
46 //                int middle = start + (end - start) / 2;
47 //
48 //                AddTask subTask1 = new AddTask(start, middle);
49 //                AddTask subTask2 = new AddTask(middle, end);
50 //                subTask1.fork();
51 //                subTask2.fork();
52 //            }
53 //        }
54 //    }
55 
56     /**
57      * 有返回值
58      */
59     static class AddTask extends RecursiveTask<Long> {
60 
61         int start, end;
62 
63         public AddTask(int start, int end) {
64             this.start = start;
65             this.end = end;
66         }
67 
68         @Override
69         protected Long compute() {
70             if(end-start <= MAX_NUM) {
71                 long sum = 0L;
72                 for(int i=start; i<end; i++) sum += nums[i];
73                 System.out.println("from:" + start + " to:" + end + " =" + sum);
74                 return sum;
75             }
76 
77             int middle = start + (end - start) / 2;
78             AddTask subTask1 = new AddTask(start, middle);
79             AddTask subTask2 = new AddTask(middle, end);
80             subTask1.fork();
81             subTask2.fork();
82             return subTask1.join() + subTask2.join();
83         }
84     }
85 
86     public static void main(String[] args) {
87 
88         java.util.concurrent.ForkJoinPool pool = new java.util.concurrent.ForkJoinPool();
89         AddTask task = new AddTask(0, nums.length);
90         pool.execute(task);  //精灵线程
91         System.out.println("总和2:" + task.join());
92         try {
93             System.in.read(); //阻塞主线程
94         } catch (IOException e) {
95             e.printStackTrace();
96         }
97     }
98 
99 }

 

posted @ 2020-01-30 23:09  尘世间迷茫的小书童  阅读(1306)  评论(0编辑  收藏  举报