找回密碼 或 安全提問
 註冊
|註冊|登錄

伊莉討論區

搜索
感激所有對伊莉作出奉獻的人你準備好成為出色的版主了嗎?安全提問(回答) 和 永久尊貴會員 事宜
ge催眠鬼父銀魂mggd國中
プロジェ神印王座immoral ssni 129超級直升princess福音戰士

休閒聊天興趣交流學術文化旅遊交流飲食交流家庭事務PC GAMETV GAME
熱門線上其他線上感情感性寵物交流家族門派動漫交流貼圖分享BL/GL
音樂世界影視娛樂女性頻道潮流資訊BT下載區GB下載區下載分享短片
電腦資訊數碼產品手機交流交易廣場網站事務長篇小說體育運動時事經濟
上班一族博彩娛樂

[繁]異修羅12- (第一

[繁]迷宮飯12-

[繁]不死不運21-

[繁]非自願的不死冒險

[繁/無修正]魔都精兵

[繁]迷宮飯11-
C & C++ 語言C# 語言Visual Basic 語言PHP 語言JAVA 語言
查看: 2682|回復: 1

[分享] 多線程中線程池[複製鏈接]

shutyou1 該用戶已被刪除
發表於 2014-6-18 05:01 PM|顯示全部樓層
成為伊莉的版主,你將獲得更高級和無限的權限。把你感興趣的版面一步步地發展和豐盛,那種滿足感等著你來嚐嚐喔。
本帖最後由 shutyou1 於 2014-6-18 05:02 PM 編輯

1. 概念

    線程池主要是通過Executors這個類來創建 返回的是ExecutorService對象

2.固定大小線程池

    例子:創建固定線程數目為3的線程池
  1. public class ExecutorThreadPool {
  2.     public static void main(String[] args) {
  3.         /**
  4.          * 創建固定大小的線程池  
  5.          */
  6.         ExecutorService threadPool = Executors.newFixedThreadPool(3) ;
  7.         for(int i=1 ;i<=10 ;i++){
  8.             final int taskid = i ;
  9.             threadPool.execute(new Runnable() {
  10.                 @Override
  11.                 public void run() {
  12.                     for(int j=1 ;j<=2 ;j++){
  13.                         try {
  14.                             Thread.sleep(20) ;
  15.                         } catch (InterruptedException e) {
  16.                             e.printStackTrace();
  17.                         }
  18.                         
  19.                         System.out.println("["+Thread.currentThread().getName()+" ]is loop of  "+j+" for task of "+taskid);
  20.                         
  21.                       }
  22.                 }   
  23.             }) ;
  24.         }
  25.     }
  26. }
複製代碼
運行結果如下:


固定的線程池數目為3,那麼始終只會有三個線程在運行,只有等到該三個線程的任務完成以后才會去執行其他的任務

3. 緩衝池


運行結果:



 緩衝池會按照jvm的調度來分配若干個線程來執行當前需要完成的任務 ,上面的例子有是個線程任務要執行,那麼自動分配了10個線程來完成

4.單一的線程池



運行結果:
  1. [pool-1-thread-1 ]is loop of  1 for task of 1
  2. [pool-1-thread-1 ]is loop of  2 for task of 1
  3. [pool-1-thread-1 ]is loop of  1 for task of 2
  4. [pool-1-thread-1 ]is loop of  2 for task of 2
  5. [pool-1-thread-1 ]is loop of  1 for task of 3
  6. [pool-1-thread-1 ]is loop of  2 for task of 3
  7. [pool-1-thread-1 ]is loop of  1 for task of 4
  8. [pool-1-thread-1 ]is loop of  2 for task of 4
  9. [pool-1-thread-1 ]is loop of  1 for task of 5
  10. [pool-1-thread-1 ]is loop of  2 for task of 5
  11. [pool-1-thread-1 ]is loop of  1 for task of 6
  12. [pool-1-thread-1 ]is loop of  2 for task of 6
  13. [pool-1-thread-1 ]is loop of  1 for task of 7
  14. [pool-1-thread-1 ]is loop of  2 for task of 7
  15. [pool-1-thread-1 ]is loop of  1 for task of 8
  16. [pool-1-thread-1 ]is loop of  2 for task of 8
  17. [pool-1-thread-1 ]is loop of  1 for task of 9
  18. [pool-1-thread-1 ]is loop of  2 for task of 9
  19. [pool-1-thread-1 ]is loop of  1 for task of 10
  20. [pool-1-thread-1 ]is loop of  2 for task of 10
複製代碼
...
瀏覽完整內容,請先 註冊登入會員
附件: 你需要登錄才可以下載或查看附件。沒有帳號?註冊
如果發覺自己無法使用一些功能或出現問題,請按重新整理一次,並待所有網頁內容完全載入後5秒才進行操作。

使用道具檢舉

jameschen7 該用戶已被刪除
發表於 2014-7-3 11:33 PM|顯示全部樓層
若新密碼無法使用,可能是數據未更新。請使用舊密碼看看。
感謝大大的無私分享,受益無窮
若有安裝色情守門員,可用無界、自由門等軟件瀏覽伊莉。或使用以下網址瀏覽伊莉: http://www.eyny.com:81/index.php

使用道具檢舉

您需要登錄後才可以回帖 登錄 | 註冊

Powered by Discuz!

© Comsenz Inc.

重要聲明:本討論區是以即時上載留言的方式運作,對所有留言的真實性、完整性及立場等,不負任何法律責任。而一切留言之言論只代表留言者個人意見,並非本網站之立場,用戶不應信賴內容,並應自行判斷內容之真實性。於有關情形下,用戶應尋求專業意見(如涉及醫療、法律或投資等問題)。 由於本討論區受到「即時上載留言」運作方式所規限,故不能完全監察所有留言,若讀者發現有留言出現問題,請聯絡我們。有權刪除任何留言及拒絕任何人士上載留言,同時亦有不刪除留言的權利。切勿上傳和撰寫 侵犯版權(未經授權)、粗言穢語、誹謗、渲染色情暴力或人身攻擊的言論,敬請自律。本網站保留一切法律權利。
回頂部