lzh-1.8-thread-spring-boot-starter 是一个基于 Spring Boot 的线程管理启动器,旨在提供灵活且可扩展的线程执行能力。它通过封装线程池和线程装饰器,简化了在 Spring Boot 应用中管理多线程任务的过程。
- 线程池支持:提供自定义线程池实现,支持核心线程数、最大线程数、任务队列等配置。
- 线程装饰器:允许通过
ThreadDecorator接口对线程进行扩展和定制。 - 线程工厂:提供统一的线程创建机制,支持线程名称前缀、守护线程设置等功能。
- 计划任务支持:支持创建可调度的线程池,用于执行定时或周期性任务。
在 pom.xml 中添加以下依赖以使用该组件:
<dependency>
<groupId>com.lzh</groupId>
<artifactId>lzh-1.8-thread-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>你可以通过代码直接创建和配置线程池:
LzhThreadExecutor executor = new LzhThreadExecutor(
2, 10, 60L, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(),
new LzhThreadFactory("my-pool"),
new MyThreadDecorator(),
new ThreadPoolExecutor.AbortPolicy()
);使用线程池提交任务:
executor.execute(() -> {
// 执行任务逻辑
});创建一个定时任务:
ScheduledThreadPoolExecutor scheduledExecutor = LzhThreadExecutor.createScheduledThreadPoolExecutor(2, "scheduled-pool");
scheduledExecutor.scheduleAtFixedRate(() -> {
// 定时任务逻辑
}, 0, 1, TimeUnit.SECONDS);- LzhThreadExecutor:继承自
ThreadPoolExecutor,提供增强的线程池功能。 - LzhThreadFactory:线程工厂类,用于创建具有统一命名规则的线程。
- ThreadDecorator:线程装饰接口,用于扩展线程行为。
欢迎贡献代码或提出建议!请遵循以下步骤:
- Fork 本仓库。
- 创建新分支 (
git checkout -b feature/your-feature-name)。 - 提交更改 (
git commit -am 'Add some feature')。 - 推送分支 (
git push origin feature/your-feature-name)。 - 创建 Pull Request。
本项目采用 MIT 许可证。详情请查看 LICENSE 文件。