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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java new thread参数_java线程池01-ThreadPoolExecutor构造方法参数的使用规则

發(fā)布時(shí)間:2024/9/19 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java new thread参数_java线程池01-ThreadPoolExecutor构造方法参数的使用规则 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

為了更好的使用多線程,JDK提供了線程池供開發(fā)人員使用,目的在于減少線程的創(chuàng)建和銷毀次數(shù),以此達(dá)到線程的重復(fù)利用。

其中ThreadPoolExecutor是線程池中最核心的一個(gè)類,我們先簡(jiǎn)單看一下這個(gè)類的繼承關(guān)系。

其中Executor是線程池的頂級(jí)接口,接口中只定義了一個(gè)方法? void execute(Runnable command);線程池的操作方法都是定義子在ExecutorService子接口中的,所以說ExecutorService是線程池真正的接口。

ThreadPoolExecutor提供了四個(gè)構(gòu)造方法,我們看一下參數(shù)最全的一個(gè)構(gòu)造函數(shù);

public ThreadPoolExecutor(intcorePoolSize,intmaximumPoolSize,longkeepAliveTime,

TimeUnit unit,

BlockingQueueworkQueue,

ThreadFactory threadFactory,

RejectedExecutionHandler handler) {}

函數(shù)的參數(shù)含義如下:

corePoolSize: 線程池核心線程數(shù)

maximumPoolSize:線程池最大數(shù)

keepAliveTime: 空閑線程存活時(shí)間

unit: 時(shí)間單位

workQueue: 線程池所使用的緩沖隊(duì)列

threadFactory:線程池創(chuàng)建線程使用的工廠

handler: 線程池對(duì)拒絕任務(wù)的處理策略

本節(jié)我們主要對(duì)前五個(gè)參數(shù)中的corePoolSize,maximumPoolSize及workQueue是如何配合使用做出說明(keepAliveTime,unit主要對(duì)空閑線程的存活時(shí)間做的定義,見名知意,不再做出說明),以此來引出線程池的一些特性。

threadFactory和handler這兩個(gè)參數(shù)都有默認(rèn)值,對(duì)于它們的用法將放到其它章節(jié)去做說明。

特性一:當(dāng)池中正在運(yùn)行的線程數(shù)(包括空閑線程)小于corePoolSize時(shí),新建線程執(zhí)行任務(wù)。

下面用實(shí)驗(yàn)來說明,代碼如下:

public classTestThreadPoolExecutor {public static voidmain(String[] args) {

ThreadPoolExecutor pool= new ThreadPoolExecutor(2, 3, 60L, TimeUnit.SECONDS,new LinkedBlockingQueue<>(1));//任務(wù)1

pool.execute(newRunnable() {

@Overridepublic voidrun() {

System.out.println("-------------helloworld_001---------------" +Thread.currentThread().getName());

}

});try{//主線程睡2秒

Thread.sleep(2*1000);

}catch(InterruptedException e) {

e.printStackTrace();

}//任務(wù)2

pool.execute(newRunnable() {

@Overridepublic voidrun() {

System.out.println("-------------helloworld_002---------------" +Thread.currentThread().getName());

}

});

}

}

實(shí)驗(yàn)結(jié)果如下:

實(shí)驗(yàn)結(jié)果分析:

從實(shí)驗(yàn)結(jié)果上可以看出,當(dāng)執(zhí)行任務(wù)1的線程(thread-1)執(zhí)行完成之后,任務(wù)2并沒有去復(fù)用thread-1而是新建線程(thread-2)去執(zhí)行任務(wù)。

特性二:當(dāng)池中正在運(yùn)行的線程數(shù)大于等于corePoolSize時(shí),新插入的任務(wù)進(jìn)入workQueue排隊(duì)(如果workQueue長度允許),等待空閑線程來執(zhí)行。

下面用實(shí)驗(yàn)來說明,代碼如下:

public classTestThreadPoolExecutor {public static voidmain(String[] args) {

ThreadPoolExecutor pool= new ThreadPoolExecutor(2, 3, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1));//任務(wù)1

pool.execute(newRunnable() {

@Overridepublic voidrun() {try{

Thread.sleep(3 * 1000);

System.out.println("-------------helloworld_001---------------" +Thread.currentThread().getName());

}catch(InterruptedException e) {

e.printStackTrace();

}

}

});//任務(wù)2

pool.execute(newRunnable() {

@Overridepublic voidrun() {try{

Thread.sleep(5 * 1000);

System.out.println("-------------helloworld_002---------------" +Thread.currentThread().getName());

}catch(InterruptedException e) {

e.printStackTrace();

}

}

});//任務(wù)3

pool.execute(newRunnable() {

@Overridepublic voidrun() {

System.out.println("-------------helloworld_003---------------" +Thread.currentThread().getName());

}

});

}

}

