Java 中線程池創(chuàng)建的幾種方式
首先我們要先知道 Java 中創(chuàng)建線程池的方式,java中創(chuàng)建線程池的方式一般有兩種,如下所示:
- 通過Executors工廠方法創(chuàng)建
- 通過new ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue)自定義創(chuàng)建
Executors 工廠方法創(chuàng)建
上代碼:
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
package com.base.demo.design.play; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; /** * @Description: 線程池代碼 * @BelongsProject: base-demo-design * @BelongsPackage: com.base.demo.design.play * @Author: ChenYongJia * @CreateTime: 2021-08-14 15:26 * @Email: chen87647213@163.com * @Version: 1.0 */ public class TestThreadPoolExecutor { public static void main(String[] args) { // 創(chuàng)建使用單個線程的線程池 ExecutorService es1 = Executors.newSingleThreadExecutor(); for ( int i = 0 ; i < 10 ; i++) { es1.submit( new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName() + "正在執(zhí)行任務(wù)" ); } }); } // 創(chuàng)建使用固定線程數(shù)的線程池 ExecutorService es2 = Executors.newFixedThreadPool( 3 ); for ( int i = 0 ; i < 10 ; i++) { es2.submit( new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName() + "正在執(zhí)行任務(wù)" ); } }); } // 創(chuàng)建一個會根據(jù)需要創(chuàng)建新線程的線程池 ExecutorService es3 = Executors.newCachedThreadPool(); for ( int i = 0 ; i < 20 ; i++) { es3.submit( new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName() + "正在執(zhí)行任務(wù)" ); } }); } // 創(chuàng)建擁有固定線程數(shù)量的定時線程任務(wù)的線程池 ScheduledExecutorService es4 = Executors.newScheduledThreadPool( 2 ); System.out.println( "時間:" + System.currentTimeMillis()); for ( int i = 0 ; i < 5 ; i++) { es4.schedule( new Runnable() { @Override public void run() { System.out.println( "時間:" +System.currentTimeMillis()+ "--" +Thread.currentThread().getName() + "正在執(zhí)行任務(wù)" ); } }, 3 , TimeUnit.SECONDS); } // 創(chuàng)建只有一個線程的定時線程任務(wù)的線程池 ScheduledExecutorService es5 = Executors.newSingleThreadScheduledExecutor(); System.out.println( "時間:" + System.currentTimeMillis()); for ( int i = 0 ; i < 5 ; i++) { es5.schedule( new Runnable() { @Override public void run() { System.out.println( "時間:" +System.currentTimeMillis()+ "--" +Thread.currentThread().getName() + "正在執(zhí)行任務(wù)" ); } }, 3 , TimeUnit.SECONDS); } } } |
運(yùn)行結(jié)果如下:
pool-1-thread-1正在執(zhí)行任務(wù)
pool-1-thread-1正在執(zhí)行任務(wù)
pool-1-thread-1正在執(zhí)行任務(wù)
pool-1-thread-1正在執(zhí)行任務(wù)
pool-1-thread-1正在執(zhí)行任務(wù)
pool-1-thread-1正在執(zhí)行任務(wù)
pool-1-thread-1正在執(zhí)行任務(wù)
pool-1-thread-1正在執(zhí)行任務(wù)
pool-1-thread-1正在執(zhí)行任務(wù)
pool-1-thread-1正在執(zhí)行任務(wù)
pool-2-thread-1正在執(zhí)行任務(wù)
pool-2-thread-2正在執(zhí)行任務(wù)
pool-2-thread-1正在執(zhí)行任務(wù)
pool-2-thread-3正在執(zhí)行任務(wù)
pool-2-thread-2正在執(zhí)行任務(wù)
pool-2-thread-3正在執(zhí)行任務(wù)
pool-2-thread-1正在執(zhí)行任務(wù)
pool-2-thread-3正在執(zhí)行任務(wù)
pool-2-thread-2正在執(zhí)行任務(wù)
pool-2-thread-1正在執(zhí)行任務(wù)
pool-3-thread-1正在執(zhí)行任務(wù)
pool-3-thread-2正在執(zhí)行任務(wù)
pool-3-thread-2正在執(zhí)行任務(wù)
pool-3-thread-3正在執(zhí)行任務(wù)
pool-3-thread-1正在執(zhí)行任務(wù)
pool-3-thread-3正在執(zhí)行任務(wù)
pool-3-thread-4正在執(zhí)行任務(wù)
pool-3-thread-1正在執(zhí)行任務(wù)
pool-3-thread-3正在執(zhí)行任務(wù)
pool-3-thread-4正在執(zhí)行任務(wù)
pool-3-thread-5正在執(zhí)行任務(wù)
pool-3-thread-4正在執(zhí)行任務(wù)
pool-3-thread-6正在執(zhí)行任務(wù)
pool-3-thread-7正在執(zhí)行任務(wù)
pool-3-thread-8正在執(zhí)行任務(wù)
pool-3-thread-9正在執(zhí)行任務(wù)
pool-3-thread-2正在執(zhí)行任務(wù)
pool-3-thread-6正在執(zhí)行任務(wù)
pool-3-thread-1正在執(zhí)行任務(wù)
pool-3-thread-3正在執(zhí)行任務(wù)
時間:1628926041159
時間:1628926041160
時間:1628926044172--pool-5-thread-1正在執(zhí)行任務(wù)
時間:1628926044172--pool-4-thread-2正在執(zhí)行任務(wù)
時間:1628926044172--pool-4-thread-1正在執(zhí)行任務(wù)
時間:1628926044172--pool-4-thread-2正在執(zhí)行任務(wù)
時間:1628926044172--pool-5-thread-1正在執(zhí)行任務(wù)
時間:1628926044172--pool-4-thread-2正在執(zhí)行任務(wù)
時間:1628926044172--pool-4-thread-1正在執(zhí)行任務(wù)
時間:1628926044172--pool-5-thread-1正在執(zhí)行任務(wù)
時間:1628926044172--pool-5-thread-1正在執(zhí)行任務(wù)
時間:1628926044172--pool-5-thread-1正在執(zhí)行任務(wù)
new ThreadPoolExecutor() 自定義創(chuàng)建
1
2
3
4
5
6
7
|
public ThreadPoolExecutor( int corePoolSize, //線程池的核心線程數(shù)量 int maximumPoolSize, //線程池的最大線程數(shù) long keepAliveTime, //當(dāng)線程數(shù)大于核心線程數(shù)時,多余的空閑線程存活的最長時間 TimeUnit unit, //時間單位 BlockingQueue<Runnable> workQueue, //任務(wù)隊(duì)列,用來儲存等待執(zhí)行任務(wù)的隊(duì)列 ThreadFactory threadFactory, //線程工廠,用來創(chuàng)建線程,一般默認(rèn)即可 RejectedExecutionHandler handler) //拒絕策略,當(dāng)提交的任務(wù)過多而不能及時處理時,我們可以定制策略來處理任務(wù) |
- corePoolSize:核心池的大小,這個參數(shù)跟后面講述的線程池的實(shí)現(xiàn)原理有非常大的關(guān)系。在創(chuàng)建了線程池后,默認(rèn)情況下,線程池中并沒有任何線程,而是等待有任務(wù)到來才創(chuàng)建線程去執(zhí)行任務(wù),除非調(diào)用了 prestartAllCoreThreads() 或者 prestartCoreThread() 方法,從這2個方法的名字就可以看出,是預(yù)創(chuàng)建線程的意思,即在沒有任務(wù)到來之前就創(chuàng)建 corePoolSize 個線程或者一個線程。默認(rèn)情況下,在創(chuàng)建了線程池后,線程池中的線程數(shù)為 0,當(dāng)有任務(wù)來之后,就會創(chuàng)建一個線程去執(zhí)行任務(wù),當(dāng)線程池中的線程數(shù)目達(dá)到 corePoolSize 后,就會把到達(dá)的任務(wù)放到緩存隊(duì)列當(dāng)中;
- maximumPoolSize:線程池最大線程數(shù),這個參數(shù)也是一個非常重要的參數(shù),它表示在線程池中最多能創(chuàng)建多少個線程;
- keepAliveTime:表示線程沒有任務(wù)執(zhí)行時最多保持多久時間會終止。默認(rèn)情況下,只有當(dāng)線程池中的線程數(shù)大于 corePoolSize 時,keepAliveTime 才會起作用,直到線程池中的線程數(shù)不大于 corePoolSize,即當(dāng)線程池中的線程數(shù)大于 corePoolSize 時,如果一個線程空閑的時間達(dá)到 keepAliveTime,則會終止,直到線程池中的線程數(shù)不超過 corePoolSize。但是如果調(diào)用了 allowCoreThreadTimeOut(boolean) 方法,在線程池中的線程數(shù)不大于 corePoolSize 時,keepAliveTime 參數(shù)也會起作用,直到線程池中的線程數(shù)為 0;
- unit:參數(shù) keepAliveTime 的時間單位,有7種取值,在 TimeUnit 類中有7種靜態(tài)屬性,如下所示:
1
2
3
4
5
6
7
|
TimeUnit.DAYS; //天 TimeUnit.HOURS; //小時 TimeUnit.MINUTES; //分鐘 TimeUnit.SECONDS; //秒 TimeUnit.MILLISECONDS; //毫秒 TimeUnit.MICROSECONDS; //微妙 TimeUnit.NANOSECONDS; //納秒 |
-
workQueue:阻塞隊(duì)列。保存等待執(zhí)行的任務(wù)的阻塞隊(duì)列,當(dāng)提交一個新的任務(wù)到線程池以后, 線程池會根據(jù)當(dāng)前線程池中正在運(yùn)行著的線程的數(shù)量來決定對該任務(wù)的處理方式,主要有以下幾種處理方式:
- 直接切換:這種方式常用的隊(duì)列是 SynchronousQueue ,不進(jìn)行任務(wù)存儲,直接執(zhí)行;
- 使用無界隊(duì)列:一般使用基于鏈表的阻塞隊(duì)列 LinkedBlockingQueue。如果使用這種方式,那么線程池中能夠創(chuàng)建的最大線程數(shù)就是 corePoolSize ,而 maximumPoolSize 就不會起作用了(因?yàn)槭菬o界的)。當(dāng)線程池中所有的核心線程都是 RUNNING狀態(tài) 時,這時一個新的任務(wù)提交就會放入等待隊(duì)列中。
- 使用有界隊(duì)列:一般使用 ArrayBlockingQueue 。使用該方式可以將線程池的最大線程數(shù)量限制為 maximumPoolSize ,這樣能夠降低資源的消耗,但同時這種方式也使得線程池對線程的調(diào)度變得更困難,因?yàn)榫€程池和隊(duì)列的容量都是有限的值,所以要想使線程池處理任務(wù)的吞吐率達(dá)到一個相對合理的范圍,又想使線程調(diào)度相對簡單,并且還要盡可能的降低線程池對資源的消耗,就需要合理的設(shè)置這兩個數(shù)量。
- PS:ArrayBlockingQueue 和 PriorityBlockingQueue 使用較少,一般使用 LinkedBlockingQueue 和 SynchronousQueue。線程池的排隊(duì)策略與 BlockingQueue 有關(guān)。
- threadFactory:用于設(shè)置創(chuàng)建線程的工廠,可以通過線程工廠給每個創(chuàng)建出來的線程做些更有意義的事情,比如設(shè)置 daemon 和 優(yōu)先級 等等
- handler:飽和策略。當(dāng)線程池的阻塞隊(duì)列已滿和指定的線程都已經(jīng)開啟,說明當(dāng)前線程池已經(jīng)處于飽和狀態(tài)了,那么就需要采用一種策略來處理這種情況。表示當(dāng)拒絕處理任務(wù)時的策略,有以下幾種取值(也可以根據(jù)應(yīng)用場景需要來實(shí)現(xiàn) RejectedExecutionHandler 接口自定義策略。如記錄日志或持久化不能處理的任務(wù)。):
AbortPolicy:默認(rèn)的拒絕策略,直接拋出異常。`throws RejectedExecutionException`。
CallerRunsPolicy:只用調(diào)用者所在線程來運(yùn)行任務(wù)(提交任務(wù)的線程自己去執(zhí)行該任務(wù))。
DiscardOldestPolicy:丟棄最老的任務(wù),其實(shí)就是把最早進(jìn)入工作隊(duì)列的任務(wù)丟棄,然后把新任務(wù)加入到工作隊(duì)列。
DiscardPolicy:不處理,直接丟棄任務(wù),沒有任何異常拋出。
執(zhí)行流程:
- 線程池創(chuàng)建線程,會判斷當(dāng)前線程數(shù)是否大于 corePoolSize。
- 如果大于則存在緩存隊(duì)列,緩沖隊(duì)列存滿后會繼續(xù)創(chuàng)建線程直到 maximumPoolSize ,拋出拒絕的異常。
- 如果小于則創(chuàng)建線程,執(zhí)行任務(wù),執(zhí)行完后會從緩存隊(duì)列中取任務(wù)再執(zhí)行
創(chuàng)建多少線程合適
一般多線程執(zhí)行的任務(wù)類型可以分為 CPU 密集型 和 I/O 密集型,根據(jù)不同的任務(wù)類型,我們計(jì)算線程數(shù)的方法也不一樣。創(chuàng)建多少線程合適,要看多線程具體的應(yīng)用場景。我們的程序一般都是 CPU 計(jì)算 和 I/O 操作交叉執(zhí)行 的,由于 I/O 設(shè)備 的速度相對于 CPU 來說都很慢,所以大部分情況下,I/O 操作執(zhí)行的時間相對于 CPU 計(jì)算來說都非常長 ,這種場景我們一般都稱為 I/O 密集型計(jì)算 和 I/O 密集型計(jì)算 相對的就是 CPU 密集型計(jì)算,CPU 密集型計(jì)算 大部分場景下都是 純 CPU 計(jì)算。
- CPU 密集型任務(wù):多線程主要目的是提成CPU利用率,保持和CPU核數(shù)一致即可。可以將線程數(shù)設(shè)置為 N(CPU 核心數(shù))+1 ,比 CPU 核心數(shù) 多出來的一個線程是 為了防止線程偶發(fā)的缺頁中斷,或者其它原因?qū)е碌?任務(wù)暫停 而帶來的影響。一旦 任務(wù)暫停 ,CPU 就會處于 空閑狀態(tài) ,而在這種情況下多出來的一個線程就可以充分利用 CPU 的空閑時間。
- 測試代碼如下,結(jié)果還是自己親力親為吧實(shí)踐出真知
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
package com.base.demo.design.play; import java.util.List; /** * @Description: CPU測試 * @BelongsProject: base-demo-design * @BelongsPackage: com.base.demo.design.play * @Author: ChenYongJia * @CreateTime: 2021-08-14 16:13 * @Email: chen87647213@163.com * @Version: 1.0 */ public class CPUTypeTest implements Runnable { /** * 整體執(zhí)行時間,包括在隊(duì)列中等待的時間 */ List<Long> wholeTimeList; /** * 真正執(zhí)行時間 */ List<Long> runTimeList; private long initStartTime = 0 ; /** * 構(gòu)造函數(shù) * * @param runTimeList * @param wholeTimeList * @return * @date 2021/8/14 16:13 * @author ChenYongJia * @version 1.0 */ public CPUTypeTest(List<Long> runTimeList, List<Long> wholeTimeList) { initStartTime = System.currentTimeMillis(); this .runTimeList = runTimeList; this .wholeTimeList = wholeTimeList; } /** * 判斷素?cái)?shù) * * @param number * @return boolean * @date 2021/8/14 16:13 * @author ChenYongJia * @version 1.0 */ public boolean isPrime( final int number) { if (number <= 1 ) return false ; for ( int i = 2 ; i <= Math.sqrt(number); i++) { if (number % i == 0 ) return false ; } return true ; } /** * 計(jì)算素?cái)?shù) * * @param lower * @param upper * @return int * @date 2021/8/14 16:14 * @author ChenYongJia * @version 1.0 */ public int countPrimes( final int lower, final int upper) { int total = 0 ; for ( int i = lower; i <= upper; i++) { if (isPrime(i)) total++; } return total; } @Override public void run() { long start = System.currentTimeMillis(); countPrimes( 1 , 1000000 ); long end = System.currentTimeMillis(); long wholeTime = end - initStartTime; long runTime = end - start; wholeTimeList.add(wholeTime); runTimeList.add(runTime); System.out.println( "單個線程花費(fèi)時間:" + (end - start)); } } |
I/O 密集型任務(wù):這種任務(wù)應(yīng)用起來,系統(tǒng)會用大部分的時間來處理 I/O 交互,而線程在處理 I/O 的時間段內(nèi)不會占用 CPU 來處理,這時就可以將 CPU 交出給其它線程使用。因此在 I/O 密集型任務(wù)的應(yīng)用中,我們可以多配置一些線程,具體的計(jì)算方法是 2N(CPU 核心數(shù))。
- 一般最佳線程數(shù)目 = (線程等待時間與線程CPU時間之比 + 1)* CPU數(shù)目
- 實(shí)戰(zhàn):實(shí)際需要根據(jù)上線情況進(jìn)行調(diào)整優(yōu)化
- 測試代碼如下,結(jié)果還是自己親力親為吧實(shí)踐出真知
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
package com.base.demo.design.play; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.Vector; /** * @Description: IO測試 * @BelongsProject: base-demo-design * @BelongsPackage: com.base.demo.design.play * @Author: ChenYongJia * @CreateTime: 2021-08-14 16:18 * @Email: chen87647213@163.com * @Version: 1.0 */ public class IOTypeTest implements Runnable { /** * 整體執(zhí)行時間,包括在隊(duì)列中等待的時間 */ Vector<Long> wholeTimeList; /** * 真正執(zhí)行時間 */ Vector<Long> runTimeList; private long initStartTime = 0 ; /** * 構(gòu)造函數(shù) * * @param runTimeList * @param wholeTimeList */ public IOTypeTest(Vector<Long> runTimeList, Vector<Long> wholeTimeList) { initStartTime = System.currentTimeMillis(); this .runTimeList = runTimeList; this .wholeTimeList = wholeTimeList; } /** * IO操作 * * @return void * @date 2021/8/14 16:18 * @author ChenYongJia * @version 1.0 */ public void readAndWrite() throws IOException { File sourceFile = new File( "D:/test.txt" ); //創(chuàng)建輸入流 BufferedReader input = new BufferedReader( new FileReader(sourceFile)); //讀取源文件,寫入到新的文件 String line = null ; while ((line = input.readLine()) != null ) { //System.out.println(line); } //關(guān)閉輸入輸出流 input.close(); } @Override public void run() { long start = System.currentTimeMillis(); try { readAndWrite(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } long end = System.currentTimeMillis(); long wholeTime = end - initStartTime; long runTime = end - start; wholeTimeList.add(wholeTime); runTimeList.add(runTime); System.out.println( "單個線程花費(fèi)時間:" + (end - start)); } } |
喜歡折騰的同學(xué)可以試試(我貼出來的兩段 TEST代碼):在不同線程數(shù)的情況下,run 方法運(yùn)行時間的差異。可以通過創(chuàng)建不同數(shù)量的線程,線程中 new 該 Test 對象(new 兩個 List 傳到構(gòu)造參數(shù)里)并提交到線程池。查看并歸納計(jì)算得出結(jié)果。
附:線程池原理
學(xué)習(xí)線程池的實(shí)現(xiàn)原理,有助于你更好地理解內(nèi)容。
在 HotSpot VM 的線程模型中,Java 線程被一對一映射為內(nèi)核線程。Java 在使用線程執(zhí)行程序時,需要創(chuàng)建一個內(nèi)核線程;當(dāng)該 Java 線程被終止時,這個內(nèi)核線程也會被回收。因此 Java 線程的創(chuàng)建與銷毀將會消耗一定的計(jì)算機(jī)資源,從而增加系統(tǒng)的性能開銷。
除此之外,大量創(chuàng)建線程同樣會給系統(tǒng)帶來性能問題,因?yàn)閮?nèi)存和 CPU 資源都將被線程搶占,如果處理不當(dāng),就會發(fā)生內(nèi)存溢出、CPU 使用率超負(fù)荷等問題。
為了解決上述兩類問題,Java 提供了線程池概念,對于頻繁創(chuàng)建線程的業(yè)務(wù)場景,線程池可以創(chuàng)建固定的線程數(shù)量,并且在操作系統(tǒng)底層,輕量級進(jìn)程將會把這些線程映射到內(nèi)核。
線程池可以提高線程復(fù)用,又可以固定最大線程使用量,防止無限制地創(chuàng)建線程。當(dāng)程序提交一個任務(wù)需要一個線程時,會去線程池中查找是否有空閑的線程,若有,則直接使用線程池中的線程工作,若沒有,會去判斷當(dāng)前已創(chuàng)建的線程數(shù)量是否超過最大線程數(shù)量,如未超過,則創(chuàng)建新線程,如已超過,則進(jìn)行排隊(duì)等待或者直接拋出異常。
最后
在不同的業(yè)務(wù)場景以及不同配置的部署機(jī)器中,線程池的線程數(shù)量設(shè)置是不一樣的。其設(shè)置不宜過大,也不宜過小,要根據(jù)具體情況,計(jì)算出一個大概的數(shù)值,再通過實(shí)際的性能測試,計(jì)算出一個合理的線程數(shù)量。
到此這篇關(guān)于Java線程池大小設(shè)置的文章就介紹到這了,更多相關(guān)Java線程池大小設(shè)置內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://juejin.cn/post/6997566049155547166