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

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

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

    新的起點(diǎn) 新的開始

    快樂生活 !

    學(xué)習(xí)實(shí)踐 JDK5 concurrent 并行包之executor(一)

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

    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) 評(píng)論(1)  編輯  收藏

    評(píng)論

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

    建議樓主加點(diǎn)注釋  回復(fù)  更多評(píng)論   


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     

    公告

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

    統(tǒng)計(jì)

    常用鏈接

    留言簿(13)

    隨筆分類(71)

    隨筆檔案(179)

    文章檔案(13)

    新聞分類

    IT人的英語學(xué)習(xí)網(wǎng)站

    JAVA站點(diǎn)

    優(yōu)秀個(gè)人博客鏈接

    官網(wǎng)學(xué)習(xí)站點(diǎn)

    生活工作站點(diǎn)

    最新隨筆

    搜索

    積分與排名

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 国产亚洲精品国产福利在线观看| 亚洲av色福利天堂| 亚洲丰满熟女一区二区哦| 麻花传媒剧在线mv免费观看| 亚洲毛片αv无线播放一区| 成人免费乱码大片A毛片| 亚洲中文字幕丝袜制服一区| 香港经典a毛片免费观看看| 免费无遮挡无码视频网站| 日韩国产欧美亚洲v片| 免费一级特黄特色大片在线观看 | 亚洲综合图片小说区热久久| 日韩免费无码一区二区三区 | 东北美女野外bbwbbw免费| 亚洲国产精品成人久久| 国产一精品一AV一免费| 久久久亚洲裙底偷窥综合| 国产大片免费网站不卡美女| 久久夜色精品国产噜噜亚洲a| 日韩精品免费一区二区三区| 99亚洲男女激情在线观看| 亚洲毛片不卡av在线播放一区| h视频在线观看免费| 亚洲Av永久无码精品三区在线 | 亚洲精品无码午夜福利中文字幕| 久久精品乱子伦免费| 亚洲av专区无码观看精品天堂| 香蕉高清免费永久在线视频 | 四虎影视成人永久免费观看视频 | 国产三级免费电影| 成年女人A毛片免费视频| 91亚洲国产成人久久精品网站| 成年女人看片免费视频播放器| 人成午夜免费大片在线观看| 亚洲成AV人片在线观看| 免费大片黄在线观看yw| 一个人看的www免费高清| 在线电影你懂的亚洲| 成人亚洲网站www在线观看| 好久久免费视频高清| 亚洲天然素人无码专区|