public final class QueuableCachedPool extends ThreadPoolExecutor
From Tomcat 8.5.6, 传统的FixedThreadPool有Queue但线程数量不变,而CachedThreadPool线程数可变但没有Queue
Tomcat的线程池,通过控制TaskQueue,线程数,但线程数到达最大时会进入Queue中.
代码从Tomcat复制,主要修改包括:
1. 删除定期重启线程避免内存泄漏的功能,
2. TaskQueue中可能3次有锁的读取线程数量,改为只读取1次,这把锁也是这个实现里的唯一遗憾了。
https://github.com/apache/tomcat/blob/trunk/java/org/apache/tomcat/util/threads/ThreadPoolExecutor.java
| 限定符和类型 | 类和说明 |
|---|---|
protected static class |
QueuableCachedPool.ControllableQueue
https://github.com/apache/tomcat/blob/trunk/java/org/apache/tomcat/util/threads/TaskQueue.java
|
ThreadPoolExecutor.AbortPolicy, ThreadPoolExecutor.CallerRunsPolicy, ThreadPoolExecutor.DiscardOldestPolicy, ThreadPoolExecutor.DiscardPolicy| 构造器和说明 |
|---|
QueuableCachedPool(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
QueuableCachedPool.ControllableQueue workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) |
| 限定符和类型 | 方法和说明 |
|---|---|
protected void |
afterExecute(Runnable r,
Throwable t) |
void |
execute(Runnable command) |
void |
execute(Runnable command,
long timeout,
TimeUnit unit)
Executes the given command at some time in the future.
|
int |
getSubmittedCount() |
allowCoreThreadTimeOut, allowsCoreThreadTimeOut, awaitTermination, beforeExecute, 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, toStringinvokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor, submit, submit, submitpublic QueuableCachedPool(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
QueuableCachedPool.ControllableQueue workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
protected void afterExecute(Runnable r, Throwable t)
afterExecute 在类中 ThreadPoolExecutorpublic int getSubmittedCount()
public void execute(Runnable command)
execute 在接口中 Executorexecute 在类中 ThreadPoolExecutorpublic void execute(Runnable command, long timeout, TimeUnit unit)
command - the runnable tasktimeout - A timeout for the completion of the taskunit - The timeout time unitRejectedExecutionException - if this task cannot be accepted for execution - the queue is fullNullPointerException - if command or unit is nullCopyright © 2019. All rights reserved.