打印1到1亿的平方

 1 public class CalcTest{
 2 
 3     private volatile int beforeValue = 0;//最后一个启动线程需要计算范围的的起始值
 4     private volatile boolean isStop = false;//是否完成了所有的输出
 5     private volatile boolean isChangeBeforeValue = true;//是否有线程启动成功,并改变了beforeValue
 6     private volatile int threadCount = 0;//所有线程总数
 7     private volatile Integer nowThreadCount = 0;//当前计算线程总数
 8     private final int minValue = 0;//范围最小值
 9     private final int maxValue = 100000000;//范围最大值
10     private final int speed = 10000;//每一个线程需要计算数字的个数,改值设置的越小,每一个子线程进行计算所需时间就会相应减少,当前处于计算的总子线程数也就相应减小
11     private LinkedBlockingQueue<String> result = new LinkedBlockingQueue<>();//计算结果存放队列
12 
13     @Test
14     public void test_calc() {
15         Timestamp startTime = new Timestamp(System.currentTimeMillis());//开始时间
16         final ExecutorService calcThreadPool = Executors.newFixedThreadPool(1);//进行拆分计算的固定缓存池
17         calcThreadPool.execute(new Runnable() {
18             @Override
19             public void run() {
20                 calc();
21                 calcThreadPool.shutdown();
22             }
23         });
24         //从结果队列中时时取出结果
25         String nextResult;
26         while (!isStop || !result.isEmpty()) {
27             nextResult = result.poll();
28             if (nextResult == null) {
29                 continue;
30             }
31             System.out.println("now calc thread total count:" + nowThreadCount + " , now beforeValue:" + beforeValue + " ," + nextResult);
32         }
33         //计算完了统计一下
34         System.out.println("------------------- ok ---------------------");
35         System.out.println(result);
36         Timestamp endTime = new Timestamp(System.currentTimeMillis());
37         System.out.println(startTime + "-->" + endTime);
38         System.out.println("共开起线程总数:" + threadCount);
39         System.out.println((endTime.getTime() - startTime.getTime()) / 1000 + "秒");
40     }
41 
42     public void calc() {
43         beforeValue = minValue;
44         while (true) {
45             if (isChangeBeforeValue) {
46                 threadCount++;
47                 isChangeBeforeValue = false;
48                 try {
49                     synchronized (nowThreadCount) {
50                         nowThreadCount++;
51                     }
52                     final ExecutorService calcThreadPool = Executors.newCachedThreadPool();
53                     calcThreadPool.execute(new Runnable() {
54                         @Override
55                         public void run() {
56                             int firstValue = beforeValue + 1;
57                             int lastValue = firstValue + speed;
58                             final int endValue = (lastValue > maxValue ? maxValue : lastValue);
59                             isStop = endValue == maxValue;
60                             beforeValue = endValue;
61                             isChangeBeforeValue = true;
62                             StringBuilder sb = new StringBuilder();
63                             for (int i = firstValue; i <= endValue; i++) {
64                                 String str = " " + i + "平方:" + Math.pow(i, 2);
65                                 sb.append(str);
66                             }
67                             try {
68                                 result.put(" calc thread " + Thread.currentThread().getId() + " => " + sb.toString());
69                                 sb.setLength(0);
70                             } catch (InterruptedException e) {
71                                 e.printStackTrace();
72                             }
73                             synchronized (nowThreadCount) {
74                                 nowThreadCount--;
75                             }
76                             calcThreadPool.shutdown();
77                         }
78                     });
79                 } catch (Exception e) {
80                     System.out.println(e.getMessage());
81                     break;
82                 }
83             }
84             if (isStop && !result.isEmpty()) {
85                 break;
86             }
87         }
88     }
89 }
90   
View Code
原文地址:https://www.cnblogs.com/cz-xjw/p/8474697.html