生活随笔
收集整理的這篇文章主要介紹了
Java通过Executors提供四种线程池
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://cuisuqiang.iteye.com/blog/2019372
Java通過Executors提供四種線程池,分別為:
newCachedThreadPool創建一個可緩存線程池,如果線程池長度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程。
newFixedThreadPool 創建一個定長線程池,可控制線程最大并發數,超出的線程會在隊列中等待。
newScheduledThreadPool 創建一個定長線程池,支持定時及周期性任務執行。
newSingleThreadExecutor 創建一個單線程化的線程池,它只會用唯一的工作線程來執行任務,保證所有任務按照指定順序(FIFO, LIFO, 優先級)執行。
?
(1) newCachedThreadPool
創建一個可緩存線程池,如果線程池長度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程。示例代碼如下:
Java代碼 ?
package?test;??import?java.util.concurrent.ExecutorService;??import?java.util.concurrent.Executors;??public?class?ThreadPoolExecutorTest?{???public?static?void?main(String[]?args)?{????ExecutorService?cachedThreadPool?=?Executors.newCachedThreadPool();????for?(int?i?=?0;?i?<?10;?i++)?{?????final?int?index?=?i;?????try?{??????Thread.sleep(index?*?1000);?????}?catch?(InterruptedException?e)?{??????e.printStackTrace();?????}?????cachedThreadPool.execute(new?Runnable()?{??????public?void?run()?{???????System.out.println(index);??????}?????});????}???}??}?? ?
線程池為無限大,當執行第二個任務時第一個任務已經完成,會復用執行第一個任務的線程,而不用每次新建線程。
?
(2) newFixedThreadPool
創建一個定長線程池,可控制線程最大并發數,超出的線程會在隊列中等待。示例代碼如下:
Java代碼 ?
package?test;??import?java.util.concurrent.ExecutorService;??import?java.util.concurrent.Executors;??public?class?ThreadPoolExecutorTest?{???public?static?void?main(String[]?args)?{????ExecutorService?fixedThreadPool?=?Executors.newFixedThreadPool(3);????for?(int?i?=?0;?i?<?10;?i++)?{?????final?int?index?=?i;?????fixedThreadPool.execute(new?Runnable()?{??????public?void?run()?{???????try?{????????System.out.println(index);????????Thread.sleep(2000);???????}?catch?(InterruptedException?e)?{????????e.printStackTrace();???????}??????}?????});????}???}??}?? ?
因為線程池大小為3,每個任務輸出index后sleep 2秒,所以每兩秒打印3個數字。
定長線程池的大小最好根據系統資源進行設置。如Runtime.getRuntime().availableProcessors()
?
(3)? newScheduledThreadPool
創建一個定長線程池,支持定時及周期性任務執行。延遲執行示例代碼如下:
Java代碼 ?
package?test;??import?java.util.concurrent.Executors;??import?java.util.concurrent.ScheduledExecutorService;??import?java.util.concurrent.TimeUnit;??public?class?ThreadPoolExecutorTest?{???public?static?void?main(String[]?args)?{????ScheduledExecutorService?scheduledThreadPool?=?Executors.newScheduledThreadPool(5);????scheduledThreadPool.schedule(new?Runnable()?{?????public?void?run()?{??????System.out.println("delay?3?seconds");?????}????},?3,?TimeUnit.SECONDS);???}??}?? ?
表示延遲3秒執行。
定期執行示例代碼如下:
Java代碼 ?
package?test;??import?java.util.concurrent.Executors;??import?java.util.concurrent.ScheduledExecutorService;??import?java.util.concurrent.TimeUnit;??public?class?ThreadPoolExecutorTest?{???public?static?void?main(String[]?args)?{????ScheduledExecutorService?scheduledThreadPool?=?Executors.newScheduledThreadPool(5);????scheduledThreadPool.scheduleAtFixedRate(new?Runnable()?{?????public?void?run()?{??????System.out.println("delay?1?seconds,?and?excute?every?3?seconds");?????}????},?1,?3,?TimeUnit.SECONDS);???}??}?? ?
表示延遲1秒后每3秒執行一次。
?
(4) newSingleThreadExecutor
創建一個單線程化的線程池,它只會用唯一的工作線程來執行任務,保證所有任務按照指定順序(FIFO, LIFO, 優先級)執行。示例代碼如下:
Java代碼 ?
package?test;??import?java.util.concurrent.ExecutorService;??import?java.util.concurrent.Executors;??public?class?ThreadPoolExecutorTest?{???public?static?void?main(String[]?args)?{????ExecutorService?singleThreadExecutor?=?Executors.newSingleThreadExecutor();????for?(int?i?=?0;?i?<?10;?i++)?{?????final?int?index?=?i;?????singleThreadExecutor.execute(new?Runnable()?{??????public?void?run()?{???????try?{????????System.out.println(index);????????Thread.sleep(2000);???????}?catch?(InterruptedException?e)?{????????e.printStackTrace();???????}??????}?????});????}???}??}?? ?
結果依次輸出,相當于順序執行各個任務。
你可以使用JDK自帶的監控工具來監控我們創建的線程數量,運行一個不終止的線程,創建指定量的線程,來觀察:
工具目錄:C:\Program Files\Java\jdk1.6.0_06\bin\jconsole.exe
運行程序做稍微修改:
Java代碼 ?
package?test;??import?java.util.concurrent.ExecutorService;??import?java.util.concurrent.Executors;??public?class?ThreadPoolExecutorTest?{???public?static?void?main(String[]?args)?{????ExecutorService?singleThreadExecutor?=?Executors.newCachedThreadPool();????for?(int?i?=?0;?i?<?100;?i++)?{?????final?int?index?=?i;?????singleThreadExecutor.execute(new?Runnable()?{??????public?void?run()?{???????try?{????????while(true)?{?????????System.out.println(index);?????????Thread.sleep(10?*?1000);????????}???????}?catch?(InterruptedException?e)?{????????e.printStackTrace();???????}??????}?????});?????try?{??????Thread.sleep(500);?????}?catch?(InterruptedException?e)?{??????e.printStackTrace();?????}????}???}??}?? ?
效果如下:
?
選擇我們運行的程序:
監控運行狀態
轉載于:https://www.cnblogs.com/bill89/p/10483022.html
總結
以上是生活随笔為你收集整理的Java通过Executors提供四种线程池的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。