2020-10-26 多线程学习2

40 阅读1分钟

十个线程分别计算1-10.11-20......91-100,最后计算总和是多少?
分析:首先应该有十个线程,然后每个线程计算一部分,将十个线程的结果相加即可。

给出几种写法:
第一种:sync关键字

public class 十个线程 {

    private int shu = 0;

    private int count = 0;

    public static void main(String[] args) {

//        for (int k = 0; k < 10; k++) {

        十个线程 十个线程 = new 十个线程();

        for (int i = 0; i < 10; i++) {
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    十个线程.print(十个线程.shu);
                }
            });
            t.start();
            try {
                t.join();//按顺序执行
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(十个线程.count);
//        }

    }

    public void print(int start) {
        for (int i = 1; i <= 10; i++) {
//            System.out.println(start + i);
            shu = start + i;
            count += shu;
        }

    }


}

后面两种几乎没有区别

第二种:executor框架

public class 十个线程基于executor {

    private volatile int shu=0;

    private int j;

    public static void main(String[] args) {

        十个线程基于executor 十个线程 = new 十个线程基于executor();

        ExecutorService executorService = Executors.newFixedThreadPool(10);

        for (int i = 0; i < 10; i++) {
            FutureTask<Integer> future = new FutureTask<Integer>(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    return 十个线程.print(十个线程.j);
                }
            });
            executorService.submit(future);
            try {
                System.out.println(future.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }

        }
        executorService.shutdown();
        System.out.println(executorService.isShutdown());

    }

    public Integer print(int start){
        for (int i = 1; i <= 10; i++) {
            System.out.println(start+i);
            j=start+i;
            shu += start+i;
        }
        System.out.println(Thread.currentThread().getName());
        return shu;
    }


}

第三种:faturetask

public class 十个线程基于fature {

    private int shu = 0;

    private int j;

    public static void main(String[] args) {

//        for (int k = 0; k < 10; k++) {

        十个线程基于fature 十个线程 = new 十个线程基于fature();

        for (int i = 0; i < 10; i++) {
            FutureTask<Integer> integerFutureTask = new FutureTask<Integer>(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    return 十个线程.print(十个线程.j);
                }
            });
            Thread thread = new Thread(integerFutureTask);
            thread.start();
            try {
                //                thread.join(); 无需join
                System.out.println(integerFutureTask.get() + "=======");//已经帮忙计算好了所有的和
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
//        }


    }

    public Integer print(int start) {
        for (int i = 1; i <= 10; i++) {
            System.out.println(start + i);
            j = start + i;
            shu += start + i;
        }
        return shu;
    }


}

本文转自 jimolvxing.blog.csdn.net/article/det…,如有侵权,请联系删除。