shutyou1 該用戶已被刪除 | 成為伊莉的版主,你將獲得更高級和無限的權限。把你感興趣的版面一步步地發展和豐盛,那種滿足感等著你來嚐嚐喔。 本帖最後由 shutyou1 於 2014-6-18 05:02 PM 編輯
1. 概念
線程池主要是通過Executors這個類來創建 返回的是ExecutorService對象
2.固定大小線程池
例子:創建固定線程數目為3的線程池- public class ExecutorThreadPool {
- public static void main(String[] args) {
- /**
- * 創建固定大小的線程池
- */
- ExecutorService threadPool = Executors.newFixedThreadPool(3) ;
- for(int i=1 ;i<=10 ;i++){
- final int taskid = i ;
- threadPool.execute(new Runnable() {
- @Override
- public void run() {
- for(int j=1 ;j<=2 ;j++){
- try {
- Thread.sleep(20) ;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
-
- System.out.println("["+Thread.currentThread().getName()+" ]is loop of "+j+" for task of "+taskid);
-
- }
- }
- }) ;
- }
- }
- }
複製代碼 運行結果如下:
固定的線程池數目為3,那麼始終只會有三個線程在運行,只有等到該三個線程的任務完成以后才會去執行其他的任務
3. 緩衝池
運行結果:
緩衝池會按照jvm的調度來分配若干個線程來執行當前需要完成的任務 ,上面的例子有是個線程任務要執行,那麼自動分配了10個線程來完成
4.單一的線程池
運行結果:- [pool-1-thread-1 ]is loop of 1 for task of 1
- [pool-1-thread-1 ]is loop of 2 for task of 1
- [pool-1-thread-1 ]is loop of 1 for task of 2
- [pool-1-thread-1 ]is loop of 2 for task of 2
- [pool-1-thread-1 ]is loop of 1 for task of 3
- [pool-1-thread-1 ]is loop of 2 for task of 3
- [pool-1-thread-1 ]is loop of 1 for task of 4
- [pool-1-thread-1 ]is loop of 2 for task of 4
- [pool-1-thread-1 ]is loop of 1 for task of 5
- [pool-1-thread-1 ]is loop of 2 for task of 5
- [pool-1-thread-1 ]is loop of 1 for task of 6
- [pool-1-thread-1 ]is loop of 2 for task of 6
- [pool-1-thread-1 ]is loop of 1 for task of 7
- [pool-1-thread-1 ]is loop of 2 for task of 7
- [pool-1-thread-1 ]is loop of 1 for task of 8
- [pool-1-thread-1 ]is loop of 2 for task of 8
- [pool-1-thread-1 ]is loop of 1 for task of 9
- [pool-1-thread-1 ]is loop of 2 for task of 9
- [pool-1-thread-1 ]is loop of 1 for task of 10
- [pool-1-thread-1 ]is loop of 2 for task of 10
複製代碼 ... |
附件: 你需要登錄才可以下載或查看附件。沒有帳號?註冊 |
如果發覺自己無法使用一些功能或出現問題,請按重新整理一次,並待所有網頁內容完全載入後5秒才進行操作。 |
| |
| |
jameschen7 該用戶已被刪除 | 若新密碼無法使用,可能是數據未更新。請使用舊密碼看看。 |
若有安裝色情守門員,可用無界、自由門等軟件瀏覽伊莉。或使用以下網址瀏覽伊莉: http://www.eyny.com:81/index.php |
| |
| |
Powered by Discuz!
© Comsenz Inc.
重要聲明:本討論區是以即時上載留言的方式運作,對所有留言的真實性、完整性及立場等,不負任何法律責任。而一切留言之言論只代表留言者個人意見,並非本網站之立場,用戶不應信賴內容,並應自行判斷內容之真實性。於有關情形下,用戶應尋求專業意見(如涉及醫療、法律或投資等問題)。 由於本討論區受到「即時上載留言」運作方式所規限,故不能完全監察所有留言,若讀者發現有留言出現問題,請聯絡我們。有權刪除任何留言及拒絕任何人士上載留言,同時亦有不刪除留言的權利。切勿上傳和撰寫 侵犯版權(未經授權)、粗言穢語、誹謗、渲染色情暴力或人身攻擊的言論,敬請自律。本網站保留一切法律權利。