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 聽風 閱讀(198) 評論(0)  編輯  收藏 所屬分類: JAVA

          主站蜘蛛池模板: 新田县| 赤水市| 英吉沙县| 九江县| 江西省| 城固县| 肇州县| 郯城县| 潞西市| 西乡县| 甘南县| 万山特区| 日喀则市| 丹东市| 额尔古纳市| 孝昌县| 鹿邑县| 清流县| 肇州县| 龙岩市| 河间市| 富蕴县| 綦江县| 桃源县| 富锦市| 板桥市| 通山县| 闻喜县| 遂平县| 南涧| 民县| 镇远县| 仪征市| 广安市| 应用必备| 杭州市| 老河口市| 崇州市| 普定县| 赫章县| 三原县|