Class ThreadUtil

java.lang.Object
com.sure.tool.thread.ThreadUtil

public class ThreadUtil extends Object
线程工具类:异步执行、休眠、线程工厂,参考 Hutool 的 ThreadUtil 设计。

English: Thread creation, sleeping, joining and virtual-thread helpers.

Since:
0.1.0
Author:
suretool
  • Method Details

    • execAsync

      public static Future<?> execAsync(Runnable runnable)
      异步执行任务。
      Parameters:
      runnable - 任务
      Returns:
      Future
    • execAsync

      public static <T> Future<T> execAsync(Callable<T> callable)
      异步执行有返回值任务。
      Type Parameters:
      T - 返回类型
      Parameters:
      callable - 任务
      Returns:
      Future
    • execute

      public static void execute(Runnable runnable)
      后台线程执行任务(不阻塞当前线程,不捕获异常)。
      Parameters:
      runnable - 任务
    • sleep

      public static void sleep(long millis)
      休眠(毫秒),中断时恢复中断标志并抛运行时异常。
      Parameters:
      millis - 毫秒数
    • sleep

      public static void sleep(long timeout, TimeUnit unit)
      休眠(指定时间单位),中断时恢复中断标志并抛运行时异常。
      Parameters:
      timeout - 时长
      unit - 时间单位
    • sleep

      public static void sleep(Duration duration)
      休眠(指定时长),中断时恢复中断标志并抛运行时异常。
      Parameters:
      duration - 时长,不能为 null 且不能为负
    • sleepInterruptibly

      public static void sleepInterruptibly(Duration duration) throws InterruptedException
      可中断睡眠:InterruptedException 直接抛出(区别于 sleep(Duration) 的静默恢复中断位),适合需要感知中断的取消协作场景。
      Parameters:
      duration - 睡眠时长(null / 非正时长直接返回)
      Throws:
      InterruptedException - 线程被中断
      Since:
      1.12.0
    • newThread

      public static Thread newThread(Runnable runnable, String name)
      创建命名线程。
      Parameters:
      runnable - 任务
      name - 线程名
      Returns:
      线程
    • newThread

      public static Thread newThread(Runnable runnable, String name, boolean daemon)
      创建命名线程。
      Parameters:
      runnable - 任务
      name - 线程名
      daemon - 是否守护线程
      Returns:
      线程
    • getExecutor

      public static ExecutorService getExecutor()
      获取共享线程池。
      Returns:
      共享线程池
    • newExecutor

      public static ExecutorService newExecutor(int size)
      创建命名固定大小线程池(守护线程,前缀 sure-)。
      Parameters:
      size - 线程数,必须大于 0
      Returns:
      线程池
    • newExecutor

      public static ExecutorService newExecutor(int size, String prefix)
      创建命名固定大小线程池(守护线程,前缀 sure-{prefix}-)。
      Parameters:
      size - 线程数,必须大于 0
      prefix - 线程名前缀
      Returns:
      线程池
    • newScheduledExecutor

      public static ScheduledExecutorService newScheduledExecutor(int size, String prefix)
      创建命名调度线程池(守护线程,前缀 sure-{prefix}-)。
      Parameters:
      size - 核心线程数,必须大于 0
      prefix - 线程名前缀
      Returns:
      调度线程池
    • joinQuietly

      public static void joinQuietly(Thread thread)
      等待线程结束,忽略中断(中断标志保持原样)。
      Parameters:
      thread - 线程,可为 null
    • getProcessorCount

      public static int getProcessorCount()
      处理器核数。
      Returns:
      CPU 核数
    • shutdownQuietly

      public static void shutdownQuietly(ExecutorService executor)
      安静关闭线程池(最多等待 30 秒)。
      Parameters:
      executor - 线程池
    • startVirtualThread

      public static Thread startVirtualThread(Runnable runnable)
      启动一个虚拟线程执行任务(JDK 21+)。
      Parameters:
      runnable - 任务
      Returns:
      已启动的虚拟线程
    • newVirtualThread

      public static Thread newVirtualThread(Runnable runnable, String name)
      创建虚拟线程(未启动)。
      Parameters:
      runnable - 任务
      name - 线程名
      Returns:
      虚拟线程
    • virtualExecutor

      public static ExecutorService virtualExecutor()
      创建每任务一虚拟线程的执行器(JDK 21+,适合高并发 I/O 密集任务)。
      Returns:
      ExecutorService
    • virtualExecutor

      public static ExecutorService virtualExecutor(String prefix)
      创建带命名前缀的每任务一虚拟线程执行器。
      Parameters:
      prefix - 线程名前缀
      Returns:
      ExecutorService
    • virtualThreadFactory

      public static ThreadFactory virtualThreadFactory(String prefix)
      虚拟线程工厂(Thread.ofVirtual 便捷入口)。
      Parameters:
      prefix - 线程名前缀
      Returns:
      ThreadFactory
    • isVirtual

      public static boolean isVirtual(Thread thread)
      判断线程是否为虚拟线程。
      Parameters:
      thread - 线程
      Returns:
      是否虚拟线程
    • isVirtual

      public static boolean isVirtual()
      判断当前线程是否为虚拟线程。
      Returns:
      是否虚拟线程
    • parallel

      public static void parallel(Runnable... tasks)
      用虚拟线程并发执行全部任务并等待完成;任一任务抛异常则聚合抛出。
      Parameters:
      tasks - 任务列表
    • invokeAll

      public static <T> List<T> invokeAll(List<Callable<T>> tasks)
      用虚拟线程并发执行有返回值任务并返回结果(按入参顺序)。
      Type Parameters:
      T - 返回类型
      Parameters:
      tasks - 任务列表
      Returns:
      结果列表
    • invokeAll

      public static <T> List<T> invokeAll(List<Callable<T>> tasks, Duration timeout)
      并行执行全部任务并在指定时限内返回结果(限时版本)。

      超时后未完成的任务会被取消,并抛出 IllegalStateException(携带超时原因)。

      Type Parameters:
      T - 返回类型
      Parameters:
      tasks - 任务列表,可为空
      timeout - 整体执行时限,不允许为 null
      Returns:
      结果列表;输入为空时返回空列表
      Since:
      1.1.0