Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 75dd3daef8 | |||
| c76c902319 |
@@ -1,30 +1 @@
|
|||||||
# spring-boot-threadpool-demo
|
https://doc.hutool.cn/pages/ExecutorBuilder
|
||||||
|
|
||||||
> 📚 适用于 SpringBoot 的多种线程池实现
|
|
||||||
|
|
||||||
**注意:Main 分支不包含具体实现,具体实现在项目的不同分支上**
|
|
||||||
|
|
||||||
## 分支大纲
|
|
||||||
|
|
||||||
### 分支 1: `basic-async-config`
|
|
||||||
|
|
||||||
> ✅ Completed
|
|
||||||
|
|
||||||
**主题:基础 @Async 注解方式**
|
|
||||||
- 实现简单的异步任务执行
|
|
||||||
- 配置基本的线程池参数
|
|
||||||
- 配置拒绝策略
|
|
||||||
- 测试异步方法调用
|
|
||||||
|
|
||||||
### 分支 2: `executor-service-config`
|
|
||||||
|
|
||||||
> ✅ Completed
|
|
||||||
|
|
||||||
- Java 自带线程池
|
|
||||||
- 无限队列长度
|
|
||||||
|
|
||||||
### 分支 3: `hutool-core-config`
|
|
||||||
|
|
||||||
> ✅ Completed
|
|
||||||
|
|
||||||
- 简单易用的 Hutool 工具类
|
|
||||||
@@ -23,6 +23,12 @@
|
|||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.hutool</groupId>
|
||||||
|
<artifactId>hutool-core</artifactId>
|
||||||
|
<version>5.8.31</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
|||||||
@@ -1,11 +1,42 @@
|
|||||||
package cn.hezhaohui.threadpool;
|
package cn.hezhaohui.threadpool;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import cn.hutool.core.thread.ExecutorBuilder;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
@SpringBootApplication
|
import java.util.concurrent.ArrayBlockingQueue;
|
||||||
|
import java.util.concurrent.RejectedExecutionHandler;
|
||||||
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
public class ThreadPoolApplication {
|
public class ThreadPoolApplication {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(ThreadPoolApplication.class, args);
|
ExecutorBuilder builder = new ExecutorBuilder();
|
||||||
|
ThreadPoolExecutor executor = builder
|
||||||
|
.setCorePoolSize(5)
|
||||||
|
.setMaxPoolSize(10)
|
||||||
|
.setKeepAliveTime(0)
|
||||||
|
.setHandler(new RejectedExecutionHandler(){
|
||||||
|
@Override
|
||||||
|
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
|
||||||
|
log.info(r.toString());
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.setWorkQueue(new ArrayBlockingQueue<>(100))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
for (int i = 1; i <= 1000; i++) {
|
||||||
|
executor.execute(() -> {
|
||||||
|
log.info("Hello");
|
||||||
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
executor.shutdown();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user