Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d56e55a038 | |||
| 8819b8481e | |||
| fa4d6e0549 |
@@ -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;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Slf4j
|
||||
@SpringBootApplication
|
||||
public class ThreadPoolApplication {
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(2);
|
||||
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);
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ThreadPoolApplication.class, args);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user