ExecutorService.submit(Callable).get()不并发执行原因

原文地址:https://www.cnblogs.com/adaikiss/archive/2010/12/24/1915735.html

原文地址:https://blog.csdn.net/weixin_34406796/article/details/90817124

原文地址:https://www.jianshu.com/p/5f06d3fe3968

!!!!!不并发执行的原因

 

上代码

1.不并发执行的代码

public class CallableTry {
 
    class Task implements Callable<Long> {
        private long times;
        private String name;
 
        public Task(long times, String name) {
            this.name = name;
            this.times = times;
        }
 
        @Override
        public Long call() {
            System.out.println(name + "开始执行, time[" + times + "]...");
            long before = System.currentTimeMillis();
            for (int i = 0; i < times; i++)
                ;
            long after = System.currentTimeMillis();
            System.out.println(name + "执行结束.");
            long cost = after - before;
            System.out.println(name + "耗时 :" + cost);
            return cost;
        }
    }
 
    /**
     * @param args
     */
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        long total = 0;
        CallableTry tr = new CallableTry();
        ExecutorService pool = Executors.newCachedThreadPool();
        Random rand = new Random();
        int count = 10;
        for (int i = 0; i < count; i++) {
            total += pool.submit(tr.new Task(10000000 * rand.nextInt(100), i + "任务")).get();
            System.out.println("next task...");
        }
        pool.shutdown();
        while (!pool.isTerminated())
            ;
        System.out.println("耗时:" + total + "毫秒, 平均用时:" + total * 1.0 / count + "毫秒");
    }
 
}

2.运行结果没有并发执行

0任务开始执行, time[320000000]...
0任务执行结束.
0任务耗时 :169
next task...
1任务开始执行, time[180000000]...
1任务执行结束.
1任务耗时 :96
next task...
2任务开始执行, time[950000000]...
2任务执行结束.
2任务耗时 :469
next task...
3任务开始执行, time[440000000]...
3任务执行结束.
3任务耗时 :224
next task...
4任务开始执行, time[500000000]...
4任务执行结束.
4任务耗时 :243
next task...
5任务开始执行, time[700000000]...
5任务执行结束.
5任务耗时 :334
next task...
6任务开始执行, time[930000000]...
6任务执行结束.
6任务耗时 :486
next task...
7任务开始执行, time[110000000]...
7任务执行结束.
7任务耗时 :56
next task...
8任务开始执行, time[820000000]...
8任务执行结束.
8任务耗时 :433
next task...
9任务开始执行, time[320000000]...
9任务执行结束.
9任务耗时 :158
next task...
耗时:2668毫秒, 平均用时:266.8毫秒

 3.修改代码

public class CallableTry02 {
 
    class Task implements Callable<Long> {
        private long times;
        private String name;
 
        public Task(long times, String name) {
            this.name = name;
            this.times = times;
        }
 
        @Override
        public Long call() {
            System.out.println(name + "开始执行, time[" + times + "]...");
            long before = System.currentTimeMillis();
            for (int i = 0; i < times; i++);
            long after = System.currentTimeMillis();
            System.out.println(name + "执行结束.");
            long cost = after - before;
            System.out.println(name + "耗时 :" + cost);
            return cost;
        }
    }
 
    /**
//   * @param args
     */
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        long total = 0;
        CallableTry02 tr = new CallableTry02();
        ExecutorService pool = Executors.newCachedThreadPool();
        Random rand = new Random();
        int count = 10;
        Future[] futures = new Future[count];
        for (int i = 0; i < count; i++) {
//          total += pool.submit(tr.new Task(10000000 * rand.nextInt(100), i + "任务")).get();
            futures[i]= pool.submit(tr.new Task(10000000 * rand.nextInt(100), i + "任务"));
            
            
            
            System.out.println("next task...");
        }
        pool.shutdown();
        while (!pool.isTerminated());
        System.out.println("耗时:" + total + "毫秒, 平均用时:" + total * 1.0 / count + "毫秒");
    }
 
}

4.改后执行结果 

next task...
0任务开始执行, time[460000000]...
next task...
1任务开始执行, time[380000000]...
next task...
next task...
2任务开始执行, time[30000000]...
next task...
next task...
next task...
next task...
6任务开始执行, time[510000000]...
next task...
next task...
3任务开始执行, time[650000000]...
2任务执行结束.
2任务耗时 :37
5任务开始执行, time[270000000]...
4任务开始执行, time[770000000]...
7任务开始执行, time[180000000]...
9任务开始执行, time[610000000]...
8任务开始执行, time[120000000]...
8任务执行结束.
8任务耗时 :267
7任务执行结束.
7任务耗时 :448
5任务执行结束.
5任务耗时 :504
1任务执行结束.
1任务耗时 :802
6任务执行结束.
6任务耗时 :889
0任务执行结束.
0任务耗时 :930
9任务执行结束.
9任务耗时 :851
3任务执行结束.
3任务耗时 :987
4任务执行结束.
4任务耗时 :1061

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值