OpenClawgemma-3-12b-it低成本方案自建接口替代SaaS服务1. 为什么需要本地化模型方案去年我在用OpenClaw自动化处理公司内部文档时遇到了一个尴尬问题连续运行两周后OpenClaw调用商业API的账单竟然超过了团队月度云计算预算的60%。这促使我开始寻找更经济的替代方案。经过多次测试我发现gemma-3-12b-it这个120亿参数的中等规模模型在OpenClaw的自动化任务场景中展现出惊人的性价比。与商业API相比自建服务不仅将Token成本降低了一半以上更重要的是实现了数据的完全本地化处理——这对我们处理敏感客户数据的金融团队来说简直是救命稻草。2. gemma-3-12b-it的技术特性2.1 模型架构优势gemma-3-12b-it作为第三代Gemma模型的指令调优版本在保持12B参数量级的同时通过以下改进显著提升了OpenClaw场景下的实用性长上下文优化支持32K tokens的上下文窗口足以覆盖OpenClaw多数自动化任务的需求链工具调用增强对点击/输入/截图等操作指令的理解准确率比基础版提升27% -# 1. 概述本文分享Dubbo 的线程池策略。在 《Dubbo 用户指南 —— 线程模型》 文档对 Dubbo 的线程池策略说明如下如果事件处理的逻辑能迅速完成并且不会发起新的 IO 请求比如只是在内存中记个标识则直接在 IO 线程上处理更快因为减少了线程池调度。但如果事件处理逻辑较慢或者需要发起新的 IO 请求比如需要查询数据库则必须派发到线程池否则 IO 线程阻塞将导致不能接收其它请求。如果用 IO 线程处理事件又在事件处理过程中发起新的 IO 请求比如在连接事件中发起登录请求会报“可能引发死锁”异常但不会真死锁。因此需要通过不同的派发策略和不同的线程池配置的组合来应对不同的场景:dispatcher |-- all 所有消息都派发到线程池包括请求响应连接事件断开事件心跳等。 |-- direct 所有消息都不派发到线程池全部在 IO 线程上直接执行。 |-- message 只有请求响应消息派发到线程池其它连接断开事件心跳等消息直接在 IO 线程上执行。 |-- execution 只有请求消息派发到线程池不含响应响应和其它连接断开事件心跳等消息直接在 IO 线程上执行。 |-- connection 在 IO 线程上将连接断开事件放入队列有序逐个执行其它消息派发到线程池。目前 Dubbo 提供五种Dispatcher 默认为all。在 《Dubbo 用户指南 —— 性能调优》 文档推荐 Provider 配置dispatchermessage和threadpoolfixed并设置threads为具体数值。在 《Dubbo 用户指南 —— 线程模型》 文档推荐线程池配置如下fixed固定大小线程池启动时建立线程不关闭一直持有。(缺省)cached缓存线程池空闲一分钟自动删除需要时重建。limited可伸缩线程池但池中的线程数只会增长不会收缩。只增长不收缩的目的是为了避免收缩时突然来了大流量引起的性能问题。eager优先创建Worker线程池。在任务数量大于corePoolSize但是小于maximumPoolSize时优先创建Worker来处理任务。当任务数量大于maximumPoolSize时将任务放入阻塞队列中。阻塞队列充满时抛出RejectedExecutionException。(相比于cached:cached在任务数量超过maximumPoolSize时直接抛出异常而不是将任务放入阻塞队列)本文涉及的类如下图所示2. Dispatchercom.alibaba.dubbo.common.threadpool.support.Dispatcher线程分发器接口。代码如下SPI(AllDispatcher.NAME) public interface Dispatcher { /** * dispatch the message to threadpool. * * 派发消息到线程池 * * param handler 处理器 * param url url * return channel handler */ Adaptive({Constants.DISPATCHER_KEY, dispather, channel.handler}) // The last two parameters are reserved for compatibility with the old configuration ChannelHandler dispatch(ChannelHandler handler, URL url); }SPI(AllDispatcher.NAME)注解Dubbo SPI拓展点默认为all。Adaptive注解基于 Dubbo SPI Adaptive 机制加载对应的线程分发器实现使用URL.dispatcher属性。#dispatch(handler, url)方法修饰handler例如将handler包装成派发到线程池的 ChannelHandler实现类。2.1 AllDispatchercom.alibaba.dubbo.common.threadpool.support.all.AllDispatcher实现 Dispatcher 接口所有消息都派发到线程池包括请求响应连接事件断开事件心跳等。代码如下public class AllDispatcher implements Dispatcher { public static final String NAME all; Override public ChannelHandler dispatch(ChannelHandler handler, URL url) { return new AllChannelHandler(handler, url); } }2.2 DirectDispatchercom.alibaba.dubbo.common.threadpool.support.direct.DirectDispatcher实现 Dispatcher 接口所有消息都不派发到线程池全部在 IO 线程上直接执行。代码如下public class DirectDispatcher implements Dispatcher { public static final String NAME direct; Override public ChannelHandler dispatch(ChannelHandler handler, URL url) { return handler; } }2.3 MessageOnlyDispatchercom.alibaba.dubbo.common.threadpool.support.message.MessageOnlyDispatcher实现 Dispatcher 接口只有请求响应消息派发到线程池其它连接断开事件心跳等消息直接在 IO 线程上执行。代码如下public class MessageOnlyDispatcher implements Dispatcher { public static final String NAME message; Override public ChannelHandler dispatch(ChannelHandler handler, URL url) { return new MessageOnlyChannelHandler(handler, url); } }2.4 ExecutionDispatchercom.alibaba.dubbo.common.threadpool.support.execution.ExecutionDispatcher实现 Dispatcher 接口只有请求消息派发到线程池不含响应响应和其它连接断开事件心跳等消息直接在 IO 线程上执行。代码如下public class ExecutionDispatcher implements Dispatcher { public static final String NAME execution; Override public ChannelHandler dispatch(ChannelHandler handler, URL url) { return new ExecutionChannelHandler(handler, url); } }2.5 ConnectionOrderedDispatchercom.alibaba.dubbo.common.threadpool.support.connection.ConnectionOrderedDispatcher实现 Dispatcher 接口在 IO 线程上将连接断开事件放入队列有序逐个执行其它消息派发到线程池。代码如下public class ConnectionOrderedDispatcher implements Dispatcher { public static final String NAME connection; Override public ChannelHandler dispatch(ChannelHandler handler, URL url) { return new ConnectionOrderedChannelHandler(handler, url); } }3. ChannelHandler在dubbo-remoting-api项目中com.alibaba.dubbo.remoting.transport.ChannelHandlerDelegate实现 ChannelHandler 接口通道处理器代理接口。代码如下public interface ChannelHandlerDelegate extends ChannelHandler { /** * return channel handler */ ChannelHandler getHandler(); }3.1 WrappedChannelHandlercom.alibaba.dubbo.remoting.transport.dispatcher.WrappedChannelHandler实现 ChannelHandlerDelegate 接口通道处理器代理抽象类。3.1.1 构造方法/** * 通道处理器 */ protected final ChannelHandler handler; /** * URL */ protected final URL url; /** * 线程池 */ protected final ExecutorService executor; public WrappedChannelHandler(ChannelHandler handler, URL url) { this.handler handler; this.url url; // 创建线程池 executor (ExecutorService) ExtensionLoader.getExtensionLoader(ThreadPool.class).getAdaptiveExtension().getExecutor(url); // 省略一些代码 }handler属性通道处理器。url属性URL 。executor属性线程池。在构造方法中调用ExtensionLoader#getExecutor(url)方法基于 Dubbo SPI Adaptive 机制获得线程池实现对象。代码如下// ThreadPool$Adaptive#getExecutor(url) public java.util.concurrent.ExecutorService getExecutor(com.alibaba.dubbo.common.URL arg0) { if (arg0 null) { throw new IllegalArgumentException(url null); } com.alibaba.dubbo.common.URL url arg0; // 线程池类型 String extName url.getParameter(Constants.THREADPOOL_KEY, Constants.DEFAULT_THREADPOOL); if (extName null) { throw new IllegalStateException(Fail to get extension(com.alibaba.dubbo.common.threadpool.ThreadPool) name from url( url.toString() ) use keys([threadpool])); } // 加载线程池实现 com.alibaba.dubbo.common.threadpool.ThreadPool extension (com.alibaba.dubbo.common.threadpool.ThreadPool) ExtensionLoader.getExtensionLoader(com.alibaba.dubbo.common.threadpool.ThreadPool.class).getExtension(extName); return extension.getExecutor(arg0); }线程池类型即threadpool配置项默认为fixed。目前有四种线程池实现在文章开头已经介绍。3.1.2 公用方法public void close() { try { if (executor ! null) { executor.shutdown(); } } catch (Throwable t) { logger.warn(fail to destroy thread pool of server: t.getMessage(), t); } } Deprecated public void close(int timeout) { close(); } protected void sendFeedback(Channel channel, Request request, Throwable t) throws RemotingException { // ... 省略代码 }3.2 ChannelEventRunnablecom.alibaba.dubbo.remoting.transport.dispatcher.ChannelEventRunnable实现 Runnable 接口通道事件运行任务。代码如下public class ChannelEventRunnable implements Runnable { /** * 通道处理器 */ private final ChannelHandler handler; /** * 通道 */ private final Channel channel; /** * 通道事件 */ private final ChannelState state; /** * 异常 */ private final Throwable exception; /** * 消息 */ private final Object message; public ChannelEventRunnable(Channel channel, ChannelHandler handler, ChannelState state, Throwable exception) { this(channel, handler, state, exception, null); } public ChannelEventRunnable(Channel channel, ChannelHandler handler, ChannelState state, Object message) { this(channel, handler, state, null, message); } public ChannelEventRunnable(Channel channel, ChannelHandler handler, ChannelState state, Throwable exception, Object message) { this.channel channel; this.handler handler; this.state state; this.exception exception; this.message message; } Override public void run() { // 连接状态 if (state ChannelState.RECEIVED) { try { handler.received(channel, message); } catch (Exception e) { logger.warn(ChannelEventRunnable handle state operation error, channel is channel , message is message, e); } } // 未连接状态 else { switch (state) { case CONNECTED: try { handler.connected(channel); } catch (Exception e) { logger.warn(ChannelEventRunnable handle state operation error, channel is channel, e); } break; case DISCONNECTED: try { handler.disconnected(channel); } catch (Exception e) { logger.warn(ChannelEventRunnable handle state operation error, channel is channel, e); } break; case SENT: try { handler.sent(channel, message); } catch (Exception e) { logger.warn(ChannelEventRunnable handle state operation error, channel is channel , message is message, e); } break; case CAUGHT: try { handler.caught(channel, exception); } catch (Exception e) { logger.warn(ChannelEventRunnable handle state operation error, channel is channel , message is: message , exception is exception, e); } break; default: logger.warn(unknown state: state , message is message); } } } /** * ChannelState */ public enum ChannelState { /** * CONNECTED */ CONNECTED, /** * DISCONNECTED */ DISCONNECTED, /** * SENT */ SENT, /** * RECEIVED */ RECEIVED, /** * CAUGHT */ CAUGHT } }在#run()方法中根据不同的 ChannelState 状态调用不同的 ChannelHandler 的方法处理通道事件。3.3 AbstractChannelHandlerDelegatecom.alibaba.dubbo.remoting.transport.dispatcher.all.AbstractChannelHandlerDelegate实现 ChannelHandlerDelegate 接口通道处理器代理抽象类。代码如下public abstract class AbstractChannelHandlerDelegate implements ChannelHandlerDelegate { protected final ChannelHandler handler; protected AbstractChannelHandlerDelegate(ChannelHandler handler) { Assert.notNull(handler, handler null); this.handler handler; } Override public ChannelHandler getHandler() { if (handler instanceof ChannelHandlerDelegate) { return ((ChannelHandlerDelegate) handler).getHandler(); } return handler; } Override public void connected(Channel channel) throws RemotingException { handler.connected(channel); } Override public void disconnected(Channel channel) throws RemotingException { handler.disconnected(channel); } Override public void sent(Channel channel, Object message) throws RemotingException { handler.sent(channel, message); } Override public void received(Channel channel, Object message) throws RemotingException { handler.received(channel, message); } Override public void caught(Channel channel, Throwable exception) throws RemotingException { handler.caught(channel, exception); } }3.4 AllChannelHandlercom.alibaba.dubbo.remoting.transport.dispatcher.all.AllChannelHandler实现 WrappedChannelHandler 抽象类所有消息都派发到线程池包括请求响应连接事件断开事件心跳等。代码如下public class AllChannelHandler extends WrappedChannelHandler { public AllChannelHandler(ChannelHandler handler, URL url) { super(handler, url); } Override public void connected(Channel channel) throws RemotingException { // 获得线程池 ExecutorService cexecutor getExecutorService(); try { // 提交线程池执行任务 cexecutor.execute(new ChannelEventRunnable(channel, handler, ChannelEventRunnable.ChannelState.CONNECTED)); } catch (Throwable t) { throw new ExecutionException(connect event, channel, getClass() error when process connected event ., t); } } Override public void disconnected(Channel channel) throws RemotingException { ExecutorService cexecutor getExecutorService(); try { cexecutor.execute(new ChannelEventRunnable(channel, handler, ChannelEventRunnable.ChannelState.DISCONNECTED)); } catch (Throwable t) { throw new ExecutionException(disconnect event, channel, getClass() error when process disconnected event ., t); } } Override public void received(Channel channel, Object message) throws RemotingException { ExecutorService cexecutor getExecutorService(); try { cexecutor.execute(new ChannelEventRunnable(channel, handler, ChannelEventRunnable.ChannelState.RECEIVED, message)); } catch (Throwable t) { //TODO A temporary solution to the problem that the exception information can not be sent to the opposite end after the thread pool is full. Need a refactoring //fix The thread pool is full, refuses to call, does not return, and causes the consumer to wait for time out if (message instanceof Request t instanceof RejectedExecutionException) { Request request (Request) message; if (request.isTwoWay()) { String msg Server side( url.getIp() , url.getPort() ) threadpool is exhausted ,detail msg: t.getMessage(); Response response new Response(request.getId(), request.getVersion()); response.setStatus(Response.SERVER_THREADPOOL_EXHAUSTED_ERROR); response.setErrorMessage(msg); channel.send(response); return; } } throw new ExecutionException(message, channel, getClass() error when process received event ., t); } } Override public void caught(Channel channel, Throwable exception) throws RemotingException { ExecutorService cexecutor getExecutorService(); try { cexecutor.execute(new ChannelEventRunnable(channel, handler, ChannelEventRunnable.ChannelState.CAUGHT, exception)); } catch (Throwable t) { throw new ExecutionException(caught event, channel, getClass() error when process caught event ., t); } } }对于每个事件提交到线程池进行执行。以#received(channel, message)方法举例子详细解析第 3 行调用#getExecutorService()方法获得线程池。第 5 行创建 ChannelEventRunnable 对象。第 6 行提交线程池执行任务。第 7 至 21 行发生 RejectedExecutionException 异常并且请求需要响应则返回SERVER_THREADPOOL_EXHAUSTED_ERROR响应。第 22 行发生其它异常抛出 ExecutionException 异常。3.5 MessageOnlyChannelHandlercom.alibaba.dubbo.remoting.transport.dispatcher.message.MessageOnlyChannelHandler实现 WrappedChannelHandler 抽象类只有请求响应消息派发到线程池其它连接断开事件心跳等消息直接在 IO 线程上执行。代码如下public class MessageOnlyChannelHandler extends WrappedChannelHandler { public MessageOnlyChannelHandler(ChannelHandler handler, URL url) { super(handler, url); } Override public void connected(Channel channel) throws RemotingException { handler.connected(channel); } Override public void disconnected(Channel channel) throws RemotingException { handler.disconnected(channel); } Override public void received(Channel channel, Object message) throws RemotingException { ExecutorService cexecutor getExecutorService(); try { cexecutor.execute(new ChannelEventRunnable(channel, handler, ChannelEventRunnable.ChannelState.RECEIVED, message)); } catch (Throwable t) { //TODO 临时解决线程池满后异常信息无法发送到对端的问题。待重构 //fix 线程池满了拒绝调用不返回导致消费者一直等待超时 if (message instanceof Request t instanceof RejectedExecutionException) { Request request (Request) message; if (request.isTwoWay()) { String msg Server side( url.getIp() , url.getPort() ) threadpool is exhausted ,detail msg: t.getMessage(); Response response new Response(request.getId(), request.getVersion()); response.setStatus(Response.SERVER_THREADPOOL_EXHAUSTED_ERROR); response.setErrorMessage(msg); channel.send(response); return; } } throw new ExecutionException(message, channel, getClass() error when process received event ., t); } } Override public void caught(Channel channel, Throwable exception) throws RemotingException { handler.caught(channel, exception); } }和 AllChannelHandler 相比仅#received(channel, message)方法提交到线程池进行执行。3.6 ExecutionChannelHandlercom.alibaba.dubbo.remoting.transport.dispatcher.execution.ExecutionChannelHandler实现 WrappedChannelHandler 抽象类只有请求消息派发到线程池不含响应响应和其它连接断开事件心跳等消息直接在 IO 线程上执行。代码如下public class ExecutionChannelHandler extends WrappedChannelHandler { public ExecutionChannelHandler(ChannelHandler handler, URL url) { super(handler, url); } Override public void connected(Channel channel) throws RemotingException { handler.connected(channel); } Override public void disconnected(Channel channel) throws RemotingException { handler.disconnected(channel); } Override public void received(Channel channel, Object message) throws RemotingException { // 获得线程池 ExecutorService cexecutor getExecutorService(); // 请求消息提交到线程池 if (message instanceof Request) { try { cexecutor.execute(new ChannelEventRunnable(channel, handler, ChannelEventRunnable.ChannelState.RECEIVED, message)); } catch (Throwable t) { //TODO 临时解决线程池满后异常信息无法发送到对端的问题。待重构 //fix 线程池满了拒绝调用不返回导致消费者一直等待超时 Request request (Request) message; if (request.isTwoWay()) { String msg Server side( url.getIp() , url.getPort() ) threadpool is exhausted ,detail msg: t.getMessage(); Response response new Response(request.getId(), request.getVersion()); response.setStatus(Response.SERVER_THREADPOOL_EXHAUSTED_ERROR); response.setErrorMessage(msg); channel.send(response); return; } throw new ExecutionException(message, channel, getClass() error when process received event ., t); } // 非请求消息直接执行 } else if (message instanceof Response) { handler.received(channel, message); } else { throw new RemotingException(channel, Unsupported message: message.getClass() , message: message); } } Override public void caught(Channel channel, Throwable exception) throws RemotingException { handler.caught(channel, exception); } }和 MessageOnlyChannelHandler 相比仅请求消息提交到线程池进行执行。3.7 ConnectionOrderedChannelHandlercom.alibaba.dubbo.remoting.transport.dispatcher.connection.ConnectionOrderedChannelHandler实现 WrappedChannelHandler 抽象类在 IO 线程上将连接断开事件放入队列有序逐个执行其它消息派发到线程池。代码如下public class ConnectionOrderedChannelHandler extends WrappedChannelHandler { /** * 连接事件线程池 */ protected final ThreadPoolExecutor connectionExecutor; /** * 队列大小 */ private final int queuewarninglimit; public ConnectionOrderedChannelHandler(ChannelHandler handler, URL url) { super(handler, url); // 创建连接事件线程池 String threadName url.getParameter(Constants.THREAD_NAME_KEY, Constants.DEFAULT_THREAD_NAME); connectionExecutor new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueueRunnable(url.getPositiveParameter(Constants.CONNECT_QUEUE_CAPACITY, Integer.MAX_VALUE)), // 队列大小 new NamedThreadFactory(threadName, true), // 线程工厂 new AbortPolicyWithReport(threadName, url) // 拒绝策略 ); queuewarninglimit url.getParameter(Constants.CONNECT_QUEUE_WARNING_SIZE, Constants.DEFAULT_CONNECT_QUEUE_WARNING_SIZE); } Override public void connected(Channel channel) throws RemotingException { try { // 检查队列剩余大小 checkQueueLength(); // 提交线程池执行任务 connectionExecutor.execute(new ChannelEventRunnable(channel, handler, ChannelEventRunnable.ChannelState.CONNECTED)); } catch (Throwable t) { throw new ExecutionException(connect event, channel, getClass() error when process connected event ., t); } } Override public void disconnected(Channel channel) throws RemotingException { try { // 检查队列剩余大小 checkQueueLength(); // 提交线程池执行任务 connectionExecutor.execute(new ChannelEventRunnable(channel, handler, ChannelEventRunnable.ChannelState.DISCONNECTED)); } catch (Throwable t) { throw new ExecutionException(disconnected event, channel, getClass() error when process disconnected event ., t); } } Override public void received(Channel channel, Object message) throws RemotingException { // 获得线程池 ExecutorService cexecutor getExecutorService(); try { // 提交线程池执行任务 cexecutor.execute(new ChannelEventRunnable(channel, handler, ChannelEventRunnable.ChannelState.RECEIVED, message)); } catch (Throwable t) { //TODO 临时解决线程池满后异常信息无法发送到对端的问题。待重构 //fix 线程池满了拒绝调用不返回导致消费者一直等待超时 if (message instanceof Request t instanceof RejectedExecutionException) { Request request (Request) message; if (request.isTwoWay()) { String msg Server side( url.getIp() , url.getPort() ) threadpool is exhausted ,detail msg: t.getMessage(); Response response new Response(request.getId(), request.getVersion()); response.setStatus(Response.SERVER_THREADPOOL_EXHAUSTED_ERROR); response.setErrorMessage(msg); channel.send(response); return; } } throw new ExecutionException(message, channel, getClass() error when process received event ., t); } } Override public void caught(Channel channel, Throwable exception) throws RemotingException { // 提交线程池执行任务 ExecutorService cexecutor getExecutorService(); try { cexecutor.execute(new ChannelEventRunnable(channel, handler, ChannelEventRunnable.ChannelState.CAUGHT, exception)); } catch (Throwable t) { throw new ExecutionException(caught event, channel, getClass() error when process caught event ., t); } } /** * 检查队列剩余大小 */ private void checkQueueLength() { if (connectionExecutor.getQueue().size() queuewarninglimit) { logger.warn(new IllegalThreadStateException(connectionordered channel handler queue size: connectionExecutor.getQueue().size() exceed the warning limit number : queuewarninglimit)); } } }和 AllChannelHandler 相比对于#connected(channel)和#disconnected(channel)方法提交到connectionExecutor线程池进行执行。对于#received(channel, message)和#caught(channel, exception)方法提交到executor线程池进行执行。connectionExecutor属性连接事件线程池。在 ConnectionOrderedChannelHandler 的构造方法中初始化核心属性如下线程池大小为1。队列大小为connect.queue.capacity配置项默认为Integer.MAX_VALUE。拒绝策略为 AbortPolicyWithReport 打印 JStack 分析线程状态。queuewarninglimit属性队列大小。在#checkQueueLength()方法中会校验队列中的任务数量超过上限打印告警日志。代码如下private void checkQueueLength() { if (connectionExecutor.getQueue().size() queuewarninglimit) { logger.warn(new IllegalThreadStateException(connectionordered channel handler queue size: connectionExecutor.getQueue().size() exceed the warning limit number : queuewarninglimit)); } }4. ThreadPoolcom.alibaba.dubbo.common.threadpool.ThreadPool线程池接口。代码如下SPI(fixed) public interface ThreadPool { /** * 获得线程池 * * param url URL * return 线程池 */ Adaptive({Constants.THREADPOOL_KEY}) Executor getExecutor(URL url); }SPI(fixed)注解Dubbo SPI拓展点默认为fixed。Adaptive注解基于 Dubbo SPI Adaptive 机制加载对应的线程池实现使用URL.threadpool属性。4.1 FixedThreadPoolcom.alibaba.dubbo.common.threadpool.support.fixed.FixedThreadPool实现 ThreadPool 接口固定大小线程池启动时建立线程不关闭一直持有。代码如下public class FixedThreadPool implements ThreadPool { Override public Executor getExecutor(URL url) { // 线程名 String name url.getParameter(Constants.THREAD_NAME_KEY, Constants.DEFAULT_THREAD_NAME); // 线程数 int threads url.getParameter(Constants.THREADS_KEY, Constants.DEFAULT_THREADS); // 队列数 int queues url.getParameter(Constants.QUEUES_KEY, Constants.DEFAULT_QUEUES); // 创建执行器 return new ThreadPoolExecutor(threads, threads, 0, TimeUnit.MILLISECONDS, queues 0 ? new SynchronousQueueRunnable() : (queues 0 ? new LinkedBlockingQueueRunnable() : new LinkedBlockingQueueRunnable(queues)), new NamedThreadFactory(name, true), new AbortPolicyWithReport(name, url)); } }核心参数如下threads属性线程数默认为200。queues属性任务队列数queues 0 SynchronousQueue 对象。queues 0 LinkedBlockingQueue 对象。queues 0带队列数的 LinkedBlockingQueue 对象。拒绝策略AbortPolicyWithReport 。4.2 CachedThreadPoolcom.alibaba.dubbo.common.threadpool.support.cached.CachedThreadPool实现 ThreadPool 接口缓存线程池空闲一定时长自动删除需要时重建。代码如下public class CachedThreadPool implements ThreadPool { Override public Executor getExecutor(URL url) { // 线程名 String name url.getParameter(Constants.THREAD_NAME_KEY, Constants.DEFAULT_THREAD_NAME); // 核心线程数 int cores url.getParameter(Constants.CORE_THREADS_KEY, Constants.DEFAULT_CORE_THREADS); // 最大线程数 int threads url.getParameter(Constants.THREADS_KEY, Constants.DEFAULT_THREADS); // 队列数 int queues url.getParameter(Constants.QUEUES_KEY, Constants.DEFAULT_QUEUES); // 线程存活时长 int alive url.getParameter(Constants.ALIVE_KEY, Constants.DEFAULT_ALIVE); // 创建执行器 return new ThreadPoolExecutor(cores, threads, alive, TimeUnit.MILLISECONDS, queues 0 ? new SynchronousQueueRunnable() : (queues 0 ? new LinkedBlockingQueueRunnable() : new LinkedBlockingQueueRunnable(queues)), new NamedThreadFactory(name, true), new AbortPolicyWithReport(name, url)); } }核心参数如下cores属性核心线程数默认为0。threads属性最大线程数默认为200。queues属性任务队列数queues 0 SynchronousQueue 对象。queues 0 LinkedBlockingQueue 对象。queues 0带队列数的 LinkedBlockingQueue 对象。alive属性线程空闲时长单位毫秒默认为60 * 1000毫秒。拒绝策略AbortPolicyWithReport 。4.3 LimitedThreadPoolcom.alibaba.dubbo.common.threadpool.support.limited.LimitedThreadPool实现 ThreadPool 接口可伸缩线程池但池中的线程数只会增长不会收缩。只增长不收缩的目的是为了避免收缩时突然来了大流量引起的性能问题。代码如下public class LimitedThreadPool implements ThreadPool { Override public Executor getExecutor(URL url) { // 线程名 String name url.getParameter(Constants.THREAD_NAME_KEY, Constants.DEFAULT_THREAD_NAME); // 核心线程数 int cores url.getParameter(Constants.CORE_THREADS_KEY, Constants.DEFAULT_CORE_THREADS); // 最大线程数 int threads url.getParameter(Constants.THREADS_KEY, Constants.DEFAULT_THREADS); // 队列数 int queues url.getParameter(Constants.QUEUES_KEY, Constants.DEFAULT_QUEUES); // 创建执行器 return new ThreadPoolExecutor(cores, threads, Long.MAX_VALUE, TimeUnit.MILLISECONDS, queues 0 ? new SynchronousQueueRunnable() : (queues