一、創建線程池的幾種方式
創建線程池是多線程編程中的一個常見需求。在Java中,使用線程池可以避免在高並發情況下頻繁地創建和銷毀線程,提高系統性能和穩定性。Java中創建線程池的幾種方式如下:
1. 使用ThreadPoolExecutor類手動創建線程池;
2. 使用Executors類提供的靜態方法創建線程池;
3. 使用Spring框架提供的線程池。
二、創建線程池的四種方式
1. 在ThreadPoolExecutor構造方法中傳入必要參數。這種方式可以靈活地配置線程池,如指定核心線程數、最大線程數、線程的空閑時間等參數。
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(coreSize, maxSize, keepAliveTime,TimeUnit.SECONDS, workQueue);
2. 使用Executors工具類中提供的靜態方法創建線程池。Executors類中提供了多個靜態方法,可以根據需要選擇。
ExecutorService threadPool = Executors.newFixedThreadPool(2);
3. 創建ScheduledExecutorService線程池,可用於定時或周期性執行任務。
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(2);
4. 創建支持單個線程的線程池,這是一種特殊的線程池。
ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
三、創建線程池的七種方式
1. newCachedThreadPool()方法創建一個緩存線程池,它可以根據需求創建新線程,但在不被使用時則會回收舊線程。
ExecutorService threadPool = Executors.newCachedThreadPool();
2. newFixedThreadPool()方法創建一個固定大小的線程池,該線程池中的線程數始終保持不變。
ExecutorService threadPool = Executors.newFixedThreadPool(5);
3. newSingleThreadExecutor()方法創建一個只有一個線程的線程池,該線程池保證任何時候都只有一個線程在執行。
ExecutorService threadPool = Executors.newSingleThreadExecutor();
4. newSingleThreadScheduledExecutor()方法創建一個只有一個線程的支持定時執行的線程池。
ScheduledExecutorService scheduledThreadPool = Executors.newSingleThreadScheduledExecutor();
5. newScheduledThreadPool()方法創建一個支持定時執行的線程池。
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
6. newWorkStealingPool()方法創建一個具有工作竊取算法的線程池,可以有效地利用多核CPU的性能。
ExecutorService threadPool = Executors.newWorkStealingPool();
7. ForkJoinPool類創建一個ForkJoinPool線程池,該線程池主要用於解決分治任務。
ForkJoinPool forkJoinPool = new ForkJoinPool();
四、創建線程池的七個參數
1. corePoolSize:線程池中的常駐線程數。
2. maximumPoolSize:線程池中允許的最大線程數。
3. keepAliveTime:線程池中的空閑線程的存活時間。
4. unit:用於指定keepAliveTime的時間單位。
5. workQueue:任務等待隊列。
6. threadFactory:線程工廠,用於創建新線程。
7. handler:拒絕策略,用於處理超出線程池容量時的任務處理方式。
五、創建線程池參數
1. corePoolSize和maximumPoolSize參數的設置。
如果線程池中的線程數少於corePoolSize時,無論有多少任務需要執行,線程池都會創建新線程來處理。當線程池中的線程數已經達到corePoolSize時,新任務會被提交到等待隊列中,直到有空閑線程可以處理為止。如果等待隊列已滿並且池中線程數量還沒有達到maximumPoolSize,則繼續創建新的線程處理任務。如果池中線程數量已經達到maximumPoolSize,拒絕策略便會生效。
2. keepAliveTime參數的設置。
keepAliveTime指定了非核心線程的空閑時間,這些線程會在空閑時間超過這個值時被回收。其單位由unit參數指定。如果把keepAliveTime設置為0或者小於0,則非核心線程會立即被回收。
3. workQueue參數的設置。
workQueue指定了等待隊列,可選的隊列類型包括SynchronousQueue、LinkedTransferQueue、ArrayBlockingQueue、PriorityBlockingQueue等。對於使用SynchronousQueue隊列和maximumPoolSize=1的線程池,可以使得任務不會在隊列中等待,而是直接交給核心線程執行,避免任務執行的延遲。使用ArrayBlockingQueue等有界隊列時,任務進入隊列後,如果線程池中線程數量已經達到corePoolSize時,新任務會在等待隊列中等待一段時間,直到有空閑線程可以處理為止。
六、創建線程池的方法
1. 使用ThreadPoolExecutor類創建線程池。
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(coreSize, maxSize, keepAliveTime,TimeUnit.SECONDS, workQueue);
2. 使用Executors類提供的靜態方法創建線程池。
ExecutorService threadPool = Executors.newFixedThreadPool(2);
七、創建線程池四種方法
1. 在ThreadPoolExecutor構造方法中傳入必要參數:
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(coreSize, maxSize, keepAliveTime,TimeUnit.SECONDS, workQueue);
2. 使用Executors工具類中提供的靜態方法創建線程池:
ExecutorService threadPool = Executors.newFixedThreadPool(2);
3. 創建ScheduledExecutorService線程池,可用於定時或周期性執行任務:
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(2);
4. 創建支持單個線程的線程池:
ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
八、創建線程池進行操作
1. 使用submit()方法將Callable或Runnable任務提交給線程池。
Future<?> future = threadPool.submit(callable);
2. 使用execute()方法將Runnable任務提交給線程池。
threadPool.execute(runnable);
3. 使用invokeAll()方法批量提交Callable任務並返回結果。
List<Future<String>> futures = threadPool.invokeAll(callableTasks);
4. 使用invokeAny()方法提交多個Callable任務並返回其中任意一個任務的結果。
String result = threadPool.invokeAny(callableTasks);
九、創建線程池構造方法
1. ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,BlockingQueue<Runnable> workQueue):最基本的構造方法,通過指定corePoolSize、maximumPoolSize、keepAliveTime、unit和workQueue來創建一個線程池。
2. ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory):指定線程池中線程的創建工廠。
3. ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler):指定線程池中超出線程容量時的拒絕策略。
4. ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory,RejectedExecutionHandler handler):同時指定線程的創建工廠和拒絕策略。
十、小結
本文從多個方面詳細介紹了Java中創建線程池的方式和參數,對於想要使用線程池改善程序性能和穩定性的開發者有一定的參考幫助。但是需要注意的是,對於線程池的使用需要根據具體需求進行細緻的分析和調整,以保證程序的性能和效率。
原創文章,作者:MAMPV,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/318177.html