java通過executors提供四種線程池,分別為:
newcachedthreadpool創建一個可緩存線程池,如果線程池長度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程。
newfixedthreadpool 創建一個定長線程池,可控制線程最大并發數,超出的線程會在隊列中等待。
newscheduledthreadpool 創建一個定長線程池,支持定時及周期性任務執行。
newsinglethreadexecutor 創建一個單線程化的線程池,它只會用唯一的工作線程來執行任務,保證所有任務按照指定順序(fifo, lifo, 優先級)執行。
(1) newcachedthreadpool
創建一個可緩存線程池,如果線程池長度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程。示例代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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
創建一個定長線程池,可控制線程最大并發數,超出的線程會在隊列中等待。示例代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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
創建一個定長線程池,支持定時及周期性任務執行。延遲執行示例代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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秒執行。
定期執行示例代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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, 優先級)執行。示例代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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
運行程序做稍微修改:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
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(); } } } } |
效果如下:
選擇我們運行的程序:
監控運行狀態
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://cuisuqiang.iteye.com/blog/2019372