<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Java線程(七):Callable和Future 接著上一篇繼續并發包的學習,本篇說明的是Callable和Future,它倆很有意思的,一個產生結果,一個拿到結果。 Callable接口類似于Runnable,從名字就可以看出來了,但是Runnable不會返回結果,并且無法拋出返回結果的異常,而Callable功能更強大一些,被線程執行后,可以返回值,這個返回值可以被Future拿到,也就是說,Future可以拿到異步執行任務的返回值,下面來看一個簡單的例子: ~~~ public?class?CallableAndFuture?{?? ????public?static?void?main(String[]?args)?{?? ????????Callable?callable?=?new?Callable()?{?? ????????????public?Integer?call()?throws?Exception?{?? ????????????????return?new?Random().nextInt(100);?? ????????????}?? ????????};?? ????????FutureTask?future?=?new?FutureTask(callable);?? ????????new?Thread(future).start();?? ????????try?{?? ????????????Thread.sleep(5000);//?可能做一些事情?? ????????????System.out.println(future.get());?? ????????}?catch?(InterruptedException?e)?{?? ????????????e.printStackTrace();?? ????????}?catch?(ExecutionException?e)?{?? ????????????e.printStackTrace();?? ????????}?? ????}?? }?? ~~~ FutureTask實現了兩個接口,Runnable和Future,所以它既可以作為Runnable被線程執行,又可以作為Future得到Callable的返回值,那么這個組合的使用有什么好處呢?假設有一個很耗時的返回值需要計算,并且這個返回值不是立刻需要的話,那么就可以使用這個組合,用另一個線程去計算返回值,而當前線程在使用這個返回值之前可以做其它的操作,等到需要這個返回值時,再通過Future得到,豈不美哉!這里有一個Future模式的介紹:[](http://caterpillar.onlyfun.net/Gossip/DesignPattern/FuturePattern.htm)[http://openhome.cc/Gossip/DesignPattern/FuturePattern.htm](http://openhome.cc/Gossip/DesignPattern/FuturePattern.htm)。 下面來看另一種方式使用Callable和Future,通過ExecutorService的submit方法執行Callable,并返回Future,代碼如下: ~~~ public?class?CallableAndFuture?{?? ????public?static?void?main(String[]?args)?{?? ????????ExecutorService?threadPool?=?Executors.newSingleThreadExecutor();?? ????????Future?future?=?threadPool.submit(new?Callable()?{?? ????????????public?Integer?call()?throws?Exception?{?? ????????????????return?new?Random().nextInt(100);?? ????????????}?? ????????});?? ????????try?{?? ????????????Thread.sleep(5000);//?可能做一些事情?? ????????????System.out.println(future.get());?? ????????}?catch?(InterruptedException?e)?{?? ????????????e.printStackTrace();?? ????????}?catch?(ExecutionException?e)?{?? ????????????e.printStackTrace();?? ????????}?? ????}?? }?? ~~~ 代碼是不是簡化了很多,ExecutorService繼承自Executor,它的目的是為我們管理Thread對象,從而簡化并發編程,Executor使我們無需顯示的去管理線程的生命周期,是JDK 5之后啟動任務的首選方式。 執行多個帶返回值的任務,并取得多個返回值,代碼如下: ~~~ public?class?CallableAndFuture?{?? ????public?static?void?main(String[]?args)?{?? ????????ExecutorService?threadPool?=?Executors.newCachedThreadPool();?? ????????CompletionService?cs?=?new?ExecutorCompletionService(threadPool);?? ????????for(int?i?=?1;?i?5;?i++)?{?? ????????????final?int?taskID?=?i;?? ????????????cs.submit(new?Callable()?{?? ????????????????public?Integer?call()?throws?Exception?{?? ????????????????????return?taskID;?? ????????????????}?? ????????????});?? ????????}?? ????????//?可能做一些事情?? ????????for(int?i?=?1;?i?5;?i++)?{?? ????????????try?{?? ????????????????System.out.println(cs.take().get());?? ????????????}?catch?(InterruptedException?e)?{?? ????????????????e.printStackTrace();?? ????????????}?catch?(ExecutionException?e)?{?? ????????????????e.printStackTrace();?? ????????????}?? ????????}?? ????}?? }???????? ~~~ 其實也可以不使用CompletionService,可以先創建一個裝Future類型的集合,用Executor提交的任務返回值添加到集合中,最后遍歷集合取出數據,代碼略。 ? ? ? ?
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看