Class VirtualExecutor

java.lang.Object
cloud.opencode.base.parallel.executor.VirtualExecutor
All Implemented Interfaces:
AutoCloseable

public final class VirtualExecutor extends Object implements 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 Details

    • shared

      public static VirtualExecutor shared()
      Gets the shared virtual executor. 获取共享虚拟执行器。
      Returns:
      the shared executor - 共享执行器
    • create

      public static VirtualExecutor create()
      Creates a new virtual executor. 创建新的虚拟执行器。
      Returns:
      the executor - 执行器
    • withConcurrency

      public static VirtualExecutor withConcurrency(int maxConcurrency)
      Creates a virtual executor with concurrency limit. 创建带并发限制的虚拟执行器。
      Parameters:
      maxConcurrency - the max concurrency - 最大并发数
      Returns:
      the executor - 执行器
    • withConfig

      public static VirtualExecutor withConfig(ExecutorConfig config)
      Creates a virtual executor with configuration. 使用配置创建虚拟执行器。
      Parameters:
      config - the configuration - 配置
      Returns:
      the executor - 执行器
    • submit

      public CompletableFuture<Void> submit(Runnable task)
      Submits a runnable task. 提交 Runnable 任务。
      Parameters:
      task - the task - 任务
      Returns:
      the future - Future
    • submit

      public <T> CompletableFuture<T> submit(Callable<T> task)
      Submits a callable task. 提交 Callable 任务。
      Type Parameters:
      T - the result type - 结果类型
      Parameters:
      task - the task - 任务
      Returns:
      the future - Future
    • invokeAll

      public <T> List<T> invokeAll(Collection<? extends Callable<T>> tasks)
      Submits multiple tasks and waits for all. 提交多个任务并等待全部完成。
      Type Parameters:
      T - the result type - 结果类型
      Parameters:
      tasks - the tasks - 任务
      Returns:
      the results - 结果
    • invokeAll

      public <T> List<T> invokeAll(Collection<? extends Callable<T>> tasks, Duration timeout)
      Submits multiple tasks with timeout. 提交多个任务并设置超时。
      Type Parameters:
      T - the result type - 结果类型
      Parameters:
      tasks - the tasks - 任务
      timeout - the timeout - 超时
      Returns:
      the results - 结果
    • execute

      public Future<?> execute(Runnable task)
      Submits a task using the underlying executor. 使用底层执行器提交任务。
      Parameters:
      task - the task - 任务
      Returns:
      the future - Future
    • 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

      public ExecutorConfig getConfig()
      Gets the configuration. 获取配置。
      Returns:
      the config - 配置
    • shutdown

      public void shutdown()
      Shuts down the executor. 关闭执行器。
    • shutdownAndAwait

      public boolean shutdownAndAwait(Duration timeout) throws InterruptedException
      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:
      close in interface AutoCloseable