Hutool Hutool
(opens new window)
🏡首页
📖指南
🎢最佳实践
💖支持
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
(opens new window)
🏡首页
📖指南
🎢最佳实践
💖支持
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
  • 快速入门

  • 核心(hutool-core)

    • AI(Hutool-ai)

    • 注解(annotation)

    • 数组(array)

    • JavaBean(bean)

    • 缓存(cache)

    • 类加载器(classloader)

    • 编码解码(codec)

    • 集合(collection)

    • 比较器(comparator)

    • 压缩(compress)

    • 类型转换(convert)

    • 数据(data)

    • 日期时间(date)

    • 异常(exception)

    • 函数(func)

    • IO流(io)

    • 语言特性(lang)

    • Map(map)

    • 数字数学(math)

    • 网络(net)

    • 对象池(pool)

    • 反射(reflect)

    • 正则(regex)

    • 服务提供(spi)

    • 聚合操作(stream)

    • 字符串文本(text)

    • 并发和线程(thread)

      • 简介
      • 线程工具-ThreadUtil
        • 1. 线程的创建和启动
          • 1.1 创建并启动线程
          • 1.2 批量创建线程
        • 2. 线程的睡眠和等待
          • 2.1 线程睡眠
          • 2.2 线程等待
        • 3. 线程的中断和终止
          • 3.1 中断线程
          • 3.2 检查中断状态
        • 4. 线程的命名和状态
          • 4.1 线程命名
          • 4.2 线程状态查询
        • 5. 线程池的使用
          • 5.1 获取全局线程池
          • 5.2 创建线程池
          • 5.3 关闭线程池
        • 6. 异步执行
          • 6.1 异步执行任务
          • 6.2 异步执行带回调的任务
        • 7. 其他常用功能
          • 7.1 获取当前线程组
          • 7.2 线程本地变量
          • 7.3 线程堆栈信息
        • 8. 适用场景
      • 异步工具类-AsyncUtil
      • 自定义线程池-ExecutorBuilder
      • 高并发测试-ConcurrencyTester
    • 树结构(tree)

    • 工具集合(util)

    • XML操作(xml)

  • 定时任务(hutool-cron)

  • 加密(hutool-crypto)

  • 数据库(hutool-db)

  • 扩展(hutool-extra)

  • HTTP(hutool-http)

  • 日志(hutool-log)

  • 配置文件(hutool-setting)

  • JSON(hutool-json)

  • Office办公(hutool-poi)

  • 套接字(hutool-socket)

  • GUI(hutool-swing)

  • 指南
  • 核心(hutool-core)
  • 并发和线程(thread)
Hutool
2026-01-16
目录

线程工具-ThreadUtil

# 线程工具-ThreadUtil

ThreadUtil是Hutool中用于处理线程操作的核心工具类,提供了丰富的线程创建、启动、睡眠、中断、命名等方法,以及线程池相关的功能,让线程操作变得更加便捷和高效。

# 1. 线程的创建和启动

# 1.1 创建并启动线程

// 创建并启动线程
Thread thread = ThreadUtil.newThread(() -> {
    // 线程执行的任务
    Console.log("线程执行中...");
});

// 创建并启动命名线程
Thread namedThread = ThreadUtil.newNamedThread("my-thread", () -> {
    Console.log("命名线程执行中...");
});

// 创建并启动守护线程
Thread daemonThread = ThreadUtil.newDaemonThread(() -> {
    Console.log("守护线程执行中...");
});

# 1.2 批量创建线程

// 批量创建线程
List<Thread> threads = ThreadUtil.newThreads(5, i -> {
    return () -> {
        Console.log("线程 " + i + " 执行中...");
    };
});

# 2. 线程的睡眠和等待

# 2.1 线程睡眠

// 睡眠1秒
ThreadUtil.sleep(1000);

// 睡眠1秒,忽略 InterruptedException
ThreadUtil.sleepQuietly(1000);

// 睡眠随机时间(1-3秒)
ThreadUtil.sleepRandom(1000, 3000);

# 2.2 线程等待

// 等待线程结束
Thread thread = ThreadUtil.newThread(() -> {
    // 线程执行的任务
});
thread.start();
ThreadUtil.join(thread);

