Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9458d09f69 | |||
| b2ab34c344 |
@@ -1,30 +1 @@
|
|||||||
# spring-boot-threadpool-demo
|
> 注意:Java ExecutorService 使用的默认等待队列是无限大小的 `LinkedBlockingQueue`,容易 OOM,不推荐使用
|
||||||
|
|
||||||
> 📚 适用于 SpringBoot 的多种线程池实现
|
|
||||||
|
|
||||||
**注意:Main 分支不包含具体实现,具体实现在项目的不同分支上**
|
|
||||||
|
|
||||||
## 分支大纲
|
|
||||||
|
|
||||||
### 分支 1: `basic-async-config`
|
|
||||||
|
|
||||||
> ✅ Completed
|
|
||||||
|
|
||||||
**主题:基础 @Async 注解方式**
|
|
||||||
- 实现简单的异步任务执行
|
|
||||||
- 配置基本的线程池参数
|
|
||||||
- 配置拒绝策略
|
|
||||||
- 测试异步方法调用
|
|
||||||
|
|
||||||
### 分支 2: `executor-service-config`
|
|
||||||
|
|
||||||
> ✅ Completed
|
|
||||||
|
|
||||||
- Java 自带线程池
|
|
||||||
- 无限队列长度
|
|
||||||
|
|
||||||
### 分支 3: `hutool-core-config`
|
|
||||||
|
|
||||||
> ✅ Completed
|
|
||||||
|
|
||||||
- 简单易用的 Hutool 工具类
|
|
||||||
@@ -1,11 +1,34 @@
|
|||||||
package cn.hezhaohui.threadpool;
|
package cn.hezhaohui.threadpool;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
||||||
|
|
||||||
@SpringBootApplication
|
import java.util.Random;
|
||||||
|
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) {
|
public static void main(String[] args) throws InterruptedException {
|
||||||
SpringApplication.run(ThreadPoolApplication.class, args);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user