zoukankan      html  css  js  c++  java
  • ExecutorService.submit(Callable).get()不并发执行

    今天在学习Callable的用法,

    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
    + "毫秒");
    }

    }

    打印结果为:

    0任务开始执行, time[860000000]...
    0任务执行结束.
    0任务耗时 :4750
    next task...
    1任务开始执行, time[430000000]...
    1任务执行结束.
    1任务耗时 :2343
    next task...
    2任务开始执行, time[500000000]...
    2任务执行结束.
    2任务耗时 :1703
    next task...
    3任务开始执行, time[990000000]...
    3任务执行结束.
    3任务耗时 :3344
    next task...
    4任务开始执行, time[340000000]...
    4任务执行结束.
    4任务耗时 :1156
    next task...
    5任务开始执行, time[640000000]...
    5任务执行结束.
    5任务耗时 :2250
    next task...
    6任务开始执行, time[880000000]...
    6任务执行结束.
    6任务耗时 :3032
    next task...
    7任务开始执行, time[800000000]...
    7任务执行结束.
    7任务耗时 :2781
    next task...
    8任务开始执行, time[450000000]...
    8任务执行结束.
    8任务耗时 :1547
    next task...
    9任务开始执行, time[980000000]...
    9任务执行结束.
    9任务耗时 :3375
    next task...
    耗时:26281毫秒, 平均用时:2628.1毫秒

    运行了很多次,时间上有差异,但是执行顺序却一直是这个顺序...

    而把

     total += pool.submit(
    tr.
    new Task(10000000 * rand.nextInt(100), i + "任务")).get();的.get()去掉后
    结果是:
    next task...
    next task...
    0任务开始执行, time[880000000]...
    next task...
    next task...
    next task...
    next task...
    next task...
    next task...
    next task...
    next task...
    2任务开始执行, time[790000000]...
    1任务开始执行, time[230000000]...
    3任务开始执行, time[270000000]...
    4任务开始执行, time[490000000]...
    5任务开始执行, time[950000000]...
    6任务开始执行, time[950000000]...
    7任务开始执行, time[630000000]...
    8任务开始执行, time[510000000]...
    9任务开始执行, time[740000000]...
    1任务执行结束.
    1任务耗时 :4484
    3任务执行结束.
    3任务耗时 :5094
    7任务执行结束.
    7任务耗时 :8281
    4任务执行结束.
    4任务耗时 :9079
    9任务执行结束.
    9任务耗时 :9000
    8任务执行结束.
    8任务耗时 :9860
    5任务执行结束.
    5任务耗时 :14843
    6任务执行结束.
    6任务耗时 :15641
    2任务执行结束.
    2任务耗时 :18297
    0任务执行结束.
    0任务耗时 :19000


    是不是.get()之后就不并发执行了呢?那它还是concurrency吗?
  • 相关阅读:
    【CF875E】Delivery Club 二分+线段树
    【CF316G3】Good Substrings 后缀自动机
    【BZOJ3413】匹配 离线+后缀树+树状数组
    【BZOJ2658】[Zjoi2012]小蓝的好友(mrx) 平衡树维护笛卡尔树+扫描线
    【BZOJ5133】[CodePlus2017年12月]白金元首与独舞 矩阵树定理
    【LOJ6254】最优卡组 堆(模拟搜索)
    面试问题总结
    Nginx基本配置
    Visual Studio Enterprise 2015下载 Update3
    .net 中生成二维码的组件
  • 原文地址:https://www.cnblogs.com/adaikiss/p/1915735.html
Copyright © 2011-2022 走看看