用多线程计算1到100的和

1.使用Thread方式

思路:使用CountDownLatch保证拿到所有线程的计算结果。

  • 子线程类:
public class AddOperationThread implements Runnable {

    private int start;
    private int end;
    private CountDownLatch countDownLatch;
    private int sum;

    public AddOperationThread(int start, int end, CountDownLatch countDownLatch) {
        this.start = start;
        this.end = end;
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        for (int i = start; i <= end; i++) {
            sum = sum + i;
        }
        countDownLatch.countDown();
    }

    public int getSum() {
        return sum;
    }

}
  • 主线程类:
public class AddOperationThreadTest {

    public static void main(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();
        int sum = 0;
        int threadNum = 10;
        int count = 10;
        CountDownLatch countDownLatch = new CountDownLatch(threadNum);
        AddOperationThread[] addThreads = new AddOperationThread[threadNum];
        for (int i = 1; i <= threadNum; i++) {
            addThreads[i - 1] = new AddOperationThread((i - 1) * count + 1, count * i, countDownLatch);
            new Thread(addThreads[i - 1]).start();
        }
        countDownLatch.await();
        for (int i = 1; i <= threadNum; i++) {
            sum = sum + addThreads[i - 1].getSum();
        }
        System.out.println(sum);
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }

}

2.使用线程池方式

思路:应用线程池的future模式获取所有线程的计算结果。

  • 子线程类:
public class AddOperationPool implements Callable {

    private int start;
    private int end;
    private int sum;

    public AddOperationPool(int start, int end) {
        this.start = start;
        this.end = end;
    }

    @Override
    public Integer call() throws Exception {
        for (int i = start; i <= end; i++) {
            sum = sum + i;
        }
        return sum;
    }

}
  • 主线程类:
public class AddOperationPoolTest {

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        long start = System.currentTimeMillis();
        int sum = 0;
        int threadNum = 10;
        int count = 10;
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);
        List> futures = new ArrayList<>();
        for (int i = 1; i <= threadNum; i++) {
            AddOperationPool addOperationPool = new AddOperationPool((i - 1) * count + 1, count * i);
            Future submit = fixedThreadPool.submit(addOperationPool);
            futures.add(submit);
        }
        if (null != futures && !futures.isEmpty()) {
            for (Future future : futures) {
                Integer result = (Integer) future.get();
                sum = sum + result;
            }
        }
        System.out.println(sum);
        fixedThreadPool.shutdown();
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }

}

你可能感兴趣的:(用多线程计算1到100的和)