新的起點 新的開始

          快樂生活 !

          學習實踐 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 閱讀(728) 評論(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站點

          優秀個人博客鏈接

          官網學習站點

          生活工作站點

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 无极县| 宁晋县| 武平县| 桃江县| 紫阳县| 双峰县| 炎陵县| 黄梅县| 孝感市| 青岛市| 开化县| 斗六市| 札达县| 仙桃市| 金寨县| 石家庄市| 齐齐哈尔市| 潢川县| 沧州市| 贡嘎县| 张家口市| 大渡口区| 二连浩特市| 金秀| 上虞市| 普宁市| 奇台县| 房山区| 通州市| 高安市| 汝城县| 镇康县| 彰化市| 本溪| 黄骅市| 泽普县| 绥中县| 乌兰浩特市| 屏南县| 黔西| 九龙坡区|