Class VirtualExecutor
java.lang.Object
cloud.opencode.base.parallel.executor.VirtualExecutor
- All Implemented Interfaces:
AutoCloseable
Virtual Executor - Virtual Thread Executor
虚拟执行器 - 虚拟线程执行器
A wrapper around JDK virtual thread executor with additional features like concurrency limiting, naming, and statistics.
JDK 虚拟线程执行器的包装器,提供并发限制、命名和统计等附加功能。
Example | 示例:
VirtualExecutor executor = VirtualExecutor.create();
executor.submit(() -> processTask());
// With concurrency limit
VirtualExecutor limited = VirtualExecutor.withConcurrency(10);
limited.submit(() -> limitedTask());
Features | 主要功能:
- Virtual thread execution - 虚拟线程执行
- Concurrency limiting with Semaphore - 使用信号量限制并发
- Task statistics collection - 任务统计收集
- Named virtual thread support - 命名虚拟线程支持
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Since:
- JDK 25, opencode-base-parallel V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()static VirtualExecutorcreate()Creates a new virtual executor.Future<?> Submits a task using the underlying executor.intGets the available permits (for limited executor).longGets the number of completed tasks.Gets the configuration.longGets the number of failed tasks.longGets the number of pending tasks.longGets the number of submitted tasks.<T> List<T> invokeAll(Collection<? extends Callable<T>> tasks) Submits multiple tasks and waits for all.<T> List<T> invokeAll(Collection<? extends Callable<T>> tasks, Duration timeout) Submits multiple tasks with timeout.booleanChecks if the executor is shutdown.booleanChecks if the executor is terminated.static VirtualExecutorshared()Gets the shared virtual executor.voidshutdown()Shuts down the executor.booleanshutdownAndAwait(Duration timeout) Shuts down and waits for termination.Submits a runnable task.<T> CompletableFuture<T> Submits a callable task.static VirtualExecutorwithConcurrency(int maxConcurrency) Creates a virtual executor with concurrency limit.static VirtualExecutorwithConfig(ExecutorConfig config) Creates a virtual executor with configuration.
-
Method Details
-
create
Creates a new virtual executor. 创建新的虚拟执行器。- Returns:
- the executor - 执行器
-
withConcurrency
Creates a virtual executor with concurrency limit. 创建带并发限制的虚拟执行器。- Parameters:
maxConcurrency- the max concurrency - 最大并发数- Returns:
- the executor - 执行器
-
withConfig
Creates a virtual executor with configuration. 使用配置创建虚拟执行器。- Parameters:
config- the configuration - 配置- Returns:
- the executor - 执行器
-
submit
Submits a runnable task. 提交 Runnable 任务。- Parameters:
task- the task - 任务- Returns:
- the future - Future
-
submit
Submits a callable task. 提交 Callable 任务。- Type Parameters:
T- the result type - 结果类型- Parameters:
task- the task - 任务- Returns:
- the future - Future
-
invokeAll
Submits multiple tasks and waits for all. 提交多个任务并等待全部完成。- Type Parameters:
T- the result type - 结果类型- Parameters:
tasks- the tasks - 任务- Returns:
- the results - 结果
-
invokeAll
Submits multiple tasks with timeout. 提交多个任务并设置超时。- Type Parameters:
T- the result type - 结果类型- Parameters:
tasks- the tasks - 任务timeout- the timeout - 超时- Returns:
- the results - 结果
-
execute
-
getSubmittedCount
public long getSubmittedCount()Gets the number of submitted tasks. 获取提交的任务数。- Returns:
- the submitted count - 提交数
-
getCompletedCount
public long getCompletedCount()Gets the number of completed tasks. 获取完成的任务数。- Returns:
- the completed count - 完成数
-
getFailedCount
public long getFailedCount()Gets the number of failed tasks. 获取失败的任务数。- Returns:
- the failed count - 失败数
-
getPendingCount
public long getPendingCount()Gets the number of pending tasks. 获取待处理的任务数。- Returns:
- the pending count - 待处理数
-
getAvailablePermits
public int getAvailablePermits()Gets the available permits (for limited executor). 获取可用许可(用于受限执行器)。- Returns:
- the available permits or -1 if unlimited - 可用许可或 -1(如果无限制)
-
getConfig
-
shutdown
public void shutdown()Shuts down the executor. 关闭执行器。 -
shutdownAndAwait
Shuts down and waits for termination. 关闭并等待终止。- Parameters:
timeout- the timeout - 超时- Returns:
- true if terminated - 如果终止返回 true
- Throws:
InterruptedException- if interrupted - 如果中断
-
isShutdown
public boolean isShutdown()Checks if the executor is shutdown. 检查执行器是否已关闭。- Returns:
- true if shutdown - 如果已关闭返回 true
-
isTerminated
public boolean isTerminated()Checks if the executor is terminated. 检查执行器是否已终止。- Returns:
- true if terminated - 如果已终止返回 true
-
close
public void close()- Specified by:
closein interfaceAutoCloseable