Package org.shoulder.monitor.concurrent
Class AutoScaleThreadPool
java.lang.Object
java.util.concurrent.AbstractExecutorService
java.util.concurrent.ThreadPoolExecutor
org.shoulder.monitor.concurrent.AutoScaleThreadPool
- All Implemented Interfaces:
AutoCloseable,Executor,ExecutorService
可自动扩容、缩容核心线程数的线程池,(可更合理的利用线程资源、应对突发事件处理)适合平时节能模式处理,突然紧急情况下提前加速处理的场景
JDK 中实现的当且仅当任务队列满了时才会创建新线程,但如果这时候突发来多个任务,则导致任务很可能被拒绝 实际中应根据队列容量自动扩容线程数,如,当队列中任务数达到上限的 70%、80%、90%,则自动扩容线程,而不是满了之后才扩容,缓解大压力场景下触发
默认参数举例: - 执行前,若队列中任务数大于 75% 队列容量 且 threadSize 小于 maxSize,将线程数加一,默认冷却时间 5s - 执行后,若队列中任务数小于25% 队列容量 且 threadSize 小于 originCoreSize,将 core 线程数减少一,默认冷却时间 5s
动态设置参数实现: 对接配置中心 监控、告警实现: 对接 prometheus,过载告警 操作记录与审计: 对接日志中心,变更通知 https://tech.meituan.com/2020/04/02/java-pooling-pratice-in-meituan.html
- Author:
- lym
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class自动扩容、缩容规则 该类职责完全服务于外部类,故置为内部类Nested classes/interfaces inherited from class java.util.concurrent.ThreadPoolExecutor
ThreadPoolExecutor.AbortPolicy, ThreadPoolExecutor.CallerRunsPolicy, ThreadPoolExecutor.DiscardOldestPolicy, ThreadPoolExecutor.DiscardPolicy -
Constructor Summary
ConstructorsConstructorDescriptionAutoScaleThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler, AutoScaleThreadPool.ScaleRule scaleRule) 调用父类的构造方法,并初始化HashMap和线程池名称AutoScaleThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler, AutoScaleThreadPool.ScaleRule scaleRule) 调用父类的构造方法,并初始化HashMap和线程池名称AutoScaleThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, AutoScaleThreadPool.ScaleRule scaleRule) 调用父类的构造方法,并初始化HashMap和线程池名称AutoScaleThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, AutoScaleThreadPool.ScaleRule scaleRule) 调用父类的构造方法,并初始化HashMap和线程池名称 -
Method Summary
Modifier and TypeMethodDescriptionprotected voidafterExecute(Runnable r, Throwable t) protected voidbeforeExecute(Thread t, Runnable r) Methods inherited from class java.util.concurrent.ThreadPoolExecutor
allowCoreThreadTimeOut, allowsCoreThreadTimeOut, awaitTermination, execute, finalize, getActiveCount, getCompletedTaskCount, getCorePoolSize, getKeepAliveTime, getLargestPoolSize, getMaximumPoolSize, getPoolSize, getQueue, getRejectedExecutionHandler, getTaskCount, getThreadFactory, isShutdown, isTerminated, isTerminating, prestartAllCoreThreads, prestartCoreThread, purge, remove, setCorePoolSize, setKeepAliveTime, setMaximumPoolSize, setRejectedExecutionHandler, setThreadFactory, shutdown, shutdownNow, terminated, toStringMethods inherited from class java.util.concurrent.AbstractExecutorService
invokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor, submit, submit, submitMethods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.concurrent.ExecutorService
close
-
Constructor Details
-
AutoScaleThreadPool
public AutoScaleThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, AutoScaleThreadPool.ScaleRule scaleRule) 调用父类的构造方法,并初始化HashMap和线程池名称- Parameters:
corePoolSize- 线程池核心线程数maximumPoolSize- 线程池最大线程数keepAliveTime- 线程的最大空闲时间unit- 空闲时间的单位workQueue- 保存被提交任务的队列
-
AutoScaleThreadPool
public AutoScaleThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, AutoScaleThreadPool.ScaleRule scaleRule) 调用父类的构造方法,并初始化HashMap和线程池名称- Parameters:
corePoolSize- 线程池核心线程数maximumPoolSize- 线程池最大线程数keepAliveTime- 线程的最大空闲时间unit- 空闲时间的单位workQueue- 保存被提交任务的队列
-
AutoScaleThreadPool
public AutoScaleThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler, AutoScaleThreadPool.ScaleRule scaleRule) 调用父类的构造方法,并初始化HashMap和线程池名称- Parameters:
corePoolSize- 线程池核心线程数maximumPoolSize- 线程池最大线程数keepAliveTime- 线程的最大空闲时间unit- 空闲时间的单位workQueue- 保存被提交任务的队列
-
AutoScaleThreadPool
public AutoScaleThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler, AutoScaleThreadPool.ScaleRule scaleRule) 调用父类的构造方法,并初始化HashMap和线程池名称- Parameters:
corePoolSize- 线程池核心线程数maximumPoolSize- 线程池最大线程数keepAliveTime- 线程的最大空闲时间unit- 空闲时间的单位workQueue- 保存被提交任务的队列threadFactory- 线程工厂
-
-
Method Details
-
beforeExecute
- Overrides:
beforeExecutein classThreadPoolExecutor
-
afterExecute
- Overrides:
afterExecutein classThreadPoolExecutor
-