java new thread参数_java线程池01-ThreadPoolExecutor构造方法参数的使用规则
為了更好的使用多線程,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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: datagrip mysql乱码_Dat
- 下一篇: java volatile线程可见_vo