SpringBoot3 使用虚拟线程

目录

环境

说明

application.properties

pom.xml

[Controller Java](#Controller Java)

验证


环境

|------------|----------------------|
| Java | Java: graalvm-jdk-21 |
| Springboot | 3.3.1 |

说明

springboot3.x 打开虚拟线程非常简单,只需添加一行配置信息即可。

spring.threads.virtual.enabled = true

application.properties

XML 复制代码
spring.threads.virtual.enabled = true

pom.xml

XML 复制代码
<?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>org.example</groupId>
    <artifactId>boot3-01-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.3.1</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.graalvm.buildtools</groupId>
                <artifactId>native-maven-plugin</artifactId>
                <extensions>true</extensions>
                <executions>
                    <execution>
                        <id>build-native</id>
                        <goals>
                            <goal>compile-no-fork</goal>
                        </goals>
                        <phase>package</phase>
                    </execution>
                    <execution>
                        <id>test-native</id>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <phase>test</phase>
                    </execution>
                </executions>
                <configuration>
                    <imageName>${project.artifactId}</imageName>
                    <mainClass>com.hb.MyApplication</mainClass>
                    <buildArgs>--no-fallback</buildArgs>
                    <agent>
                        <enabled>true</enabled>
                    </agent>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>21</source>
                    <target>21</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Controller Java

java 复制代码
package com.hb.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {

    @GetMapping("/hello")
    public String hello() {
        System.out.printf("当前执行线程: %s%n", Thread.currentThread()) ;
        return "HELLO WORLD";
    }
}

验证

访问: http://localhost:8080/hello

控制台输出:

当前执行线程: VirtualThread#40,tomcat-handler-0/runnable@ForkJoinPool-1-worker-1

当前执行线程: VirtualThread#46,tomcat-handler-1/runnable@ForkJoinPool-1-worker-1

当前执行线程: VirtualThread#47,tomcat-handler-2/runnable@ForkJoinPool-1-worker-1

当前执行线程: VirtualThread#48,tomcat-handler-3/runnable@ForkJoinPool-1-worker-1

当前执行线程: VirtualThread#49,tomcat-handler-4/runnable@ForkJoinPool-1-worker-1

当前执行线程: VirtualThread#50,tomcat-handler-5/runnable@ForkJoinPool-1-worker-1

当前执行线程: VirtualThread#51,tomcat-handler-6/runnable@ForkJoinPool-1-worker-1

当设置成 spring.threads.virtual.enabled = false

当前执行线程: Thread#37,http-nio-8080-exec-1,5,main

当前执行线程: Thread#39,http-nio-8080-exec-3,5,main

当前执行线程: Thread#38,http-nio-8080-exec-2,5,main

当前执行线程: Thread#40,http-nio-8080-exec-4,5,main

当前执行线程: Thread#41,http-nio-8080-exec-5,5,main

当前执行线程: Thread#42,http-nio-8080-exec-6,5,main

当前执行线程: Thread#43,http-nio-8080-exec-7,5,main

当前执行线程: Thread#44,http-nio-8080-exec-8,5,main

当前执行线程: Thread#45,http-nio-8080-exec-9,5,main

相关推荐
SL-staff13 分钟前
智慧园区2000+设备统一管理:JVS-IoT如何降低运维成本40%
java·开发语言·物联网·智慧园区·设备管理·运维优化·jvs-iot
咖啡八杯1 小时前
GoF设计模式——模板方法模式
java·后端·spring·设计模式
爱笑的源码基地1 小时前
一款全开源的信创云PACS影像云平台解决方案,支持多模态医学影像处理(CT/MR/DR/超声/病理等)
java·源码·国产化·pacs·云影像·区域pacs
我命由我123452 小时前
Android 开发问题:ClickableSpan 的点击事件没有生效
java·java-ee·android studio·android jetpack·android-studio·android runtime
2zcode2 小时前
基于MATLAB图像处理的饮料瓶灌装液位检测系统设计与实现
开发语言·图像处理·matlab
万亿少女的梦1682 小时前
基于Python的高考志愿填报辅助系统设计与实现
java·spring boot·python·mysql·vue
微信开发api-视频号协议2 小时前
企业微信外部群开发自动化实践过程
java·前端·微信·自动化·企业微信·ipad
犀利豆3 小时前
#AI in Harness(一)
java·ai编程
极***涯3 小时前
深度拆解:多源BFS路径重构难题——跳出单层遍历误区,搞定高阶最短路径全量解
java·重构·宽度优先
必须得开心呀3 小时前
QT解决中文乱码问题
开发语言·qt