网站导航怎么设置,全国域名备案查询,网站建设代码好难啊,网站域名所有权 查询jd19支持虚拟线程#xff0c;虚拟线程是轻量级的线程#xff0c;它们不与操作系统线程绑定#xff0c;而是由 JVM 来管理。它们适用于“每个请求一个线程”的编程风格#xff0c;同时没有操作系统线程的限制。我们能够创建数以百万计的虚拟线程而不会影响吞吐。
做个 spri…jd19支持虚拟线程虚拟线程是轻量级的线程它们不与操作系统线程绑定而是由 JVM 来管理。它们适用于“每个请求一个线程”的编程风格同时没有操作系统线程的限制。我们能够创建数以百万计的虚拟线程而不会影响吞吐。
做个 springboot demo 尝试一下。
环境
jdk19gradle 7.6.1IntelliJ IDEA 2022.2.4
build.gradle
plugins {id javaid org.springframework.boot version 3.0.3id io.spring.dependency-management version 1.1.0
}group com.example
version 0.0.1-SNAPSHOT
sourceCompatibility 19configurations {compileOnly {extendsFrom annotationProcessor}
}repositories {mavenCentral()
}dependencies {implementation org.springframework.boot:spring-boot-startercompileOnly org.projectlombok:lombokdevelopmentOnly org.springframework.boot:spring-boot-devtoolsannotationProcessor org.projectlombok:lomboktestImplementation org.springframework.boot:spring-boot-starter-test
}tasks.named(test) {useJUnitPlatform()
}tasks.withType(JavaCompile) {options.compilerArgs --enable-preview
}
多线程demo
写一段多线程调用的代码程序入口
package com.example.myvirtualthreaddemo;import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.core.task.support.TaskExecutorAdapter;
import org.springframework.scheduling.annotation.EnableAsync;import java.util.concurrent.Executors;EnableAsync
SpringBootApplication
public class MyVirtualThreadDemoApplication {public static void main(String[] args) {SpringApplication.run(MyVirtualThreadDemoApplication.class, args);}BeanCommandLineRunner commandLineRunner(AsyncService asyncService){return args - {for(int i 0; i 100; i){asyncService.fun(i);}};}
}
package com.example.myvirtualthreaddemo;import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;Service
Slf4j
public class AsyncService {Asyncpublic void fun(int i) {log.info(fun:{}, i);}
}运行结果使用平台线程默认8个 使用虚拟线程
在MyVirtualThreadDemoApplication添加以下代码块
Bean(TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME)public AsyncTaskExecutor asyncTaskExecutor(){return new TaskExecutorAdapter(Executors.newVirtualThreadPerTaskExecutor());}运行结果使用虚拟线程
遇到的一些问题
请使用 --enable-preview 以启用预览 API
解决办法
build.gradle里添加以下配置
tasks.withType(JavaCompile) {options.compilerArgs --enable-preview
}设置vm选项如图