import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Created by yehan .
*? 代碼和思路主要來自于:
*
* tomcat :
*? ? ? ? org.apache.catalina.core.StandardThreadExecutor
*
* java.util.concurrent
* threadPoolExecutor execute執(zhí)行策略:? ? ? ? 優(yōu)先offer到queue蜒程,queue滿后再擴充線程到maxThread相寇,如果已經(jīng)到了maxThread就reject
*? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 比較適合于CPU密集型應用(比如runnable內(nèi)部執(zhí)行的操作都在JVM內(nèi)部因俐,memory copy, or compute等等)
*
* StandardThreadExecutor execute執(zhí)行策略: 優(yōu)先擴充線程到maxThread候齿,再offer到queue竹观,如果滿了就reject
*? ? ? ? ? ? ? ? ? ? ? ? ? 比較適合于業(yè)務處理需要遠程資源的場景
*
* @author yehan
*
*/
public class StandardThreadExecutorextends ThreadPoolExecutor {
public static final int DEFAULT_MIN_THREADS =20;
? ? public static final int DEFAULT_MAX_THREADS =200;
? ? /**1 minutes*/
? ? public static final int DEFAULT_MAX_IDLE_TIME =60 *1000;
? ? /**正在處理的任務數(shù)*/
? ? protected AtomicIntegersubmittedTasksCount;
? ? /**最大允許同時處理的任務數(shù)*/
? ? private int maxSubmittedTaskCount;
? ? public StandardThreadExecutor() {
this(DEFAULT_MIN_THREADS, DEFAULT_MAX_THREADS);
? ? }
public StandardThreadExecutor(int coreThread, int maxThreads) {
this(coreThread, maxThreads, maxThreads);
? ? }
public StandardThreadExecutor(int coreThread, int maxThreads, long keepAliveTime, TimeUnit unit) {
this(coreThread, maxThreads, keepAliveTime, unit, maxThreads);
? ? }
public StandardThreadExecutor(int coreThreads, int maxThreads, int queueCapacity) {
this(coreThreads, maxThreads, queueCapacity, Executors.defaultThreadFactory());
? ? }
public StandardThreadExecutor(int coreThreads, int maxThreads, int queueCapacity, ThreadFactory threadFactory) {
this(coreThreads, maxThreads, DEFAULT_MAX_IDLE_TIME, TimeUnit.MILLISECONDS, queueCapacity, threadFactory);
? ? }
public StandardThreadExecutor(int coreThreads, int maxThreads, long keepAliveTime, TimeUnit unit, int queueCapacity) {
this(coreThreads, maxThreads, keepAliveTime, unit, queueCapacity, Executors.defaultThreadFactory());
? ? }
public StandardThreadExecutor(int coreThreads, int maxThreads, long keepAliveTime, TimeUnit unit,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int queueCapacity, ThreadFactory threadFactory) {
this(coreThreads, maxThreads, keepAliveTime, unit, queueCapacity, threadFactory, new AbortPolicy());
? ? }
public StandardThreadExecutor(int coreThreads, int maxThreads, long keepAliveTime, TimeUnit unit,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int queueCapacity, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
super(coreThreads, maxThreads, keepAliveTime, unit, new ExecutorQueue(), threadFactory, handler);
? ? ? ? ((ExecutorQueue) getQueue()).setStandardThreadExecutor(this);
? ? ? ? submittedTasksCount =new AtomicInteger(0);
? ? ? ? // 最大并發(fā)任務限制: 隊列buffer數(shù) + 最大線程數(shù)
? ? ? ? maxSubmittedTaskCount = queueCapacity + maxThreads;
? ? }
@Override
? ? public void execute(Runnable command) {
int count =submittedTasksCount.incrementAndGet();
? ? ? ? // 超過最大的并發(fā)任務限制,進行 reject
// 依賴的LinkedTransferQueue沒有長度限制,因此這里進行控制
? ? ? ? if (count >maxSubmittedTaskCount) {
submittedTasksCount.decrementAndGet();
? ? ? ? ? ? getRejectedExecutionHandler().rejectedExecution(command, this);
? ? ? ? }
try {
super.execute(command);
? ? ? ? }catch (RejectedExecutionException rx) {
// there could have been contention around the queue
? ? ? ? ? ? if (!((ExecutorQueue) getQueue()).force(command)) {
submittedTasksCount.decrementAndGet();
? ? ? ? ? ? ? ? getRejectedExecutionHandler().rejectedExecution(command, this);
? ? ? ? ? ? }
}
}
public int getSubmittedTasksCount() {
return this.submittedTasksCount.get();
? ? }
public int getMaxSubmittedTaskCount() {
return maxSubmittedTaskCount;
? ? }
@Override
? ? protected void afterExecute(Runnable r, Throwable t) {
submittedTasksCount.decrementAndGet();
? ? }
}
/**
* LinkedTransferQueue 能保證更高性能晒哄,相比與LinkedBlockingQueue有明顯提升
*
*
*? ? ? ? 1) 不過LinkedTransferQueue的缺點是沒有隊列長度控制购啄,需要在外層協(xié)助控制
*
*
* @author yehan
*
*/
class ExecutorQueueextends LinkedTransferQueue {
private static final long serialVersionUID = -265236426751004839L;
? ? StandardThreadExecutorthreadPoolExecutor;
? ? public ExecutorQueue() {
super();
? ? }
public void setStandardThreadExecutor(StandardThreadExecutor threadPoolExecutor) {
this.threadPoolExecutor = threadPoolExecutor;
? ? }
/** 注:代碼來源于 tomcat*/
? ? public boolean force(Runnable o) {
if (threadPoolExecutor.isShutdown()) {
throw new RejectedExecutionException("Executor not running, can't force a command into the queue");
? ? ? ? }
// forces the item onto the queue, to be used if the task is rejected
? ? ? ? return super.offer(o);
? ? }
/**
*注:tomcat的代碼進行一些小變更
*
*/
? ? @Override
? ? public boolean offer(Runnable o) {
int poolSize =threadPoolExecutor.getPoolSize();
? ? ? ? // we are maxed out on threads, simply queue the object
? ? ? ? if (poolSize ==threadPoolExecutor.getMaximumPoolSize()) {
return super.offer(o);
? ? ? ? }
// we have idle threads, just add it to the queue
// note that we don't use getActiveCount(), see BZ 49730
? ? ? ? if (threadPoolExecutor.getSubmittedTasksCount() <= poolSize) {
return super.offer(o);
? ? ? ? }
// if we have less threads than maximum force creation of a new
// thread
? ? ? ? if (poolSize
return false;
? ? ? ? }
// if we reached here, we need to add it to the queue
? ? ? ? return super.offer(o);
? ? }
}
重寫并優(yōu)化 ThreadPoolExecutor 線程池
最后編輯于 :
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門木羹,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人解孙,你說我怎么就攤上這事坑填。” “怎么了弛姜?”我有些...
- 文/不壞的土叔 我叫張陵脐瑰,是天一觀的道長。 經(jīng)常有香客問我娱据,道長蚪黑,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任中剩,我火速辦了婚禮忌穿,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘结啼。我一直安慰自己掠剑,他們只是感情好,可當我...
- 文/花漫 我一把揭開白布郊愧。 她就那樣靜靜地躺著朴译,像睡著了一般。 火紅的嫁衣襯著肌膚如雪属铁。 梳的紋絲不亂的頭發(fā)上眠寿,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼奢浑!你這毒婦竟也來了蛮艰?” 一聲冷哼從身側(cè)響起,我...
- 正文 年R本政府宣布,位于F島的核電站源内,受9級特大地震影響葡粒,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜膜钓,卻給世界環(huán)境...
- 文/蒙蒙 一嗽交、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧颂斜,春花似錦夫壁、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至司蔬,卻和暖如春邑茄,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背俊啼。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 原文出處http://cmsblogs.com/ 『chenssy』 作為Executor框架中最核心的類黔寇,Thr...
- 前言 JDK中為我們提供了一個并發(fā)線程框架,它是的我們可以在有異步任務或大量并發(fā)任務需要執(zhí)行時可以使用它提供的線程...