码迷,mamicode.com
首页 > 编程语言 > 详细

Java多线程之利用线程池并行计算例子

时间:2020-01-31 00:36:34      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:rtti   nasa   executors   call()   判断   current   ati   int   static   

 1 import java.util.ArrayList;
 2 import java.util.List;
 3 import java.util.concurrent.*;
 4 import java.util.concurrent.Callable;
 5 import java.util.concurrent.ExecutorService;
 6 import java.util.concurrent.Executors;
 7 import java.util.concurrent.Future;
 8 
 9 /**
10  * 并行计算例子
11  * nasa
12  */
13 public class ParallelComputing {
14 
15     public static void main(String[] args) {
16 
17         long startTime = System.currentTimeMillis();
18         List<Integer> results = getPrime(1, 200000);
19         long endTime = System.currentTimeMillis();
20         System.out.println("耗时:" + (endTime -startTime));
21 
22         final int cpuCoreNum = 4;
23 
24         ExecutorService service = Executors.newFixedThreadPool(cpuCoreNum);
25 
26         MyTask t1 = new MyTask(1, 80000);
27         MyTask t2 = new MyTask(80001, 130000);
28         MyTask t3 = new MyTask(130001, 170000);
29         MyTask t4 = new MyTask(170001, 200000); //为什么不平均分 和质数计算速度有关
30 
31         Future<List<Integer>> s1 = service.submit(t1);
32         Future<List<Integer>> s2 = service.submit(t2);
33         Future<List<Integer>> s3 = service.submit(t3);
34         Future<List<Integer>> s4 = service.submit(t4);
35 
36         startTime = System.currentTimeMillis();
37         try {
38             System.out.println(s1.get());
39             System.out.println(s2.get());
40             System.out.println(s3.get());
41             System.out.println(s4.get());
42         } catch (InterruptedException e) {
43             e.printStackTrace();
44         } catch (ExecutionException e) {
45             e.printStackTrace();
46         }
47         endTime = System.currentTimeMillis();
48         System.out.println("耗时:" + (endTime - startTime));
49         service.shutdown();
50     }
51 
52     static class MyTask implements Callable<List<Integer>> {
53 
54         int startPos, endPos;
55 
56         MyTask(int start, int end) {
57             this.startPos = start;
58             this.endPos = end;
59         }
60 
61         @Override
62         public List<Integer> call() throws Exception {
63             List<Integer> result = getPrime(startPos, endPos);
64             return result;
65         }
66     }
67 
68     /**
69      * 判断是不是素数或质数
70      * @param num
71      * @return
72      */
73     static boolean isPrime(int num) {
74         for (int i=2; i<=num/2; i++) {
75             if(num % i == 0) return false;
76         }
77         return true;
78     }
79 
80     /**
81      * 给一个范围判断这个范围内有多少个质数
82      * @param start
83      * @param end
84      * @return
85      */
86     static List<Integer> getPrime(int start, int end) {
87         List<Integer> results = new ArrayList<>();
88         for (int i=start; i<=end; i++) {
89             if(isPrime(i)) results.add(i);
90         }
91         return results;
92     }
93 
94 }

 

Java多线程之利用线程池并行计算例子

标签:rtti   nasa   executors   call()   判断   current   ati   int   static   

原文地址:https://www.cnblogs.com/mxh-java/p/12244321.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!