Class WorkerExecutor
java.lang.Object
tech.riemann.nif.support.tiangong.connector.WorkerExecutor
天工容器业务线程池。
线程模型按以下优先级装配:
- 虚拟线程(JDK 21+ 且
preferVirtualThreads=true)—— 任务级创建,天然不设线程上限; poolSize > 0 && queueCapacity > 0—— 有界阻塞队列的固定池,队列满时抛出RejectedExecutionException,调用方通过tryExecute(Runnable)拿到false后降级处理;poolSize <= 0—— 缓存线程池(线程数无界,仅为兼容历史配置保留);- 否则 —— 固定大小池,队列无界。
生产路径(业务请求、异步请求、WebSocket 帧)应使用第 1、2 档,以获得可预期的线程上限与可关闭的生命周期。
-
Constructor Summary
ConstructorsConstructorDescriptionWorkerExecutor(int poolSize) WorkerExecutor(int poolSize, boolean preferVirtualThreads) WorkerExecutor(int poolSize, boolean preferVirtualThreads, String namePrefix, int queueCapacity) -
Method Summary
Modifier and TypeMethodDescriptionvoid提交任务;poolSize<=0(缓存池)分支下RejectedExecutionException不会出现。intbooleanstatic booleanvoidshutdown()booleanshutdownAndAwait(long timeout, TimeUnit unit) 关闭并等待既有任务结束。booleantryExecute(Runnable task) 提交任务并把「拒绝」作为返回值暴露给调用方,便于在队列打满时降级而非抛出。
-
Constructor Details
-
WorkerExecutor
public WorkerExecutor(int poolSize) -
WorkerExecutor
public WorkerExecutor(int poolSize, boolean preferVirtualThreads) -
WorkerExecutor
public WorkerExecutor(int poolSize, boolean preferVirtualThreads, String namePrefix, int queueCapacity) - Parameters:
poolSize- 线程数;<=0且队列无界时表示「缓存线程池」preferVirtualThreads- 是否优先使用虚拟线程namePrefix- 线程名前缀,便于线程转储定位queueCapacity->0时使用有界队列并启用拒绝策略,<=0时保持历史语义
-
-
Method Details
-
execute
提交任务;poolSize<=0(缓存池)分支下RejectedExecutionException不会出现。 -
tryExecute
提交任务并把「拒绝」作为返回值暴露给调用方,便于在队列打满时降级而非抛出。- Returns:
- 已接受返回
true;被拒绝返回false
-
isVirtualThreads
public boolean isVirtualThreads() -
getPoolSize
public int getPoolSize() -
getNamePrefix
-
shutdown
public void shutdown() -
shutdownAndAwait
关闭并等待既有任务结束。- Returns:
- 全部任务在超时前结束返回
true
-
isVirtualThreadsSupported
public static boolean isVirtualThreadsSupported()
-