// 等待线程结束,最多等待1秒
ThreadUtil.join(thread, 1000);

# 3. 线程的中断和终止

# 3.1 中断线程

// 中断线程
Thread thread = ThreadUtil.newThread(() -> {
    while (!Thread.currentThread().isInterrupted()) {
        // 线程执行的任务
    }
});
thread.start();
ThreadUtil.interrupt(thread);

// 中断当前线程
ThreadUtil.interrupt();

# 3.2 检查中断状态

// 检查当前线程是否被中断
boolean isInterrupted = ThreadUtil.isInterrupted();

// 检查线程是否被中断
boolean isThreadInterrupted = ThreadUtil.isInterrupted(thread);

# 4. 线程的命名和状态

# 4.1 线程命名

// 获取当前线程名称
String threadName = ThreadUtil.getName();

// 设置当前线程名称
ThreadUtil.setName("new-thread-name");

// 获取指定线程名称
String name = ThreadUtil.getName(thread);

// 设置指定线程名称
ThreadUtil.setName(thread, "new-thread-name");

# 4.2 线程状态查询

// 检查线程是否存活
boolean isAlive = ThreadUtil.isAlive(thread);

// 检查线程是否为守护线程
boolean isDaemon = ThreadUtil.isDaemon(thread);

// 获取当前线程
Thread currentThread = ThreadUtil.currentThread();

# 5. 线程池的使用

# 5.1 获取全局线程池

// 获取全局线程池
ExecutorService executor = ThreadUtil.getExecutor();

// 使用全局线程池执行任务
ThreadUtil.execAsync(() -> {
    Console.log("使用全局线程池执行任务");
});

# 5.2 创建线程池

// 创建固定大小的线程池
ExecutorService fixedExecutor = ThreadUtil.newFixedExecutor(5);

// 创建缓存线程池
ExecutorService cachedExecutor = ThreadUtil.newCachedExecutor();

// 创建单线程线程池
ExecutorService singleExecutor = ThreadUtil.newSingleExecutor();

// 创建定时线程池
ScheduledExecutorService scheduledExecutor = ThreadUtil.newScheduledExecutor(5);

# 5.3 关闭线程池

// 关闭线程池
ThreadUtil.shutdown(executor);

// 强制关闭线程池
ThreadUtil.shutdownNow(executor);

# 6. 异步执行

# 6.1 异步执行任务

// 异步执行任务
Future<?> future = ThreadUtil.execAsync(() -> {
    Console.log("异步执行任务");
    return "result";
});

// 获取异步执行结果
Object result = future.get();

# 6.2 异步执行带回调的任务

// 异步执行带回调的任务
ThreadUtil.execAsync(() -> {
    Console.log("异步执行任务");
    return "result";
}, result -> {
    Console.log("异步任务执行完成,结果:" + result);
});

# 7. 其他常用功能

# 7.1 获取当前线程组

// 获取当前线程组
ThreadGroup threadGroup = ThreadUtil.getCurrentThreadGroup();

# 7.2 线程本地变量

// 创建线程本地变量
ThreadLocal<String> threadLocal = ThreadUtil.createThreadLocal();

// 设置线程本地变量值
threadLocal.set("value");

// 获取线程本地变量值
String value = threadLocal.get();

# 7.3 线程堆栈信息

// 获取当前线程堆栈信息
String stackTrace = ThreadUtil.getStackTrace();

// 获取指定线程堆栈信息
String threadStackTrace = ThreadUtil.getStackTrace(thread);

# 8. 适用场景

ThreadUtil适用于以下场景:

  • 需要创建和管理线程时
  • 需要进行线程睡眠、等待、中断等操作时
  • 需要规范线程命名时
  • 需要使用线程池进行任务调度时
  • 需要进行异步编程时
  • 需要获取线程状态、堆栈信息等调试信息时

通过ThreadUtil,开发者可以更加便捷地进行线程操作,编写更简洁、更易维护的代码,同时享受到更丰富的线程操作功能。

简介
异步工具类-AsyncUtil

← 简介 异步工具类-AsyncUtil→

Theme by Vdoing | Copyright © 2025-2026 Hutool | Apache-2.0
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式