<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    新的起點 新的開始

    快樂生活 !

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

    這是介紹使用JDK 并行包之executor 第一部分,Executors 注意是復數,是最主要的工廠,通過它可以創建許多有用的東東,這部分介紹是如何創建一個固定的線程池。具體參考代碼中的注釋。

    1
     package net.vincent.study.executor;
     2 import java.util.concurrent.ExecutorService;
     3 import java.util.concurrent.Executors;
     4 
     5 
     6 /** This is part1 of study executor package in concurrent on JDK 5.0
     7  * 
     8  * @author wma
     9  */
    10 public class Part1 {
    11 
    12      /**
    13      Create a fixed threadPool for test.
    14      * @param Number of thread of threadPool
    15      * @return Created threadPool
    16      * @throws null
    17      */
    18     public static ExecutorService getThreadPool(int numberOfThread){
    19         if(numberOfThread<=0){
    20             return null;
    21             }
    22         ExecutorService exec = Executors.newFixedThreadPool(numberOfThread);
    23         return exec;
    24     } 
    25     /**
    26      * Create ruunbale, this runnable will random sleep.
    27      * @return a Runnbale
    28      */
    29     public static Runnable getRunnable(){
    30         Runnable run = new Runnable() {
    31             public void run() {
    32               long time = (long) (Math.random() * 1000);
    33               System.out.println("Sleeping " + time + "ms");
    34                 try {
    35                   Thread.sleep(time);
    36                 } catch (InterruptedException e) {
    37                 }
    38             }
    39           };
    40           return run;
    41     }
    42     /**In main method, we create a threadPool(ThreadPoolExecutor) and capability is 4.
    43      * After we submit 100 thread to this threadPool(ThreadPoolExecutor) and run it.
    44      * Notice: If number of thread you submitted exceed capability, and threadPool(ThreadPoolExecutor) dont block Main method,
    45      * because threadPool add other thread to queue.see code of ThreadPoolExecutor
    46      * 
    47      *  private final BlockingQueue<Runnable> workQueue;
    48      *  private final HashSet<Worker> workers = new HashSet<Worker>();
    49      *  try {
    50             if (poolSize < corePoolSize && runState == RUNNING)
    51                 t = addThread(firstTask); // add other thread to queue.
    52         } finally {
    53             mainLock.unlock();
    54         }
    55         if (t == null)
    56             return false;
    57         t.start();
    58         return true;
    59         
    60          private Thread addThread(Runnable firstTask) {
    61         Worker w = new Worker(firstTask);
    62         Thread t = threadFactory.newThread(w);
    63         if (t != null) {
    64             w.thread = t;
    65             workers.add(w);
    66             int nt = ++poolSize;
    67             if (nt > largestPoolSize)
    68                 largestPoolSize = nt;
    69         }
    70         return t;
    71     }
    72      * 
    73      * @param args
    74      */
    75     public static void main(String[] args) {
    76         ExecutorService  exe = getThreadPool(4);
    77         for(int i =0 ; i<100; i++)
    78         exe.execute(getRunnable());
    79         exe.shutdown();
    80         
    81         
    82     }
    83 
    84 }
    85 

    posted on 2007-09-26 14:32 advincenting 閱讀(648) 評論(1)  編輯  收藏

    評論

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

    建議樓主加點注釋  回復  更多評論   


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


    網站導航:
     

    公告

    Locations of visitors to this pageBlogJava
  • 首頁
  • 新隨筆
  • 聯系
  • 聚合
  • 管理
  • <2007年9月>
    2627282930311
    2345678
    9101112131415
    16171819202122
    23242526272829
    30123456

    統計

    常用鏈接

    留言簿(13)

    隨筆分類(71)

    隨筆檔案(179)

    文章檔案(13)

    新聞分類

    IT人的英語學習網站

    JAVA站點

    優秀個人博客鏈接

    官網學習站點

    生活工作站點

    最新隨筆

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 无码精品一区二区三区免费视频 | 亚洲无砖砖区免费| 亚洲性猛交XXXX| 成人一区二区免费视频| 久久久青草青青国产亚洲免观| 国产三级在线免费观看| 亚洲AV成人无码网站| 一二三四免费观看在线视频中文版 | 久久精品亚洲综合一品| 亚洲人成毛片线播放| 亚洲视频免费在线播放| 亚洲精品国产第1页| 国产精品色拉拉免费看| 亚洲综合精品伊人久久| 国产一区二区三区免费看| 九九久久国产精品免费热6| 亚洲精品无码av人在线观看| 中文字幕视频在线免费观看| 亚洲电影一区二区三区| 91精品免费在线观看| 亚洲精品无码aⅴ中文字幕蜜桃| 免费无码一区二区三区| 亚洲人成综合在线播放| 国产小视频免费观看| 国产精品视频全国免费观看 | 成人免费无码大片a毛片软件| 无人视频在线观看免费播放影院 | 亚洲黄色免费网站| a级毛片免费全部播放无码| 无码欧精品亚洲日韩一区| 91精品国产免费入口| 亚洲成a人片在线观看无码专区| 99热在线精品免费播放6| 亚洲欧美国产国产综合一区| 亚洲精品第一国产综合精品99| 国产99视频精品免费专区| 亚洲精品无码av中文字幕| 亚洲精品成人片在线播放| 成人免费网站在线观看| 皇色在线免费视频| 亚洲人成网站看在线播放|