- 相關(guān)推薦
Java自定義線程池相關(guān)代碼
創(chuàng)建Java自定義線程池的構(gòu)造方法很多,但是我們在使用中就會有以下幾個最主要的代碼應(yīng)用。我們在使用的時候就要先來了解下有關(guān)Java自定義線程池的源代碼。本例中參數(shù)的含義如下:
Java代碼
1.ThreadPoolExecutor
2.public ThreadPoolExecutor(int corePoolSize,
3.int maximumPoolSize,
4.long keepAliveTime,
5.TimeUnit unit,
6.BlockingQueue workQueue)
用給定的初始參數(shù)和默認(rèn)的線程工廠及處理程序創(chuàng)建新的 ThreadPoolExecutor。使用 Executors 工廠方法之一比使用此通用構(gòu)造方法方便得多。
參數(shù):
7.corePoolSize - 池中所保存的線程數(shù),包括空閑線程。
8.maximumPoolSize - 池中允許的最大線程數(shù)。
9.keepAliveTime - 當(dāng)線程數(shù)大于核心時,此為終止前多余的空閑線程
等待新任務(wù)的最長時間。
10.unit - keepAliveTime 參數(shù)的時間單位。
11.workQueue - 執(zhí)行前用于保持任務(wù)的隊列。此隊列僅保持由 execute
方法提交的 Runnable 任務(wù)。
拋出:
IllegalArgumentException - 如果 corePoolSize 或 keepAliveTime 小于零,或者 maximumPoolSize 小于或等于零,或者 corePoolSize 大于 maximumPoolSize。
NullPointerException - 如果 workQueue 為 null
12.ThreadPoolExecutor
13.public ThreadPoolExecutor(int corePoolSize,
14.int maximumPoolSize,
15.long keepAliveTime,
16.TimeUnit unit,
17.BlockingQueue workQueue)
用給定的初始參數(shù)和默認(rèn)的線程工廠及處理程序創(chuàng)建新的 ThreadPoolExecutor。使用 Executors 工廠方法之一比使用此通用構(gòu)造方法方便得多。
參數(shù):
18.corePoolSize - 池中所保存的線程數(shù),包括空閑線程。
19.maximumPoolSize - 池中允許的最大線程數(shù)。
20.keepAliveTime - 當(dāng)線程數(shù)大于核心時,此為終止前多余的空閑
線程等待新任務(wù)的最長時間。
21.unit - keepAliveTime 參數(shù)的時間單位。
22.workQueue - 執(zhí)行前用于保持任務(wù)的隊列。此隊列僅保持由 execute
方法提交的 Runnable 任務(wù)。
23.拋出:
24.IllegalArgumentException - 如果 corePoolSize 或
keepAliveTime 小于零,或者 maximumPoolSize 小于或等于零,
或者 corePoolSize 大于 maximumPoolSize。
25.NullPointerException - 如果 workQueue 為 null
Java自定義線程池稍微麻煩些,不過通過創(chuàng)建的ThreadPoolExecutor線程池對象,可以獲取到當(dāng)前線程池的尺寸、正在執(zhí)行任務(wù)的線程數(shù)、工作隊列等等。
【Java自定義線程池相關(guān)代碼】相關(guān)文章:
Java線程同步的方法10-25
Java多線程的實現(xiàn)方式07-08
在Java中執(zhí)行JavaScript代碼07-14
如何讓JAVA代碼更高效07-18
java證書的加密與解密代碼06-12
Java中的動態(tài)代碼編程06-27
Java代碼的基本知識10-26
java多線程面試題201710-03
關(guān)于Java源代碼折行的規(guī)則10-27
java非對稱加密的源代碼(rsa)08-01