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

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

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

    hello world

    隨筆 - 2, 文章 - 63, 評論 - 0, 引用 - 0
    數據加載中……

    java線程池的種類和應用

    轉發自 http://blog.csdn.net/paul342/article/details/52442932


    Java通過Executors提供四種線程池,分別為:
    newCachedThreadPool創建一個可緩存線程池,如果線程池長度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程。
    newFixedThreadPool 創建一個定長線程池,可控制線程最大并發數,超出的線程會在隊列中等待。
    newScheduledThreadPool 創建一個定長線程池,支持定時及周期性任務執行。
    newSingleThreadExecutor 創建一個單線程化的線程池,它只會用唯一的工作線程來執行任務,保證所有任務按照指定順序(FIFO, LIFO, 優先級)執行。

    ?

    (1) newCachedThreadPool
    創建一個可緩存線程池,如果線程池長度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程。示例代碼如下:

    ?1?package?test;??
    ?2?import?java.util.concurrent.ExecutorService;??
    ?3?import?java.util.concurrent.Executors;??
    ?4?public?class?ThreadPoolExecutorTest?{??
    ?5??public?static?void?main(String[]?args)?{??
    ?6???ExecutorService?cachedThreadPool?=?Executors.newCachedThreadPool();??
    ?7???for?(int?i?=?0;?i?<?10;?i++)?{??
    ?8????final?int?index?=?i;??
    ?9????try?{??
    10?????Thread.sleep(index?*?1000);??
    11????}?catch?(InterruptedException?e)?{??
    12?????e.printStackTrace();??
    13????}??
    14????cachedThreadPool.execute(new?Runnable()?{??
    15?????public?void?run()?{??
    16??????System.out.println(index);??
    17?????}??
    18????});??
    19???}??
    20??}??
    21?}?


    線程池為無限大,當執行第二個任務時第一個任務已經完成,會復用執行第一個任務的線程,而不用每次新建線程。
    ?
    (2) newFixedThreadPool
    創建一個定長線程池,可控制線程最大并發數,超出的線程會在隊列中等待。示例代碼如下:
    ?1?????package?test;??
    ?2?????import?java.util.concurrent.ExecutorService;??
    ?3?????import?java.util.concurrent.Executors;??
    ?4?????public?class?ThreadPoolExecutorTest?{??
    ?5??????public?static?void?main(String[]?args)?{??
    ?6???????ExecutorService?fixedThreadPool?=?Executors.newFixedThreadPool(3);??
    ?7???????for?(int?i?=?0;?i?<?10;?i++)?{??
    ?8????????final?int?index?=?i;??
    ?9????????fixedThreadPool.execute(new?Runnable()?{??
    10?????????public?void?run()?{??
    11??????????try?{??
    12???????????System.out.println(index);??
    13???????????Thread.sleep(2000);??
    14??????????}?catch?(InterruptedException?e)?{??
    15???????????e.printStackTrace();??
    16??????????}??
    17?????????}??
    18????????});??
    19???????}??
    20??????}??
    21?????}??

    因為線程池大小為3,每個任務輸出index后sleep 2秒,所以每兩秒打印3個數字。
    定長線程池的大小最好根據系統資源進行設置。如Runtime.getRuntime().availableProcessors()

    ?

    (3)? newScheduledThreadPool
    創建一個定長線程池,支持定時及周期性任務執行。延遲執行示例代碼如下:

    ?1?????package?test;??
    ?2?????import?java.util.concurrent.Executors;??
    ?3?????import?java.util.concurrent.ScheduledExecutorService;??
    ?4?????import?java.util.concurrent.TimeUnit;??
    ?5?????public?class?ThreadPoolExecutorTest?{??
    ?6??????public?static?void?main(String[]?args)?{??
    ?7???????ScheduledExecutorService?scheduledThreadPool?=?Executors.newScheduledThreadPool(5);??
    ?8???????scheduledThreadPool.schedule(new?Runnable()?{??
    ?9????????public?void?run()?{??
    10?????????System.out.println("delay?3?seconds");??
    11????????}??
    12???????},?3,?TimeUnit.SECONDS);??
    13??????}??
    14?????}??

    表示延遲3秒執行。

    定期執行示例代碼如下:

    ?1?????package?test;??
    ?2?????import?java.util.concurrent.Executors;??
    ?3?????import?java.util.concurrent.ScheduledExecutorService;??
    ?4?????import?java.util.concurrent.TimeUnit;??
    ?5?????public?class?ThreadPoolExecutorTest?{??
    ?6??????public?static?void?main(String[]?args)?{??
    ?7???????ScheduledExecutorService?scheduledThreadPool?=?Executors.newScheduledThreadPool(5);??
    ?8???????scheduledThreadPool.scheduleAtFixedRate(new?Runnable()?{??
    ?9????????public?void?run()?{??
    10?????????System.out.println("delay?1?seconds,?and?excute?every?3?seconds");??
    11????????}??
    12???????},?1,?3,?TimeUnit.SECONDS);??
    13??????}??
    14?????}??

    表示延遲1秒后每3秒執行一次。

    ?

    (4) newSingleThreadExecutor
    創建一個單線程化的線程池,它只會用唯一的工作線程來執行任務,保證所有任務按照指定順序(FIFO, LIFO, 優先級)執行。示例代碼如下:

    ?1?package?test;??
    ?2?import?java.util.concurrent.ExecutorService;??
    ?3?import?java.util.concurrent.Executors;??
    ?4?public?class?ThreadPoolExecutorTest?{??
    ?5??public?static?void?main(String[]?args)?{??
    ?6???ExecutorService?singleThreadExecutor?=?Executors.newSingleThreadExecutor();??
    ?7???for?(int?i?=?0;?i?<?10;?i++)?{??
    ?8????final?int?index?=?i;??
    ?9????singleThreadExecutor.execute(new?Runnable()?{??
    10?????public?void?run()?{??
    11??????try?{??
    12???????System.out.println(index);??
    13???????Thread.sleep(2000);??
    14??????}?catch?(InterruptedException?e)?{??
    15???????e.printStackTrace();??
    16??????}??
    17?????}??
    18????});??
    19???}??
    20??}??
    21?}?

    結果依次輸出,相當于順序執行各個任務。

    你可以使用JDK自帶的監控工具來監控我們創建的線程數量,運行一個不終止的線程,創建指定量的線程,來觀察:
    工具目錄:C:\Program Files\Java\jdk1.6.0_06\bin\jconsole.exe
    運行程序做稍微修改:

    ?1?????package?test;??
    ?2?????import?java.util.concurrent.ExecutorService;??
    ?3?????import?java.util.concurrent.Executors;??
    ?4?????public?class?ThreadPoolExecutorTest?{??
    ?5??????public?static?void?main(String[]?args)?{??
    ?6???????ExecutorService?singleThreadExecutor?=?Executors.newCachedThreadPool();??
    ?7???????for?(int?i?=?0;?i?<?100;?i++)?{??
    ?8????????final?int?index?=?i;??
    ?9????????singleThreadExecutor.execute(new?Runnable()?{??
    10?????????public?void?run()?{??
    11??????????try?{??
    12???????????while(true)?{??
    13????????????System.out.println(index);??
    14????????????Thread.sleep(10?*?1000);??
    15???????????}??
    16??????????}?catch?(InterruptedException?e)?{??
    17???????????e.printStackTrace();??
    18??????????}??
    19?????????}??
    20????????});??
    21????????try?{??
    22?????????Thread.sleep(500);??
    23????????}?catch?(InterruptedException?e)?{??
    24?????????e.printStackTrace();??
    25????????}??
    26???????}??
    27??????}??
    28?????}??

    效果如下:

    ?

    選擇我們運行的程序:

    監控運行狀態

    posted on 2017-07-25 10:35 聽風 閱讀(205) 評論(0)  編輯  收藏 所屬分類: JAVA

    主站蜘蛛池模板: 免费无码黄十八禁网站在线观看| 三年片在线观看免费| 免费a级毛片高清视频不卡| 亚洲免费观看网站| 最近2022中文字幕免费视频| 亚洲国产精品久久久久婷婷老年| 最近免费字幕中文大全| 国产精品亚洲成在人线| 99免费观看视频| 亚洲成人午夜电影| 成人特黄a级毛片免费视频| 久久久久亚洲国产| 国产人妖ts在线观看免费视频| 亚洲av午夜电影在线观看| 免费看国产一级特黄aa大片| 草久免费在线观看网站| 亚洲真人日本在线| 久章草在线精品视频免费观看| 久久亚洲精品国产精品| 最近免费中文字幕视频高清在线看 | 啊灬啊灬别停啊灬用力啊免费看| 精品一区二区三区无码免费直播| 四虎永久在线精品视频免费观看| ww在线观视频免费观看w| 亚洲va国产va天堂va久久| **俄罗斯毛片免费| 亚洲日韩一区二区三区| 久久亚洲AV永久无码精品| 无码精品国产一区二区三区免费 | 亚洲一区AV无码少妇电影| 免费一级毛片正在播放| 东方aⅴ免费观看久久av| 91亚洲自偷在线观看国产馆| 四虎在线播放免费永久视频| 水蜜桃视频在线观看免费播放高清 | 亚洲精品国产首次亮相| 亚洲色欲一区二区三区在线观看| 亚洲免费网站在线观看| 美女扒开尿口给男人爽免费视频| 亚洲人成77777在线播放网站| 国产又黄又爽又猛免费app|