Reputation: 2772
In my Spring Boot 3.4 and Java 21 based application, I have enabled virtual threads via:
spring.threads.virtual.enabled=true
I am using Spring Boot 3.4:
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.0</version>
I now understand what I was doing wrong: Need to use a StructuredTaskScope
. Anyway, if you are still interested in my initial question, keep reading.
When I run my code, I see no structured concurrency! Instead, it's happening sequentially. Here is the code:
public String testFlow() {
final Instant start = Instant.now();
// Step 1: Generate a random expression
String expression = generateExpression();
log.info("Generated expression: {}", expression);
//dummy calls to check if virtual threads make it parallel
String dummy1 = generateExpression();
String dummy2 = generateExpression();
...
private String generateExpression() {
log.info("Current Thread: {}", Thread.currentThread().getName());
return restClient
.get()
.uri("/generate")
.retrieve()
.body(String.class);
}
The Current Thread
log prints Current Thread: tomcat-handler-13
also, whereas I was expecting A virtual thread to be printed.
Complete code can be found on GitHub.
Also, below is from my distributed trace of the above flow. Here, you can see the generator
being called 3 times, and executes in a sequential manner instead of parallel.
How can this be happening?
Upvotes: 1
Views: 219