日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

2020最新Java线程池入门(超详细)

發(fā)布時(shí)間:2023/12/3 java 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2020最新Java线程池入门(超详细) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

轉(zhuǎn) https://blog.csdn.net/weixin_43893397/article/details/104361154

【1】代碼示例?

/*** 線程池測(cè)試-自定義線程池創(chuàng)建方式* @since 2021/03/23 */ public class ThreadPoolMain2 {public static void main(String[] args) throws Exception {newMethod();}public static void newMethod() throws InterruptedException {/*** 開啟一個(gè)線程池 默認(rèn)線程是10個(gè)* 使用默認(rèn)線程工廠* 拒絕策略為CallerRunsPolicy策略,讓后面的線程先等待 */ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 10, 1000L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),Executors.defaultThreadFactory(),new ThreadPoolExecutor.CallerRunsPolicy());//書寫一個(gè)循環(huán) 模擬100個(gè)用戶同時(shí)訪問(wèn)for (int request = 0; request< 100;request ++){final int temp = request; //開啟一個(gè)線程threadPoolExecutor.execute(() ->{System.out.println("任務(wù)" + temp +"開始執(zhí)行...");/*** 睡10秒 */try {Thread.sleep(1000L * 3);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("任務(wù)" + temp +"執(zhí)行結(jié)束...");});}} }

【2】 ThreadPoolExecutor 構(gòu)造器解析

/*** Creates a new {@code ThreadPoolExecutor} with the given initial* parameters.** @param corePoolSize the number of threads to keep in the pool, even* if they are idle, unless {@code allowCoreThreadTimeOut} is set* @param maximumPoolSize the maximum number of threads to allow in the* pool* @param keepAliveTime when the number of threads is greater than* the core, this is the maximum time that excess idle threads* will wait for new tasks before terminating.* @param unit the time unit for the {@code keepAliveTime} argument* @param workQueue the queue to use for holding tasks before they are* executed. This queue will hold only the {@code Runnable}* tasks submitted by the {@code execute} method.* @param threadFactory the factory to use when the executor* creates a new thread* @param handler the handler to use when execution is blocked* because the thread bounds and queue capacities are reached* @throws IllegalArgumentException if one of the following holds:<br>* {@code corePoolSize < 0}<br>* {@code keepAliveTime < 0}<br>* {@code maximumPoolSize <= 0}<br>* {@code maximumPoolSize < corePoolSize}* @throws NullPointerException if {@code workQueue}* or {@code threadFactory} or {@code handler} is null*/public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue<Runnable> workQueue,ThreadFactory threadFactory,RejectedExecutionHandler handler) {

【3】參數(shù)解析

序號(hào)參數(shù)名描述中文
1corePoolSizethe number of threads to keep in the pool, even if they are idle, unless {@code allowCoreThreadTimeOut} is set.除非設(shè)置了{(lán)@code allowCoreThreadTimeOut},否則即使它們處于空閑狀態(tài)也要保留在池中的線程數(shù)。
2maximumPoolSize?the maximum number of threads to allow in the pool池中允許的最大線程數(shù)
3keepAliveTimewhen the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating.當(dāng)線程數(shù)大于內(nèi)核數(shù)時(shí),這是多余的空閑線程將在終止之前等待新任務(wù)的最長(zhǎng)時(shí)間。
4unitthe time unit for the {@code keepAliveTime} argument{@code keepAliveTime}參數(shù)的時(shí)間單位
5workQueue

?the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method.

在執(zhí)行任務(wù)之前用于保留任務(wù)的隊(duì)列。 此隊(duì)列將僅保存execute方法提交的Runnable任務(wù)。
6threadFactorythe factory to use when the executorcreates a new thread.執(zhí)行程序創(chuàng)建新線程時(shí)要使用的工廠。
7handler

?the handler to use when execution is blockedbecause the thread bounds and queue capacities are reached.

當(dāng)執(zhí)行被阻塞時(shí)使用的處理程序,因?yàn)檫_(dá)到了線程界限和隊(duì)列容量。

【4】拋棄策略

// 當(dāng)任務(wù)無(wú)法添加到阻塞隊(duì)列時(shí)的處理策略-直接拋出異常RejectedExecutionHandler rejectHandler1 = new ThreadPoolExecutor.AbortPolicy(); // -會(huì)調(diào)用當(dāng)前線程池的所在的線程去執(zhí)行被拒絕的任務(wù)。RejectedExecutionHandler rejectHandler2 = new ThreadPoolExecutor.CallerRunsPolicy(); // 會(huì)拋棄任務(wù)隊(duì)列中最舊的任務(wù)也就是最先加入隊(duì)列的,再把這個(gè)新任務(wù)添加進(jìn)去。RejectedExecutionHandler rejectHandler3 = new ThreadPoolExecutor.DiscardOldestPolicy();// 會(huì)讓被線程池拒絕的任務(wù)直接拋棄,不會(huì)拋異常也不會(huì)執(zhí)行。RejectedExecutionHandler rejectHandler4 = new ThreadPoolExecutor.DiscardPolicy(); RejectedExecutionHandler rejectHandler = rejectHandler4; ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 10, 1000L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),Executors.defaultThreadFactory(), rejectHandler);

?

?

?

?

總結(jié)

以上是生活随笔為你收集整理的2020最新Java线程池入门(超详细)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。