• JDK5.0 ThreadPool 多线程计算


    package karl.test.threadpool;

    import java.util.concurrent.Callable;
    import java.util.concurrent.CompletionService;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.ExecutorCompletionService;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;

    public class ConcurrentCalculator2 {
        private ExecutorService exec;

        private CompletionService<Long> completionService;

        private int cpuCoreNumber;

        class SumCalculator implements Callable<Long> {
            private int[] numbers;

            private int start;

            private int end;

            public SumCalculator(int[] numbers, int start, int end) {
                this.numbers = numbers;
                this.start = start;
                this.end = end;
            }

            public Long call() throws Exception {
                Long sum = 0L;
                for (int i = start; i < end; i++) {
                    sum += numbers[i];
                }
                return sum;
            }

        }

        public ConcurrentCalculator2() {
            cpuCoreNumber = Runtime.getRuntime().availableProcessors();
            exec = Executors.newFixedThreadPool(cpuCoreNumber);
            completionService = new ExecutorCompletionService<Long>(exec);
        }

        public Long sum(final int[] numbers) {
            for (int i = 0; i < cpuCoreNumber; i++) {
                int increment = numbers.length / cpuCoreNumber + 1;
                int start = increment * i;
                int end = increment * i + increment;
                if (end > numbers.length) {
                    end = numbers.length;
                }
                SumCalculator subCalc = new SumCalculator(numbers, start, end);
                if (!exec.isShutdown()) {
                    completionService.submit(subCalc);
                }
            }
            return getResult();
        }

        public Long getResult() {
            Long result = 0L;
            for (int i = 0; i < cpuCoreNumber; i++) {
                try {
                    Long subSum = completionService.take().get();//take()已经完成的任务,如果暂时还没有完成的任务,take()方法也会阻塞。
                    result += subSum;
                } catch (InterruptedException e) {
                } catch (ExecutionException e) {
                }
            }
            return result;
        }

        public void close() {
            exec.shutdown();
        }

        public static void main(String[] args) {
            int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
            ConcurrentCalculator2 calc = new ConcurrentCalculator2();
            Long sum = calc.sum(numbers);
            System.out.println(sum);
            calc.close();
        }
    }
  • 相关阅读:
    js遍历table,gridview
    JavaScript和Webservice实现联动
    DataTable应用
    POJ1039+几何+直线于线段相交
    POJ2398+几何+排序
    HDU4506+快速求幂
    HDU4536+DFS
    POJ3304+几何
    POJ3555+几何+半平面交
    HDU4544+优先队列
  • 原文地址:https://www.cnblogs.com/zhonghan/p/2232675.html
Copyright © 2020-2023  润新知