Class OpenThread

java.lang.Object
cloud.opencode.base.core.thread.OpenThread

public final class OpenThread extends Object
Thread Utility Class - Thread pool creation and management 线程工具类 - 线程池创建和管理

Provides utilities for thread pool creation, async execution, and thread management.

提供线程池创建、异步执行和线程管理工具。

Features | 主要功能:

  • Thread pool creation (fixed, cached, scheduled, virtual) - 线程池创建
  • Async execution (runAsync, supplyAsync, timeout) - 异步执行
  • Thread sleep utilities - 线程睡眠工具
  • Graceful shutdown - 优雅关闭
  • Virtual thread support (JDK 21+) - 虚拟线程支持

Usage Examples | 使用示例:

// Create thread pool - 创建线程池
ExecutorService pool = OpenThread.createFixedThreadPool(4, "worker");

// Async execution - 异步执行
CompletableFuture<String> future = OpenThread.supplyAsync(() -> "result");

// Virtual thread - 虚拟线程
ExecutorService virtual = OpenThread.createVirtualThreadExecutor();

// Graceful shutdown - 优雅关闭
OpenThread.shutdownGracefully(pool, Duration.ofSeconds(30));

Security | 安全性:

  • Thread-safe: Yes (stateless) - 线程安全: 是 (无状态)
  • Null-safe: Partial - 空值安全: 部分
Since:
JDK 25, opencode-base-core V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • createFixedThreadPool

      public static ExecutorService createFixedThreadPool(int nThreads)
      Creates a fixed-size thread pool with default name prefix "opencode-fixed-pool" 创建固定大小线程池,使用默认名称前缀 "opencode-fixed-pool"
    • createFixedThreadPool

      public static ExecutorService createFixedThreadPool(int nThreads, String namePrefix)
      Creates a fixed-size thread pool 创建固定大小线程池
    • createCachedThreadPool

      public static ExecutorService createCachedThreadPool()
      Creates a cached thread pool with default name prefix "opencode-cached-pool" 创建缓存线程池,使用默认名称前缀 "opencode-cached-pool"
    • createCachedThreadPool

      public static ExecutorService createCachedThreadPool(String namePrefix)
      Creates a cached thread pool 创建缓存线程池
    • createSingleThreadExecutor

      public static ExecutorService createSingleThreadExecutor()
      Creates a single-thread executor with default name prefix "opencode-single-pool" 创建单线程执行器,使用默认名称前缀 "opencode-single-pool"
    • createSingleThreadExecutor

      public static ExecutorService createSingleThreadExecutor(String namePrefix)
      Creates a single-thread executor 创建单线程执行器
    • createScheduledThreadPool

      public static ScheduledExecutorService createScheduledThreadPool(int corePoolSize)
      Creates a scheduled thread pool with default name prefix "opencode-scheduled-pool" 创建调度线程池,使用默认名称前缀 "opencode-scheduled-pool"
    • createScheduledThreadPool

      public static ScheduledExecutorService createScheduledThreadPool(int corePoolSize, String namePrefix)
      Creates a scheduled thread pool 创建调度线程池
    • createVirtualThreadExecutor

      public static ExecutorService createVirtualThreadExecutor()
      Creates a virtual thread executor (JDK 21+) 创建虚拟线程执行器 (JDK 21+)
    • createVirtualThreadExecutor

      public static ExecutorService createVirtualThreadExecutor(String namePrefix)
      Creates a virtual thread executor (with name prefix) 创建虚拟线程执行器(带名称前缀)
    • runAsync

      public static CompletableFuture<Void> runAsync(Runnable runnable)
      Executes a task asynchronously 异步执行任务
    • runAsync

      public static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor)
      Executes a task asynchronously (with specified executor) 异步执行任务(指定执行器)
    • supplyAsync

      public static <T> CompletableFuture<T> supplyAsync(Supplier<T> supplier)
      Executes asynchronously and returns a result 异步执行并返回结果
    • supplyAsync

      public static <T> CompletableFuture<T> supplyAsync(Supplier<T> supplier, Executor executor)
      Executes asynchronously and returns a result (with specified executor) 异步执行并返回结果(指定执行器)
    • executeAsync

      public static <T> CompletableFuture<T> executeAsync(Supplier<T> supplier, Duration timeout)
      Executes asynchronously with timeout 带超时的异步执行
    • sleep

      public static void sleep(Duration duration)
      Puts the thread to sleep 线程睡眠
    • sleepMillis

      public static void sleepMillis(long millis)
      Puts the thread to sleep (milliseconds) 线程睡眠(毫秒)
    • sleepSeconds

      public static void sleepSeconds(long seconds)
      Puts the thread to sleep (seconds) 线程睡眠(秒)
    • sleepInterruptibly

      public static boolean sleepInterruptibly(Duration duration)
      Interruptible sleep 可中断睡眠
    • currentThread

      public static Thread currentThread()
      Gets the current thread 获取当前线程
    • currentThreadName

      public static String currentThreadName()
      Gets the current thread name 获取当前线程名称
    • currentThreadId

      public static long currentThreadId()
      Gets the current thread ID 获取当前线程 ID
    • isVirtualThread

      public static boolean isVirtualThread()
      Checks if the current thread is a virtual thread 检查当前线程是否为虚拟线程
    • isVirtualThread

      public static boolean isVirtualThread(Thread thread)
      Checks if the specified thread is a virtual thread 检查指定线程是否为虚拟线程
    • getAllThreads

      public static Thread[] getAllThreads()
      Gets all threads 获取所有线程
    • getThreadState

      public static Thread.State getThreadState(long threadId)
      Gets the thread state 获取线程状态
    • shutdownGracefully

      public static boolean shutdownGracefully(ExecutorService executor, Duration timeout)
      Gracefully shuts down the thread pool 优雅关闭线程池
    • shutdownNow

      public static void shutdownNow(ExecutorService executor)
      Immediately shuts down the thread pool 立即关闭线程池
    • isInterrupted

      public static boolean isInterrupted()
      Checks if the current thread is interrupted 检查当前线程是否被中断
    • interrupted

      public static boolean interrupted()
      Checks and clears the interrupt status 检查并清除中断状态
    • interrupt

      public static void interrupt(Thread thread)
      Interrupts the thread 中断线程