ThreadPoolExecutor是JDK中的線程池實(shí)現(xiàn),這個(gè)類實(shí)現(xiàn)了一個(gè)線程池需要的各個(gè)方法,它提供了任務(wù)提交、線程管理、監(jiān)控等方法。
下面是ThreadPoolExecutor類的構(gòu)造方法源碼,其他創(chuàng)建線程池的方法最終都會(huì)導(dǎo)向這個(gè)構(gòu)造方法,共有7個(gè)參數(shù):corePoolSize、maximumPoolSize、keepAliveTime、unit、workQueue、threadFactory、handler。
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.acc = System.getSecurityManager() == null ?
null :
AccessController.getContext();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
這些參數(shù)都通過(guò)volatile修飾:
public class ThreadPoolExecutor extends AbstractExecutorService {
private final BlockingQueue<Runnable> workQueue;
private volatile ThreadFactory threadFactory;
private volatile RejectedExecutionHandler handler;
private volatile long keepAliveTime;
// 是否允許核心線程被回收
private volatile boolean allowCoreThreadTimeOut;
private volatile int corePoolSize;
private volatile int maximumPoolSize;
}
corePoolSize:核心線程數(shù)
線程池維護(hù)的最小線程數(shù)量,核心線程創(chuàng)建后不會(huì)被回收(注意:設(shè)置allowCoreThreadTimeout=true后,空閑的核心線程超過(guò)存活時(shí)間也會(huì)被回收)。
大于核心線程數(shù)的線程,在空閑時(shí)間超過(guò)keepAliveTime后會(huì)被回收。
線程池剛創(chuàng)建時(shí),里面沒(méi)有一個(gè)線程,當(dāng)調(diào)用 execute() 方法添加一個(gè)任務(wù)時(shí),如果正在運(yùn)行的線程數(shù)量小于corePoolSize,則馬上創(chuàng)建新線程并運(yùn)行這個(gè)任務(wù)。
maximumPoolSize:最大線程數(shù)
線程池允許創(chuàng)建的最大線程數(shù)量。
當(dāng)添加一個(gè)任務(wù)時(shí),核心線程數(shù)已滿,線程池還沒(méi)達(dá)到最大線程數(shù),并且沒(méi)有空閑線程,工作隊(duì)列已滿的情況下,創(chuàng)建一個(gè)新線程,然后從工作隊(duì)列的頭部取出一個(gè)任務(wù)交由新線程來(lái)處理,而將剛提交的任務(wù)放入工作隊(duì)列尾部。
keepAliveTime:空閑線程存活時(shí)間
當(dāng)一個(gè)可被回收的線程的空閑時(shí)間大于keepAliveTime,就會(huì)被回收。
可被回收的線程:
設(shè)置allowCoreThreadTimeout=true的核心線程。大于核心線程數(shù)的線程(非核心線程)。
unit:時(shí)間單位
keepAliveTime的時(shí)間單位:
TimeUnit.NANOSECONDS TimeUnit.MICROSECONDS TimeUnit.MILLISECONDS // 毫秒 TimeUnit.SECONDS TimeUnit.MINUTES TimeUnit.HOURS TimeUnit.DAYS
workQueue:工作隊(duì)列
新任務(wù)被提交后,會(huì)先添加到工作隊(duì)列,任務(wù)調(diào)度時(shí)再?gòu)年?duì)列中取出任務(wù)。工作隊(duì)列實(shí)現(xiàn)了BlockingQueue接口。
JDK默認(rèn)的工作隊(duì)列有五種:
1.ArrayBlockingQueue 數(shù)組型阻塞隊(duì)列:數(shù)組結(jié)構(gòu),初始化時(shí)傳入大小,有界,F(xiàn)IFO,使用一個(gè)重入鎖,默認(rèn)使用非公平鎖,入隊(duì)和出隊(duì)共用一個(gè)鎖,互斥。
2。LinkedBlockingQueue 鏈表型阻塞隊(duì)列:鏈表結(jié)構(gòu),默認(rèn)初始化大小為Integer.MAX_VALUE,有界(近似無(wú)解),F(xiàn)IFO,使用兩個(gè)重入鎖分別控制元素的入隊(duì)和出隊(duì),用Condition進(jìn)行線程間的喚醒和等待。
3.SynchronousQueue 同步隊(duì)列:容量為0,添加任務(wù)必須等待取出任務(wù),這個(gè)隊(duì)列相當(dāng)于通道,不存儲(chǔ)元素。
4.PriorityBlockingQueue 優(yōu)先阻塞隊(duì)列:無(wú)界,默認(rèn)采用元素自然順序升序排列。
5.DelayQueue 延時(shí)隊(duì)列:無(wú)界,元素有過(guò)期時(shí)間,過(guò)期的元素才能被取出。
threadFactory:線程工廠
創(chuàng)建線程的工廠,可以設(shè)定線程名、線程編號(hào)等。
默認(rèn)線程工廠:
/**
* The default thread factory
*/
static class DefaultThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
DefaultThreadFactory() {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
namePrefix = "pool-" +
poolNumber.getAndIncrement() +
"-thread-";
}
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
if (t.isDaemon())
t.setDaemon(false);
if (t.getPriority() != Thread.NORM_PRIORITY)
t.setPriority(Thread.NORM_PRIORITY);
return t;
}
}
handler:拒絕策略
當(dāng)線程池線程數(shù)已滿,并且工作隊(duì)列達(dá)到限制,新提交的任務(wù)使用拒絕策略處理??梢宰远x拒絕策略,拒絕策略需要實(shí)現(xiàn)RejectedExecutionHandler接口。
JDK默認(rèn)的拒絕策略有四種:
1.AbortPolicy:丟棄任務(wù)并拋出RejectedExecutionException異常。
2.DiscardPolicy:丟棄任務(wù),但是不拋出異常??赡軐?dǎo)致無(wú)法發(fā)現(xiàn)系統(tǒng)的異常狀態(tài)。
3.DiscardOldestPolicy:丟棄隊(duì)列最前面的任務(wù),然后重新提交被拒絕的任務(wù)。
4.CallerRunsPolicy:由調(diào)用線程處理該任務(wù)。
默認(rèn)拒絕策略:
/**
* The default rejected execution handler
*/
private static final RejectedExecutionHandler defaultHandler = new AbortPolicy();
public static class AbortPolicy implements RejectedExecutionHandler {
/**
* Creates an {@code AbortPolicy}.
*/
public AbortPolicy() { }
/**
* Always throws RejectedExecutionException.
*
* @param r the runnable task requested to be executed
* @param e the executor attempting to execute this task
* @throws RejectedExecutionException always
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
throw new RejectedExecutionException("Task " + r.toString() +
" rejected from " +
e.toString());
}
}
自定義線程池工具
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 線程池工廠工具
*
* @author 向振華
* @date 2021/04/11 10:24
*/
public class ThreadPoolFactory {
/**
* 生成固定大小的線程池
*
* @param threadName 線程名稱
* @return 線程池
*/
public static ExecutorService createFixedThreadPool(String threadName) {
AtomicInteger threadNumber = new AtomicInteger(0);
return new ThreadPoolExecutor(
// 核心線程數(shù)
desiredThreadNum(),
// 最大線程數(shù)
desiredThreadNum() * 2,
// 空閑線程存活時(shí)間
60L,
// 空閑線程存活時(shí)間單位
TimeUnit.SECONDS,
// 工作隊(duì)列
new ArrayBlockingQueue<>(1024),
// 線程工廠
new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
return new Thread(r, threadName + "-" + threadNumber.getAndIncrement());
}
},
// 拒絕策略
new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
if (!executor.isShutdown()) {
try {
//嘗試阻塞式加入任務(wù)隊(duì)列
executor.getQueue().put(r);
} catch (Exception e) {
//保持線程的中斷狀態(tài)
Thread.currentThread().interrupt();
}
}
}
});
}
/**
* 理想的線程數(shù),使用 2倍cpu核心數(shù)
*/
public static int desiredThreadNum() {
return Runtime.getRuntime().availableProcessors() * 2;
}
}
到此這篇關(guān)于 Java 多線程中的線程池七個(gè)參數(shù)的全部?jī)?nèi)容就介紹結(jié)束了,想要了解更多相關(guān) Java 線程池的其他內(nèi)容請(qǐng)搜索W3Cschool以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持我們!