Skip to content

Commit

Permalink
update virtualThread support
Browse files Browse the repository at this point in the history
  • Loading branch information
guanyang committed Jan 24, 2024
1 parent 136c033 commit 671dbe9
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.4</version>
<version>3.2.2</version>
<relativePath/>
</parent>
<groupId>org.gy.demo</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package org.gy.demo.virtualthread;

import jakarta.annotation.Resource;
import org.gy.demo.virtualthread.service.AsyncTaskExecutorService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@EnableAsync
@EnableScheduling
@RestController
@SpringBootApplication
public class VirtualthreadSampleApplication {
Expand All @@ -22,7 +28,7 @@ public static void main(String[] args) {
public Object hello1() {
Map<String, Object> map = new HashMap<>();
map.put("time", System.currentTimeMillis());
map.put("msg", "Hello World!");
map.put("msg", "Hello1 World!");
map.put("thread", Thread.currentThread().toString());
return map;
}
Expand All @@ -31,10 +37,25 @@ public Object hello1() {
public Object hello2(@PathVariable long timeMillis) throws InterruptedException {
Map<String, Object> map = new HashMap<>();
map.put("time", System.currentTimeMillis());
map.put("msg", "Hello World!");
map.put("msg", "Hello2 World!");
map.put("thread", Thread.currentThread().toString());
Thread.sleep(timeMillis);
return map;
}


@Resource
private AsyncTaskExecutorService asyncTaskExecutorService;

@GetMapping("/hello/async")
public Object hello3() throws InterruptedException {
//异步调用
Map<String, Object> map = new HashMap<>();
map.put("time", System.currentTimeMillis());
map.put("msg", "Hello3 World!");
map.put("thread", Thread.currentThread().toString());
asyncTaskExecutorService.run();
return map;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.gy.demo.virtualthread.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class AsyncTaskExecutorService {

@Async
public void run() {
log.info("Async task method has been called {}", Thread.currentThread());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.gy.demo.virtualthread.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class SchedulerService {

@Scheduled(fixedDelayString = "15000")
public void run() {
log.info("Scheduled method has been called {}", Thread.currentThread());
}
}
5 changes: 4 additions & 1 deletion virtualthread-sample/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
spring:
#配置virtual表示启用虚拟线程,非virtual表示不启用,可以通过环境变量SPRING_EXECUTOR指定
executor: ${SPRING_EXECUTOR:virtual}
executor: ${SPRING_EXECUTOR:none}
threads:
virtual:
enabled: true

0 comments on commit 671dbe9

Please sign in to comment.