• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            逛奔的蝸牛

            我不聰明,但我會很努力

               ::  :: 新隨筆 ::  ::  :: 管理 ::

            import java.util.LinkedList;

            import java.util.List;

            import java.util.concurrent.Callable;

            import java.util.concurrent.ExecutionException;

            import java.util.concurrent.ExecutorService;

            import java.util.concurrent.Executors;

            import java.util.concurrent.Future;


            public class InvokeAllAndAnyTest {

                public static void main(String[] args) throws InterruptedException, ExecutionException {

                    ExecutorService pool = Executors.newCachedThreadPool();

                    List<Callable<Integer>> tasks = new LinkedList<Callable<Integer>>();

                    for (int i = 0; i < 8; ++i) {

                        tasks.add(new Printer(i));

                    }

                    List<Future<Integer>> futures = pool.invokeAll(tasks)// 阻塞直至所有線程計算結束.

                    

                    System.out.println("...............................................");

                    for (Future<Integer> future : futures) {

                        System.out.println(future.get());

                    }

                    

                    Integer result = pool.invokeAny(tasks)// 阻塞直至隨機一個線程計算結束.

                    System.out.println("...............................................");

                    System.out.println(result);

                    

                    pool.shutdown();

                }

            }


            class Printer implements Callable<Integer> {

                private int id;

                private static final int DELAY = 1000;


                public Printer(int id) {

                    this.id = id;

                }


                public Integer call() {

                    try {

                        int times = 10;

                        for (int i = 0; i < times; ++i) {

                            System.out.println("Id:" + id + " times:" + i);


                            Thread.sleep((int) (DELAY * Math.random()));

                        }

                    } catch (InterruptedException e) {

                    }


                    return id;

                }

            }


            posted on 2009-06-29 13:52 逛奔的蝸牛 閱讀(181) 評論(0)  編輯 收藏 引用 所屬分類: Java
            狠狠色丁香久久婷婷综合五月| 伊人久久大香线蕉av不变影院| 91性高湖久久久久| 国产精品99久久久久久www| 欧美日韩成人精品久久久免费看| 伊人色综合久久天天人手人婷| 久久久久亚洲AV无码网站| 成人精品一区二区久久| 久久人人爽人人爽人人片av麻烦 | 亚洲欧美国产日韩综合久久| 欧洲精品久久久av无码电影| 久久精品亚洲乱码伦伦中文| 蜜臀av性久久久久蜜臀aⅴ麻豆| 精品久久人人做人人爽综合| 久久人妻少妇嫩草AV无码专区| 久久影院午夜理论片无码| 久久99精品综合国产首页| 伊人久久大香线蕉综合Av| 欧美久久亚洲精品| 办公室久久精品| 91精品国产9l久久久久| 色偷偷久久一区二区三区| 欧美激情精品久久久久久久九九九| 国产精品久久久久国产A级| 久久精品国产男包| 亚洲国产精品无码久久青草 | 久久国产精品77777| 久久久久久久女国产乱让韩| 亚洲Av无码国产情品久久| 一级做a爰片久久毛片人呢| 狠色狠色狠狠色综合久久| 亚洲综合伊人久久大杳蕉| 色播久久人人爽人人爽人人片AV| 蜜桃麻豆www久久国产精品| 久久九九久精品国产| 国产叼嘿久久精品久久| 亚洲国产精品婷婷久久| 94久久国产乱子伦精品免费| 国产精品免费看久久久香蕉 | 久久综合亚洲鲁鲁五月天| 久久久久亚洲AV片无码下载蜜桃|