實(shí)驗(yàn)結(jié)果如下:

實(shí)驗(yàn)結(jié)果分析:

從實(shí)驗(yàn)結(jié)果上看,任務(wù)3會(huì)等待任務(wù)1執(zhí)行完之后,有了空閑線程,才會(huì)執(zhí)行。并沒有新建線程執(zhí)行任務(wù)3,這時(shí)maximumPoolSize=3這個(gè)參數(shù)不起作用。

特性三:當(dāng)隊(duì)列里的任務(wù)數(shù)達(dá)到上限,并且池中正在運(yùn)行的線程數(shù)小于maximumPoolSize,對(duì)于新加入的任務(wù),新建線程。

下面用實(shí)驗(yàn)來說明,代碼如下:

public classTestThreadPoolExecutor {public static voidmain(String[] args) {

ThreadPoolExecutor pool= new ThreadPoolExecutor(2, 3, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1));//任務(wù)1

pool.execute(newRunnable() {

@Overridepublic voidrun() {try{

Thread.sleep(3 * 1000);

System.out.println("-------------helloworld_001---------------" +Thread.currentThread().getName());

}catch(InterruptedException e) {

e.printStackTrace();

}

}

});//任務(wù)2

pool.execute(newRunnable() {

@Overridepublic voidrun() {try{

Thread.sleep(5 * 1000);

System.out.println("-------------helloworld_002---------------" +Thread.currentThread().getName());

}catch(InterruptedException e) {

e.printStackTrace();

}

}

});//任務(wù)3

pool.execute(newRunnable() {

@Overridepublic voidrun() {

System.out.println("-------------helloworld_003---------------" +Thread.currentThread().getName());

}

});//任務(wù)4

pool.execute(newRunnable() {

@Overridepublic voidrun() {

System.out.println("-------------helloworld_004---------------" +Thread.currentThread().getName());

}

});

}

}

實(shí)驗(yàn)結(jié)果如下:

實(shí)驗(yàn)結(jié)果分析:

當(dāng)任務(wù)4進(jìn)入隊(duì)列時(shí)發(fā)現(xiàn)隊(duì)列的長度已經(jīng)到了上限,所以無法進(jìn)入隊(duì)列排隊(duì),而此時(shí)正在運(yùn)行的線程數(shù)(2)小于maximumPoolSize所以新建線程執(zhí)行該任務(wù)。

特性四:當(dāng)隊(duì)列里的任務(wù)數(shù)達(dá)到上限,并且池中正在運(yùn)行的線程數(shù)等于maximumPoolSize,對(duì)于新加入的任務(wù),執(zhí)行拒絕策略(線程池默認(rèn)的拒絕策略是拋異常)。

下面用實(shí)驗(yàn)來說明,代碼如下:

public classTestThreadPoolExecutor {public static voidmain(String[] args) {

ThreadPoolExecutor pool= new ThreadPoolExecutor(2, 3, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1));//任務(wù)1

pool.execute(newRunnable() {

@Overridepublic voidrun() {try{

Thread.sleep(3 * 1000);

System.out.println("-------------helloworld_001---------------" +Thread.currentThread().getName());

}catch(InterruptedException e) {

e.printStackTrace();

}

}

});//任務(wù)2

pool.execute(newRunnable() {

@Overridepublic voidrun() {try{

Thread.sleep(5 * 1000);

System.out.println("-------------helloworld_002---------------" +Thread.currentThread().getName());

}catch(InterruptedException e) {

e.printStackTrace();

}

}

});//任務(wù)3

pool.execute(newRunnable() {

@Overridepublic voidrun() {

System.out.println("-------------helloworld_003---------------" +Thread.currentThread().getName());

}

});//任務(wù)4

pool.execute(newRunnable() {

@Overridepublic voidrun() {try{

Thread.sleep(2 * 1000);

}catch(InterruptedException e) {

e.printStackTrace();

}

System.out.println("-------------helloworld_004---------------" +Thread.currentThread().getName());

}

});//任務(wù)5

pool.execute(newRunnable() {

@Overridepublic voidrun() {

System.out.println("-------------helloworld_005---------------" +Thread.currentThread().getName());

}

});

}

}

實(shí)驗(yàn)結(jié)果如下:

實(shí)驗(yàn)結(jié)果分析:

