Working with Virtual Thread - up1/course-springboot-2024 GitHub Wiki
Working with Virtual Thread
- Start from Java 19 (preview feature)
1. Enable Virtual Thread in application.properties
spring.threads.virtual.enabled=true
2. Sample app :: with blocking tasks
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class VirtualThreadController {
@GetMapping("/blocking-task")
public String performBlockingTask() throws InterruptedException {
// Simulate a blocking operation, e.g., a network call or database query
Thread.sleep(2000); // Simulate a 2-second delay
return "Blocking task completed by: " + Thread.currentThread().getName();
}
}