新的起點 新的開始

          快樂生活 !

          學習實踐 JDK5 concurrent 并行包之executor(二)

          這是介紹使用JDK 并行包之executor 第二部分,這部分介紹是如何創建一個可以調度的線程池。具體參考代碼中的注釋。
          /*
                   * delay 4 seconds and run thread every 2 seconds.
                   
          */
                  
          //ses.scheduleAtFixedRate(GetRunnable(), 4, 2, TimeUnit.SECONDS);
                  
                  
          /*
                   * delay 4 seconds and run thread delay 1 second.
                   
          */
                  ses.scheduleWithFixedDelay(GetRunnable(), 
          21, TimeUnit.SECONDS);
                  
                  
          /*
          上述代碼是關鍵。




          package net.vincent.study.executor;

          import java.util.Date;
          import java.util.concurrent.Executors;
          import java.util.concurrent.ScheduledExecutorService;
          import java.util.concurrent.TimeUnit;

              
          /**
               * This is example of using ScheduleExecutorService.
               * 
          @author wma
               
          */

          public class Part2 {

              
          /**
               * Create scheduleThreadPool
               * 
          @param numberOfTHread in threadPool.
               * 
          @return ScheduleThreadPool
               
          */
              
          public static ScheduledExecutorService getScheduleThreadPool(int numberOfThread){
                  
          if(numberOfThread <=0)
                      
          return null;
                  ScheduledExecutorService  scheduleThreadPool 
          = Executors.newScheduledThreadPool(numberOfThread) ;
                  
          return scheduleThreadPool;
              }
              
          /**
               * 
               * Create Runnable 
               
          */
              
          public static Runnable GetRunnable(){
                  
          final Runnable runnable = new Runnable() {
                        
          int count = 0;
                        
          public void run() {
                          System.out.println(
          new Date() + " run " + (++count));
                        }
                      };
                  
          return runnable;
                  
              }
              
              
              
          public static void main(String[] args) {
                  ScheduledExecutorService ses 
          = getScheduleThreadPool(4);
                  System.out.println(
          new Date());
                  
          /*
                   * delay 4 seconds and run thread every 2 seconds.
                   
          */
                  
          //ses.scheduleAtFixedRate(GetRunnable(), 4, 2, TimeUnit.SECONDS);
                  
                  
          /*
                   * delay 4 seconds and run thread delay 1 second.
                   
          */
                  ses.scheduleWithFixedDelay(GetRunnable(), 
          21, TimeUnit.SECONDS);
                  
                  
          /*
                   * ses should be shutdown.
                   
          */
                  
          //ses.shutdown();

              }

          }

          posted on 2007-09-26 17:16 advincenting 閱讀(729) 評論(1)  編輯  收藏

          評論

          # re: 學習實踐 JDK5 concurrent 并行包之executor(二) 2007-09-27 19:19 千里冰封

          不會吧,就這樣帖點代碼,也解釋一下吧  回復  更多評論   


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          公告

          Locations of visitors to this pageBlogJava
        1. 首頁
        2. 新隨筆
        3. 聯系
        4. 聚合
        5. 管理
        6. <2007年9月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          統計

          常用鏈接

          留言簿(13)

          隨筆分類(71)

          隨筆檔案(179)

          文章檔案(13)

          新聞分類

          IT人的英語學習網站

          JAVA站點

          優秀個人博客鏈接

          官網學習站點

          生活工作站點

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 义马市| 海安县| 黄骅市| 舟曲县| 洛扎县| 日喀则市| 定西市| 漳浦县| 水富县| 山西省| 阿拉善盟| 阿尔山市| 兴安盟| 澜沧| 贵南县| 英山县| 枣阳市| 滁州市| 蛟河市| 乐业县| 佳木斯市| 伊金霍洛旗| 高碑店市| 普定县| 宽甸| 鄂州市| 呼图壁县| 天门市| 易门县| 深水埗区| 惠安县| 织金县| 宕昌县| 河北区| 延边| 维西| 甘德县| 广南县| 平昌县| 唐河县| 扬中市|