ゞ沉默是金ゞ

          魚離不開水,但是沒有說不離開哪滴水.
          posts - 98,comments - 104,trackbacks - 0
          Future<?> submit( Runnable r )
          Today in part 4 of the series we will talk about submission of runnable task via executor service. This method can be very beneficial if someone wants to upgrade legacy code from 1.4 to 1.5 onwards.
          As per JAVA version 6.0, ExecutorService Interface has following method -
          Future< ? >  submit( Runnable r ) 
            > Submits a Runnable task for execution and returns a Future representing that task.
            > This method will return NULL on successful completeion of the process, otherwise result will not be null.


          Lets start with Runnable task - As we  know that Runnable interface has following method - public void run()
          So when we say - ExecutorService.submit(Runnable Task) --> It starts a new stack starting with run method of runnable task, but as run method can not return any value so does future object. Due to this reason the signature of submit(Runnable r) is Future< ? >.
          ? --> means anything that extends Object, has no lower limit

          package com.jovialjava.blog.threads;

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

          public class RunnableExample {
              
              
          private static final ExecutorService executorPool=Executors.newFixedThreadPool(2);
              
              
          public static void main(String[] args) {
                  RunnableTask_1 task_1 
          = new RunnableTask_1();
                  RunnableTask_2 task_2 
          = new RunnableTask_2();
                  
                  
          /**
                   * Submit the first task
                   
          */
                  Future
          <?> fut_1 = executorPool.submit(task_1);
                  Future
          <?> fut_2 = executorPool.submit(task_2);
                  
                  
          try{
                      
          if(fut_1.get() == null){
                          System.out.println(
          "TASK 1 completed SUCCESSFULLY");
                      }
                      
          if(fut_2.get() == null){
                          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 RunnableTask_1 implements Runnable{
                  
          public void run()throws NullPointerException{
                      System.out.println(
          "Hi, Inside Runnable Task 1");            
                  }        

              }
              
              
          /**
               * This task will result in error.
               
          */
              
          public static class RunnableTask_2 implements Runnable{
                  
          public void run()throws NullPointerException{
                      System.out.println(
          "Hi, Inside Runnable Task 2");
                      
          throw new IllegalStateException("Runnable Task Exception");
                  }        

              }
          }
          posted on 2012-08-06 10:33 ゞ沉默是金ゞ 閱讀(774) 評論(0)  編輯  收藏 所屬分類: Java SE
          主站蜘蛛池模板: 淳安县| 永丰县| 泌阳县| 天长市| 滨州市| 合阳县| 安国市| 丰城市| 陆河县| 曲靖市| 萨迦县| 云安县| 县级市| 陇南市| 大关县| 崇仁县| 白城市| 马关县| 开原市| 乌什县| 渭南市| 静乐县| 名山县| 固安县| 福建省| 石阡县| 府谷县| 葫芦岛市| 定陶县| 修文县| 贞丰县| 河北区| 卢湾区| 兴山县| 邵东县| 苍溪县| 威信县| 邵武市| 松滋市| 中阳县| 黔东|