Class OpenThread
java.lang.Object
cloud.opencode.base.core.thread.OpenThread
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 Summary
Modifier and TypeMethodDescriptionstatic ExecutorServiceCreates a cached thread pool with default name prefix "opencode-cached-pool" 创建缓存线程池,使用默认名称前缀 "opencode-cached-pool"static ExecutorServicecreateCachedThreadPool(String namePrefix) Creates a cached thread pool 创建缓存线程池static ExecutorServicecreateFixedThreadPool(int nThreads) Creates a fixed-size thread pool with default name prefix "opencode-fixed-pool" 创建固定大小线程池,使用默认名称前缀 "opencode-fixed-pool"static ExecutorServicecreateFixedThreadPool(int nThreads, String namePrefix) Creates a fixed-size thread pool 创建固定大小线程池static ScheduledExecutorServicecreateScheduledThreadPool(int corePoolSize) Creates a scheduled thread pool with default name prefix "opencode-scheduled-pool" 创建调度线程池,使用默认名称前缀 "opencode-scheduled-pool"static ScheduledExecutorServicecreateScheduledThreadPool(int corePoolSize, String namePrefix) Creates a scheduled thread pool 创建调度线程池static ExecutorServiceCreates a single-thread executor with default name prefix "opencode-single-pool" 创建单线程执行器,使用默认名称前缀 "opencode-single-pool"static ExecutorServicecreateSingleThreadExecutor(String namePrefix) Creates a single-thread executor 创建单线程执行器static ExecutorServiceCreates a virtual thread executor (JDK 21+) 创建虚拟线程执行器 (JDK 21+)static ExecutorServicecreateVirtualThreadExecutor(String namePrefix) Creates a virtual thread executor (with name prefix) 创建虚拟线程执行器(带名称前缀)static ThreadGets the current thread 获取当前线程static longGets the current thread ID 获取当前线程 IDstatic StringGets the current thread name 获取当前线程名称static <T> CompletableFuture<T> executeAsync(Supplier<T> supplier, Duration timeout) Executes asynchronously with timeout 带超时的异步执行static Thread[]Gets all threads 获取所有线程static Thread.StategetThreadState(long threadId) Gets the thread state 获取线程状态static voidInterrupts the thread 中断线程static booleanChecks and clears the interrupt status 检查并清除中断状态static booleanChecks if the current thread is interrupted 检查当前线程是否被中断static booleanChecks if the current thread is a virtual thread 检查当前线程是否为虚拟线程static booleanisVirtualThread(Thread thread) Checks if the specified thread is a virtual thread 检查指定线程是否为虚拟线程static CompletableFuture<Void> Executes a task asynchronously 异步执行任务static CompletableFuture<Void> Executes a task asynchronously (with specified executor) 异步执行任务(指定执行器)static booleanshutdownGracefully(ExecutorService executor, Duration timeout) Gracefully shuts down the thread pool 优雅关闭线程池static voidshutdownNow(ExecutorService executor) Immediately shuts down the thread pool 立即关闭线程池static voidPuts the thread to sleep 线程睡眠static booleansleepInterruptibly(Duration duration) Interruptible sleep 可中断睡眠static voidsleepMillis(long millis) Puts the thread to sleep (milliseconds) 线程睡眠(毫秒)static voidsleepSeconds(long seconds) Puts the thread to sleep (seconds) 线程睡眠(秒)static <T> CompletableFuture<T> supplyAsync(Supplier<T> supplier) Executes asynchronously and returns a result 异步执行并返回结果static <T> CompletableFuture<T> supplyAsync(Supplier<T> supplier, Executor executor) Executes asynchronously and returns a result (with specified executor) 异步执行并返回结果(指定执行器)
-
Method Details
-
createFixedThreadPool
Creates a fixed-size thread pool with default name prefix "opencode-fixed-pool" 创建固定大小线程池,使用默认名称前缀 "opencode-fixed-pool" -
createFixedThreadPool
Creates a fixed-size thread pool 创建固定大小线程池 -
createCachedThreadPool
Creates a cached thread pool with default name prefix "opencode-cached-pool" 创建缓存线程池,使用默认名称前缀 "opencode-cached-pool" -
createCachedThreadPool
Creates a cached thread pool 创建缓存线程池 -
createSingleThreadExecutor
Creates a single-thread executor with default name prefix "opencode-single-pool" 创建单线程执行器,使用默认名称前缀 "opencode-single-pool" -
createSingleThreadExecutor
Creates a single-thread executor 创建单线程执行器 -
createScheduledThreadPool
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
Creates a virtual thread executor (JDK 21+) 创建虚拟线程执行器 (JDK 21+) -
createVirtualThreadExecutor
Creates a virtual thread executor (with name prefix) 创建虚拟线程执行器(带名称前缀) -
runAsync
Executes a task asynchronously 异步执行任务 -
runAsync
Executes a task asynchronously (with specified executor) 异步执行任务(指定执行器) -
supplyAsync
Executes asynchronously and returns a result 异步执行并返回结果 -
supplyAsync
Executes asynchronously and returns a result (with specified executor) 异步执行并返回结果(指定执行器) -
executeAsync
Executes asynchronously with timeout 带超时的异步执行 -
sleep
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
Interruptible sleep 可中断睡眠 -
currentThread
Gets the current thread 获取当前线程 -
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
Checks if the specified thread is a virtual thread 检查指定线程是否为虚拟线程 -
getAllThreads
Gets all threads 获取所有线程 -
getThreadState
Gets the thread state 获取线程状态 -
shutdownGracefully
Gracefully shuts down the thread pool 优雅关闭线程池 -
shutdownNow
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
Interrupts the thread 中断线程
-