Author SHA1 Message Date
wonder 9458d09f69 🔄Update: 创建线程池 2025-11-10 21:32:43 +08:00
wonder b2ab34c344 📝Docs: 更新 README 2025-11-10 19:31:05 +08:00
wonder 6dde9aa414 🔄Update: 初始化搭建 2025-11-10 13:30:45 +08:00
wonder e5fdc84192 📝Docs: 项目大纲更新 2025-11-10 13:12:18 +08:00
4 changed files with 107 additions and 3 deletions
+4
View File
@@ -0,0 +1,4 @@
.idea
target
*.iml
*.log
+1 -3
View File
@@ -1,3 +1 @@
# spring-boot-threadpool-demo > 注意:Java ExecutorService 使用的默认等待队列是无限大小的 `LinkedBlockingQueue`,容易 OOM,不推荐使用
📚 适用于 SpringBoot 的多种线程池实现
+68
View File
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.hezhaohui</groupId>
<artifactId>spring-boot-threadpool</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.2</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Maven Compiler Plugin,用于编译 Java 代码 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.2.2</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,34 @@
package cn.hezhaohui.threadpool;
import lombok.extern.slf4j.Slf4j;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
@Slf4j
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);
}
}