ゞ沉默是金ゞ

          魚離不開水,但是沒有說不離開哪滴水.
          posts - 98,comments - 104,trackbacks - 0
          Future<T> submit(Callable<T> c)
          Today in next part of the series we will talk about submission of callable task via executor service. 

          As per JAVA version 6.0, ExecutorService Interface has following method -
          Future< T >  submit( callable c )
          1. Submits a Callable task for execution and returns a Future representing that task computation.
          2. Future< T > will return T on successful completion of the process, otherwise result will not be T.
          Lets start with Callable task - As we  know that Callable<T> interface has following method - public T call()
          So when we say - ExecutorService.submit(Callable Task) --> It starts a new stack starting with call method of callable task, as call method can return T so does future object. Due to this reason the signature of submit(Callable c) is Future< T >.
          T --> means anything that extends Object.
          package com.jovialjava.blog.threads;

          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 CallableExample {

              
          private static final ExecutorService executorPool=Executors.newFixedThreadPool(2);
              
              
          public static void main(String[] args) {
                  CallableTask_1 task_1 
          = new CallableTask_1();
                  CallableTask_2 task_2 
          = new CallableTask_2();
                  
                  
          /**
                   * Submit the first task
                   
          */
                  Future
          <Boolean> fut_1 = executorPool.submit(task_1);
                  Future
          <Boolean> fut_2 = executorPool.submit(task_2);
                  
                  
          try{
                      
          if(fut_1.get()){
                          System.out.println(
          "TASK 1 completed SUCCESSFULLY");
                      }
                      
          if(fut_2.get()){
                          System.out.println(
          "TASK 2 completed SUCCESSFULLY");
                      }
                  }
          catch(ExecutionException e){            
                      System.out.println(e.getMessage());
                  }
          catch(InterruptedException e){
                      System.out.println(e.getMessage());
                  }
          finally{
                      
          /**====VERY IMPORTANT===
                       * This is required to stop the executor pool to
                       * stop accepting new request.
                       
          */
                      executorPool.shutdown();
                  }

              }

              
              
          /**
               * This task will complete successfully
               
          */
              
          public static class CallableTask_1 implements Callable<Boolean>{
                  
          public Boolean call()throws NullPointerException{
                      System.out.println(
          "Hi, Inside Callable Task 1");
                      
          return true;
                  }        

              }
              
              
          /**
               * This task will result in error.
               
          */
              
          public static class CallableTask_2 implements Callable<Boolean>{
                  
          public Boolean call()throws NullPointerException{
                      System.out.println(
          "Hi, Inside Callable Task 2");
                      
          throw new IllegalStateException("Callable Task Exception");
                  }        

              }
          }
          posted on 2012-08-06 10:35 ゞ沉默是金ゞ 閱讀(798) 評(píng)論(0)  編輯  收藏 所屬分類: Java SE
          主站蜘蛛池模板: 资阳市| 隆回县| 玛多县| 田阳县| 广宗县| 水富县| 买车| 通海县| 久治县| 竹溪县| 康定县| 凤台县| 涡阳县| 松阳县| 贡山| 青龙| 合水县| 阳山县| 邵武市| 长白| 溧阳市| 沐川县| 广平县| 新密市| 咸阳市| 长白| 宁城县| 宜阳县| 罗江县| 清水县| 西城区| 明光市| 嘉定区| 商丘市| 扶沟县| 松溪县| 怀柔区| 宜兰市| 来宾市| 启东市| 马关县|