一般來說池中總線程數是核心池線程數量兩倍,只要確保當核心池有線程停止時,核心池外能有線程進入核心池即可。
我們所需要關心的主要是核心池線程的數量該如何設置。
自定義線程池代碼
package com.lc.concurrent; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class MyThreadPoolExecutor { //最大可用的CPU核數 public static final int PROCESSORS=Runtime.getRuntime().availableProcessors(); //線程最大的空閑存活時間,單位為秒 public static final int KEEPALIVETIME=60; //任務緩存隊列長度 public static final int BLOCKINGQUEUE_LENGTH=500; public ThreadPoolExecutor createThreadPool(){ return new ThreadPoolExecutor(PROCESSORS * 2,PROCESSORS * 4,KEEPALIVETIME,TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(BLOCKINGQUEUE_LENGTH)); } }
線程中的任務最終是交給CPU的線程去處理的,而CPU可同時處理線程數量大部分是CPU核數的兩倍,運行環境中CPU
的核數我們可以通過Runtime.getRuntime().availableProcessors()這個方法而獲取。理論上來說核心池線程數量應該為
Runtime.getRuntime().availableProcessors()*2,那么結果是否符合我們的預期呢,可以來測試一下(本次測試測試的
是I/O密集型任務,事實上大部分的任務都是I/O密集型的,即大部分任務消耗集中在的輸入輸出。而CPU密集型任務主
要消耗CPU資源進行計算,當任務為CPU密集型時,核心池線程數設置為CPU核數+1即可)
package com.lc.concurrent; import java.util.Arrays; import java.util.Random; import java.util.concurrent.ThreadPoolExecutor; public class CreateThreads { public synchronized static void main(String[] args) { System.out.println(MyThreadPoolExecutor.PROCESSORS); new CreateThreads().test(); } public synchronized void test(){ ThreadPoolExecutor threadPoolExecutor=new MyThreadPoolExecutor().createThreadPool(); for (int i = 0; i <= 100; i++) { MyTask myTask = new MyTask(i); threadPoolExecutor.execute(myTask); } threadPoolExecutor.shutdown(); } } class MyTask implements Runnable{ private int i; public MyTask(int i){ this.i=i; } @Override public void run() { System.out.println("任務"+i+"開始執行"+System.currentTimeMillis()); for (int i=0;i<32766;i++){ Random random=new Random(); int randNum=random.nextInt(); int[] a={1,2,3,4,5,6,9,18,290,238,991,100,19,1932,randNum}; Arrays.sort(a); Arrays.hashCode(a); Arrays.stream(a); } System.out.println("任務"+i+"結束執行"+System.currentTimeMillis()); } }
本機CPU核數為4,可同時處理8線程,測試結果如下:
核心池線程數量 執行耗時(毫秒,多次測試結果以/間隔)
4 474/479/471
8 430/436/421
12 432/425/438
16 437/431/449
20 471/481/469
可以發現當線程數量小於CPU核數兩倍時速度明顯較慢,超過兩倍后速度差不多,當核心池數量過多時,速度又會顯著下降
由此可以看出,核心池線程數量大小應在CPU核數兩倍以上且不宜過多。
所以說,將線程池的核心池線程數量配置為CPU核數的兩倍是比較合適的。