用springboot3.4.1 maven为例子
1 pom
···省略部分信息,仅保留核心部分
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2 开启
properties
spring.threads.virtual.enabled=true
yaml
spring:
threads:
virtual:
enabled: true
3.验证
3.1实现一个简单get接口获取当前线程
@GetMapping("/name")
public String toThread() {
return Thread.currentThread().toString();
}
3.2 返回内容
**[#93,tomcat-handler-0]/runnable@ForkJoinPool-1-worker-1
3.3 关闭虚拟线程再次调用返回内容
Thread[#68,http-nio-8080-exec-3,5,main]
4 验证undertow
4.1 修改pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
4.2 开启虚拟线程返回
**[#115,undertow-0]/runnable@ForkJoinPool-1-worker-1
4.3 关闭虚拟线程返回
Thread[#106,XNIO-1 task-2,5,main]