Tomcat的:
tomcat.apache.org/tomcat-9.0-…
| name | 描述 | |
|---|---|---|
| maxThreads: | 最大并发数,默认设置 200,一般建议在 500 ~ 800,根据硬件设施和业务来判断 | The maximum number of request processing threads to be created by this Connector, which therefore determines the maximum number of simultaneous requests that can be handled. If not specified, this attribute is set to 200. If an executor is associated with this connector, this attribute is ignored as the connector will execute tasks using the executor rather than an internal thread pool. Note that if an executor is configured any value set for this attribute will be recorded correctly but it will be reported (e.g. via JMX) as -1 to make clear that it is not used. |
| minSpareThreads: | Tomcat 初始化时创建的线程数,默认设置 10 | The minimum number of threads always kept running. This includes both active and idle threads. If not specified, the default of 10 is used. If an executor is associated with this connector, this attribute is ignored as the connector will execute tasks using the executor rather than an internal thread pool. Note that if an executor is configured any value set for this attribute will be recorded correctly but it will be reported (e.g. via JMX) as -1 to make clear that it is not used. |
| acceptCount: | 最大的等待队列数,超过则拒绝请求,默认设置 100 | The maximum queue length for incoming connection requests when all possible request processing threads are in use. Any requests received when the queue is full will be refused. The default value is 100. |
Dubbo的:
dubbo的做法是包罗万象的意思,线程池可以有多种的实现方式,并且可以控制哪些任务是通过线程池来处理,哪些任务直接在IO线程上处理
| en | cn | eg:(这个例如是为了便于理解概念) |
|---|---|---|
| If events handing can be executed quickly without sending new request like marking in memory. Events should be handled by I/O thread since it reduces thread dispatching. | 如果事件处理的逻辑能迅速完成,并且不会发起新的 IO 请求,比如只是在内存中记个标识,则直接在 IO 线程上处理更快,因为减少了线程池调度。 | 就好比你的服务是个redis(这样就没有必要使用线程池) |
| If event handling will be executed slowly or needs to send new I/O request like querying from database,events should be handled in thread pool. Otherwise, I/O thread will be blocked and then will be not able to receive requests. | 但如果事件处理逻辑较慢,或者需要发起新的 IO 请求,比如需要查询数据库,则必须派发到线程池,否则 IO 线程阻塞,将导致不能接收其它请求。 | 就好比你的服务是个经典的CURD操作(这个就很有必要使用线程池) |
| If events are handled by I/O thread, and send new I/O requests during the handling like sending a l login request during connect event,it will alert with “Potentially leading to deadlock”, but deadlock will not happen actually. | 如果用 IO 线程处理事件,又在事件处理过程中发起新的 IO 请求,比如在连接事件中发起登录请求,会报“可能引发死锁”异常,但不会真死锁。 | -- |
默认值
| name | 描述 | 默认值 |
|---|---|---|
| DEFAULT_CORE_THREADS | 核心线程数 | 0 |
| DEFAULT_THREADS | 最大线程池大小 | 200 |
| DEFAULT_QUEUES | 线程池等待队列大小 | 0 |
grpc官方默认的:
没有看到相关的文档 直接贴代码吧:private static final ObjectPool<? extends Executor> DEFAULT_EXECUTOR_POOL = SharedResourcePool.forResource(GrpcUtil.SHARED_CHANNEL_EXECUTOR);
/**`` ``* Creates a thread pool that creates new threads as needed, but`` ``* will reuse previously constructed threads when they are`` ``* available, and uses the provided`` ``* ThreadFactory to create new threads when needed.`` ``* @param threadFactory the factory to use when creating new threads`` ``* @return the newly created thread pool`` ``* @throws NullPointerException if threadFactory is null`` ``*/``public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {`` ``return new ThreadPoolExecutor(``0``, Integer.MAX_VALUE,`` ``60L, TimeUnit.SECONDS,`` ``new SynchronousQueue(),`` ``threadFactory);``} |
|---|
xueqiu-spring的:
Executor线程池可以自定义,也可以在使用xueqiu-spring的默认实现
1.自定义是依赖spring的IOC注入实现,要求实例bean的name为grpcExecutor
后续针对这种场景做了针对gRPC线程池的监控:集成监控Executor的线程池参数
example:
是个fixedthreadPool,机器硬件资源(CPU和内存)充足可以一直挂起这多的线程为grpc服务
@Bean``(name = ``"grpcExecutor"``)``@Order``(Ordered.HIGHEST_PRECEDENCE)``public static ExecutorService newFixedThreadPool() {`` ``AtomicInteger executorCounter = ``new AtomicInteger();`` ``ThreadFactory factory = (target) -> {`` ``Thread thread = ``new Thread(target, String.format(``"my-grpc-executor-%d"``, executorCounter.getAndIncrement()));`` ``thread.setDaemon(``true``);`` ``return thread;`` ``};`` ``return new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors() * ``2``, Runtime.getRuntime().availableProcessors() * ``2``,`` ``0L, TimeUnit.MILLISECONDS,`` ``new LinkedBlockingQueue(), factory);``} |
|---|
各种线程池对应的场景
| threadPool | 场景 |
|---|---|
| FixedThreadPool | 可用于Web服务瞬时削峰,但需注意长时间持续高峰情况造成的队列阻塞。 |
| CachedThreadPool | 快速处理大量耗时较短的任务,如Netty的NIO接受请求时,可使用CachedThreadPool。 |
| 自定义 | 使用者可以根据自己的场景定制,好比拒绝策略(xueqiu-spring的拒绝策略就是告警日志,没有抛出异常) |
2.xueqiu-spring默认实现
public static final String DEFAULT_REGION = ``"default"``;``public static final int DEFAULT_TIMEOUT = ``5``;``public static final int DEFAULT_INFLIGHT = ``0``;``public static final int DEFAULT_THREADS_MAX = ``200``;``public static final int DEFAULT_THREADS_CORE = Runtime.getRuntime().availableProcessors() * ``2``;``public static final int DEFAULT_PORT = ``6565``;``public static final int DEFAULT_MAX_INBOUND_MESSAGE_SIZE = ``1024 * ``1024 * ``4``;``……``private static Executor createGrpcExecutor(GRpcServerProperties properties) {`` ``// GRPC自带的线程池是CacheThreadPool 可以适当调整下`` ``RejectedExecutionHandler handler = (target, executor) -> {`` ``LOGGER.warn(``"can't handle request: {} in executor: {}"``, target, executor);`` ``};`` ``AtomicInteger executorCounter = ``new AtomicInteger();`` ``ThreadFactory factory = (target) -> {`` ``Thread thread = ``new Thread(target, String.format(``"xueqiu-grpc-executor-%d"``, executorCounter.getAndIncrement()));`` ``thread.setDaemon(``true``);`` ``return thread;`` ``};`` ``ArrayBlockingQueue inflight = ``new ArrayBlockingQueue<>(properties.getInflight(), ``false``);`` ``Executor executor = ``new ThreadPoolExecutor(properties.getThreadsCore(), properties.getThreadsMax(), ``5``, TimeUnit.MINUTES, inflight, factory, handler);`` ``return executor;``} |
|---|
默认参数配置:
| name | 描述 | 默认值 |
|---|---|---|
| ``` | ||
| DEFAULT_THREADS_CORE |
| ```
DEFAULT_THREADS_MAX
``` | 默认最大线程数 | 200 |
| ```
DEFAULT_INFLIGHT
``` | 默认线程等待队列大小 | 0 |