3 Commits

Author SHA1 Message Date
wonder d56e55a038 📝Docs: [hutool-core-config] Completed 2025-11-10 21:59:41 +08:00
wonder 8819b8481e 📝Docs: [executor-service-config] Completed 2025-11-10 21:41:23 +08:00
wonder fa4d6e0549 📝Docs: [basic-async-config] Completed 2025-11-10 18:59:10 +08:00
2 changed files with 35 additions and 29 deletions
+30 -1
View File
@@ -1 +1,30 @@
> 注意:Java ExecutorService 使用的默认等待队列是无限大小的 `LinkedBlockingQueue`,容易 OOM,不推荐使用 # spring-boot-threadpool-demo
> 📚 适用于 SpringBoot 的多种线程池实现
**注意:Main 分支不包含具体实现,具体实现在项目的不同分支上**
## 分支大纲
### 分支 1: `basic-async-config`
> ✅ Completed
**主题:基础 @Async 注解方式**
- 实现简单的异步任务执行
- 配置基本的线程池参数
- 配置拒绝策略
- 测试异步方法调用
### 分支 2: `executor-service-config`
> ✅ Completed
- Java 自带线程池
- 无限队列长度
### 分支 3: `hutool-core-config`
> ✅ Completed
- 简单易用的 Hutool 工具类
@@ -1,34 +1,11 @@
package cn.hezhaohui.threadpool; package cn.hezhaohui.threadpool;
import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.Random; @SpringBootApplication
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
@Slf4j
public class ThreadPoolApplication { public class ThreadPoolApplication {
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(2); SpringApplication.run(ThreadPoolApplication.class, args);
for (int i = 0; i < 100000000; i++) {
executorService.execute(() -> {
StringBuilder builder = new StringBuilder();
int random = new Random().nextInt(1000);
for (int j = 1; j < random; j++) {
builder.append(j);
}
try {
TimeUnit.HOURS.sleep(1);
} catch (InterruptedException e) {
log.error("[Error]");
}
log.info(builder.toString());
});
}
executorService.shutdown();
executorService.awaitTermination(1, TimeUnit.HOURS);
} }
} }