當(dāng)任務(wù)5加入時(shí),隊(duì)列達(dá)到上限,池內(nèi)運(yùn)行的線程數(shù)達(dá)到最大,故執(zhí)行默認(rèn)的拒絕策略,拋異常。

本文中使用到的隊(duì)列類型雖然僅限于LinkedBlockingQueue這一種隊(duì)列類型,但總結(jié)出來的特性,對(duì)與常用ArrayBlockingQueue 和?SynchronousQueue同樣適用,些許不同及三種隊(duì)列的區(qū)別,將在下個(gè)章節(jié)中說明。

最后說一點(diǎn),我們作為程序員,研究問題還是要仔細(xì)深入一點(diǎn)的。當(dāng)你對(duì)原理了解的有夠透徹,開發(fā)起來也就得心應(yīng)手了,很多開發(fā)中的問題和疑惑也就迎刃而解了,而且在面對(duì)其他問題的時(shí)候也可做到觸類旁通。當(dāng)然在開發(fā)中沒有太多的時(shí)間讓你去研究原理,開發(fā)中要以實(shí)現(xiàn)功能為前提,可等項(xiàng)目上線的后,你有大把的時(shí)間或者空余的時(shí)間,你大可去刨根問底,深入的去研究一項(xiàng)技術(shù),為覺得這對(duì)一名程序員的成長是很重要的事情。

總結(jié)

以上是生活随笔為你收集整理的java new thread参数_java线程池01-ThreadPoolExecutor构造方法参数的使用规则的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 国产精品11 | 欧美中文字幕在线播放 | 国产靠逼视频 | 女人性高潮视频 | 国产精品女优 | 亚洲一区二区三区91 | 青青草国产在线 | wwwav在线播放 | 亚洲国产aⅴ精品一区二区的游戏 | 国产又黄又粗又硬 | 正在播放木下凛凛xv99 | 性饥渴的农村熟妇 | 一区二区三区精品国产 | 青青草视频网站 | 看特级毛片 | av在线资源| 国产乱强伦一区二区三区 | 欧美国产乱视频 | 久久精品视频免费播放 | 天天干天天日夜夜操 | 欧美成人免费 | 天天干天天爱天天操 | 小蝌蚪av| 国产一区二区在线电影 | 亚洲天堂免费在线 | 一区二区三区入口 | 日韩精品一区二区视频 | 成年人在线播放视频 | 日日干天天爽 | 色诱视频在线观看 | 国产成人午夜视频 | 20日本xxxxxxxxx46 欧美激情一级 | 中文字幕素人 | 2024国产精品 | 老熟女一区二区三区 | 色婷婷精品视频 | 在线中文字幕视频 | 国产乱子一区二区 | 乌克兰av在线 | 成年人网站免费看 | 成人123区| 国产精品久久国产精品 | 日本美女三级 | 探花视频在线免费观看 | 亚洲国产成人久久 | 99这里 | 农村老女人av | 国产视频你懂得 | 艳妇臀荡乳欲伦交换h漫 | 尤物av在线 | 亚洲一区二区三区不卡视频 | 特黄一级片 | 欧美a在线播放 | 欧美性大交 | 欧美视频a | 久久精品9 | 久久精品这里有 | wwwxxx日本人 | 午夜8888 | 男男车车的车车网站w98免费 | 色在线看| 国产一区亚洲一区 | 国产精品99久久免费黑人人妻 | 日韩午夜影院 | 日本高清视频一区二区 | 亚洲经典在线观看 | 国产sm网站 | 国产一区视频在线观看免费 | 成人黄色小说视频 | 狂野欧美性猛交免费视频 | 欧美韩国日本一区 | 不卡中文字幕在线 | 爱情岛亚洲论坛入口 | eeuss日韩| 鬼灭之刃柱训练篇在线观看 | 俺也去在线视频 | 一二三区中文字幕 | 色七七桃花综合影院 | 午夜视频黄色 | 制服下的诱惑暮生 | 日日操天天 | 亚洲一区二区三区四区五区xx | 国产良妇出轨视频在线观看 | 深夜久久久 | www.色多多| 国产美女在线播放 | 91片黄在线观看 | 岛国av不卡 | 极品一区 | 喷潮在线 | 欧美日韩在线视频一区 | 中文字幕一区二区久久人妻网站 | 懂色av一区二区夜夜嗨 | 98久久 | 久久久噜噜噜 | 爱草在线 | xxx性欧美 | 日韩成人高清在线 | 我和我的太阳泰剧在线观看泰剧 |