Package org.shoulder.monitor.concurrent
Class MonitorableThreadPool
java.lang.Object
java.util.concurrent.AbstractExecutorService
java.util.concurrent.ThreadPoolExecutor
org.shoulder.monitor.concurrent.MonitorableThreadPool
- All Implemented Interfaces:
AutoCloseable,Executor,ExecutorService
带指标可监控的线程池,推荐需要稳定执行、重要的业务使用,以更好的掌握系统运行状态
- Author:
- lym
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.concurrent.ThreadPoolExecutor
ThreadPoolExecutor.AbortPolicy, ThreadPoolExecutor.CallerRunsPolicy, ThreadPoolExecutor.DiscardOldestPolicy, ThreadPoolExecutor.DiscardPolicy -
Constructor Summary
ConstructorsConstructorDescriptionMonitorableThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, String poolName) 调用父类的构造方法,并初始化HashMap和线程池名称MonitorableThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler, String poolName) 调用父类的构造方法,并初始化HashMap和线程池名称MonitorableThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, String poolName) 调用父类的构造方法,并初始化HashMap和线程池名称MonitorableThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler, String poolName) 调用父类的构造方法,并初始化HashMap和线程池名称 -
Method Summary
Modifier and TypeMethodDescriptionprotected voidafterExecute(Runnable r, Throwable t) protected voidbeforeExecute(Thread t, Runnable r) Getter method for property metrics.获取线程池名称voidsetCorePoolSize(int newCorePoolSize) 修改核心线程数voidsetMaximumPoolSize(int newMaximumPoolSize) 修改最大线程数voidshutdown()线程池延迟关闭时(不再接收、执行新任务,等待线程池里的任务都执行完毕)立即关闭时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, setKeepAliveTime, setRejectedExecutionHandler, setThreadFactory, 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
-
MonitorableThreadPool
public MonitorableThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, String poolName) 调用父类的构造方法,并初始化HashMap和线程池名称- Parameters:
corePoolSize- 线程池核心线程数maximumPoolSize- 线程池最大线程数keepAliveTime- 线程的最大空闲时间unit- 空闲时间的单位workQueue- 保存被提交任务的队列poolName- 线程池名称
-
MonitorableThreadPool
public MonitorableThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, String poolName) 调用父类的构造方法,并初始化HashMap和线程池名称- Parameters:
corePoolSize- 线程池核心线程数maximumPoolSize- 线程池最大线程数keepAliveTime- 线程的最大空闲时间unit- 空闲时间的单位workQueue- 保存被提交任务的队列poolName- 线程池名称
-
MonitorableThreadPool
public MonitorableThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler, String poolName) 调用父类的构造方法,并初始化HashMap和线程池名称- Parameters:
corePoolSize- 线程池核心线程数maximumPoolSize- 线程池最大线程数keepAliveTime- 线程的最大空闲时间unit- 空闲时间的单位workQueue- 保存被提交任务的队列poolName- 线程池名称
-
MonitorableThreadPool
public MonitorableThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler, String poolName) 调用父类的构造方法,并初始化HashMap和线程池名称- Parameters:
corePoolSize- 线程池核心线程数maximumPoolSize- 线程池最大线程数keepAliveTime- 线程的最大空闲时间unit- 空闲时间的单位workQueue- 保存被提交任务的队列threadFactory- 线程工厂poolName- 线程池名称
-
-
Method Details
-
getMetrics
Getter method for property metrics.- Returns:
- property value of metrics
-
beforeExecute
- Overrides:
beforeExecutein classThreadPoolExecutor
-
afterExecute
- Overrides:
afterExecutein classThreadPoolExecutor
-
shutdownNow
立即关闭时- Specified by:
shutdownNowin interfaceExecutorService- Overrides:
shutdownNowin classThreadPoolExecutor- Returns:
- 没有执行的任务
-
shutdown
public void shutdown()线程池延迟关闭时(不再接收、执行新任务,等待线程池里的任务都执行完毕)- Specified by:
shutdownin interfaceExecutorService- Overrides:
shutdownin classThreadPoolExecutor
-
setCorePoolSize
public void setCorePoolSize(int newCorePoolSize) 修改核心线程数- Overrides:
setCorePoolSizein classThreadPoolExecutor- Parameters:
newCorePoolSize- [0 , maximumPoolSize]
-
setMaximumPoolSize
public void setMaximumPoolSize(int newMaximumPoolSize) 修改最大线程数- Overrides:
setMaximumPoolSizein classThreadPoolExecutor- Parameters:
newMaximumPoolSize- corePoolSize 小于等于 maximumPoolSize 且 maximumPoolSize 大于 0
-
getPoolName
获取线程池名称- Returns:
- 线程池名称
-