Chan Chen Coding...

          Runnable Callable Future and Executor in Java

          Refer to: http://blog.csdn.net/zhangzhaokun/article/details/6615454

          Executor就是Runnable和Callable的調度容器,Future就是對于具體的調度任務的執行結果進行查看,最為關鍵的是Future可以檢查對應的任務是否已經完成,也可以阻塞在get方法上一直等待任務返回結果。Runnable和Callable的差別就是Runnable是沒有結果可以返回的,就算是通過Future也看不到任務調度的結果的。 

          package com.future.demo;

          import java.util.concurrent.Callable;
          import java.util.concurrent.ExecutorService;
          import java.util.concurrent.Executors;
          import java.util.concurrent.Future;

          public class RunnableAndCallable2Future {
              public static void main(String[] args) {

                  // 創建一個執行任務的服務
                  ExecutorService executor = Executors.newFixedThreadPool(3);
                  try {
                      // 1.Runnable通過Future返回結果為空
                      
          // 創建一個Runnable,來調度,等待任務執行完畢,取得返回結果
                      Future<?> runnable1 = executor.submit(new Runnable() {
                          @Override
                          public void run() {
                              System.out.println("runnable1 running.");
                          }
                      });
                      System.out.println("Runnable1:" + runnable1.get());

                      // 2.Callable通過Future能返回結果
                      
          // 提交并執行任務,任務啟動時返回了一個 Future對象,
                      
          // 如果想得到任務執行的結果或者是異常可對這個Future對象進行操作
                      Future<String> future1 = executor.submit(new Callable<String>() {
                          @Override
                          public String call() throws Exception {
                              // TODO Auto-generated method stub
                              return "result=task1";
                          }
                      });
                      // 獲得任務的結果,如果調用get方法,當前線程會等待任務執行完畢后才往下執行
                      System.out.println("task1: " + future1.get());

                      // 3. 對Callable調用cancel可以對對該任務進行中斷
                      
          // 提交并執行任務,任務啟動時返回了一個 Future對象,
                      
          // 如果想得到任務執行的結果或者是異常可對這個Future對象進行操作
                      Future<String> future2 = executor.submit(new Callable<String>() {
                          @Override
                          public String call() throws Exception {
                              try {
                                  while (true) {
                                      System.out.println("task2 running.");
                                      Thread.sleep(50);
                                  }
                              } catch (InterruptedException e) {
                                  System.out.println("Interrupted task2.");
                              }
                              return "task2=false";
                          }
                      });

                      // 等待5秒后,再停止第二個任務。因為第二個任務進行的是無限循環
                      Thread.sleep(10);
                      System.out.println("task2 cancel: " + future2.cancel(true));

                      // 4.用Callable時拋出異常則Future什么也取不到了
                      
          // 獲取第三個任務的輸出,因為執行第三個任務會引起異常
                      
          // 所以下面的語句將引起異常的拋出
                      Future<String> future3 = executor.submit(new Callable<String>() {

                          @Override
                          public String call() throws Exception {
                              throw new Exception("task3 throw exception!");
                          }

                      });
                      System.out.println("task3: " + future3.get());
                  } catch (Exception e) {
                      System.out.println(e.toString());
                  }
                  // 停止任務執行服務
                  executor.shutdownNow();
              }

          }


          -----------------------------------------------------
          Silence, the way to avoid many problems;
          Smile, the way to solve many problems;

          posted on 2012-11-26 11:59 Chan Chen 閱讀(363) 評論(0)  編輯  收藏 所屬分類: Scala / Java

          主站蜘蛛池模板: 古浪县| 新沂市| 青海省| 德清县| 武宁县| 海晏县| 开原市| 隆昌县| 扎赉特旗| 塔城市| 巴彦县| 延津县| 潢川县| 南川市| 宜宾县| 永州市| 普格县| 桂平市| 遂平县| 怀宁县| 河曲县| 湾仔区| 从江县| 商城县| 晋中市| 彝良县| 商水县| 易门县| 永春县| 镇安县| 勃利县| 墨江| 濮阳县| 类乌齐县| 修武县| 宁津县| 满城县| 台前县| 偃师市| 博罗县| 陆川县|