How to Use Virtual Threads in Java 21 for High-Concurrency Applications
Java 21 makes virtual threads a practical choice for applications that spend much of their time waiting on databases, HTTP services, queues, or file systems. They use the familiar Thread API while allowing a Java process to manage a far larger number of concurrent tasks than a platform-thread-per-request design.
For Australian teams building banking portals, parcel tracking systems, booking platforms, or SaaS products for customers in Sydney, Melbourne, and Brisbane, this can improve responsiveness without requiring a complete rewrite. Virtual threads are especially useful when workloads involve many blocked I/O operations, such as calls to payment providers or services hosted across Australian cloud regions.
Understanding Virtual Threads
A virtual thread is managed by the Java runtime rather than being permanently tied to an operating-system thread. When it reaches a blocking operation, the JVM can suspend it and use the underlying platform thread for other work. This makes blocking code considerably cheaper at high concurrency.
The API is deliberately straightforward:
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
for (int customerId : customerIds) {
executor.submit(() -> loadCustomer(customerId));
}
}
Each submitted task receives its own virtual thread. The executor creates threads as needed and closes after all submitted tasks finish. This approach is often easier to maintain than converting every operation to callbacks or reactive pipelines.
Virtual threads are a strong fit for request-per-task workloads. They are less useful for CPU-heavy calculations, where the number of tasks should remain close to the number of available processor cores.
Creating a Request-Oriented Service
Spring Boot applications can use virtual threads by enabling the relevant property:
spring.threads.virtual.enabled=true
This allows supported Spring components and embedded servers to use virtual threads for request handling. A controller can then call a repository, an external API, and an authentication service using ordinary sequential Java code.
For a more explicit design, use an executor:
@Service
public class OrderService {
public OrderSummary fetchSummary(long orderId) throws Exception {
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
Future<Order> order = executor.submit(() -> orderRepository.findById(orderId));
Future<PaymentStatus> payment =
executor.submit(() -> paymentClient.status(orderId));
return new OrderSummary(order.get(), payment.get());
}
}
}
The blocking calls remain readable, while unrelated operations run concurrently. In an Australian e-commerce system, this can help coordinate an order database, an Australia Post integration, and a payment gateway without creating a large platform-thread pool.
Choosing the Right Concurrency Model
Virtual threads do not eliminate the need for capacity planning. A service may accept thousands of concurrent requests, yet its PostgreSQL connection pool may support only 50 active connections. The database, downstream APIs, rate limits, and memory budget still define the true throughput.
| Workload or Approach | Best Fit | Main Advantage | Important Limitation |
|---|---|---|---|
| Platform thread per task | Small or bounded workloads | Familiar operating-system threads | High memory and scheduling cost |
| Virtual thread per task | I/O-heavy, high-concurrency services | Simple blocking code at large scale | Does not make CPU work faster |
| Reactive programming | Streaming and carefully optimised pipelines | Fine-grained resource control | More complex application flow |
| Fixed thread pool | External systems with strict limits | Protects a constrained dependency | Tasks may queue under load |
A virtual thread should not be treated as an unlimited database connection. Use semaphores, bounded queues, or client-side limits when a dependency has a fixed capacity. For example, an Australian bank API may impose request quotas that require deliberate throttling even if the application can create thousands of virtual threads.
Avoiding Pinning And Resource Problems
Pinning occurs when a virtual thread cannot be unmounted from its carrier platform thread. Long-running synchronised blocks and some native or foreign-function operations can cause this behaviour. Short synchronisation sections are usually acceptable, but locks around slow network or database calls should be redesigned.
Use modern concurrency primitives and keep blocking work outside critical sections. Java Flight Recorder and runtime diagnostics can help identify unexpected pinning and long waits. Test with realistic traffic rather than relying only on local development results from a laptop in Perth or Adelaide.
Practical safeguards include:
- Keep database pools and HTTP connection pools deliberately bounded.
- Avoid holding
synchronizedlocks while calling remote services. - Set timeouts for every outbound HTTP and database operation.
- Propagate cancellation when a client disconnects.
- Monitor heap usage, carrier-thread utilisation, and task latency.
Thread-local state also deserves attention. Thousands of virtual threads can make per-thread caches or large diagnostic objects expensive. Prefer scoped, request-level data and use context propagation carefully when working with logging, tracing, or security identities.
Testing And Operating In Production
Load tests should model the traffic pattern of the real product. A Melbourne ticketing service may receive sharp bursts when an event goes on sale, while a regional health platform may experience slower links and longer-lived requests. Test both steady concurrency and sudden spikes, including timeouts from remote dependencies.
Capture request latency percentiles, queue time, error rates, database pool saturation, and outbound API failures. A service that handles 20,000 virtual threads may still perform poorly if every task waits for one exhausted connection pool. Observability should show where tasks spend their time rather than simply reporting thread counts.
Useful practices for production readiness include:
- Run on Java 21 or a later supported release with consistent JVM settings.
- Use structured concurrency patterns where task lifecycles must remain related.
- Configure bounded external resources independently of thread count.
- Review libraries for blocking, native, or pinning-prone behaviour.
- Validate graceful shutdown during deployments and autoscaling events.
For teams serving customers across Australia, also account for cloud-region placement, cross-region latency, and local privacy obligations. Keeping services close to users in Sydney or Melbourne can reduce network delay, while careful logging and data handling support compliance expectations for customer and payment information.
Applying Virtual Threads Safely
Start with a service that has clear I/O waits, such as an API gateway, authentication flow, JDBC-backed endpoint, or integration service. Measure its current latency and throughput, enable virtual threads, then compare the same workload under controlled conditions.
Do not replace every executor automatically. A CPU-bound image-processing job, a memory-intensive report, or a tightly constrained legacy integration may need a fixed pool. Virtual threads provide a scalable concurrency model, not a universal performance switch.
A sensible rollout includes a small production canary, dashboards for dependency saturation, and a rollback path. When the workload is dominated by waiting and the surrounding resources are properly bounded, Java 21 virtual threads can deliver high concurrency with code that remains familiar to Java developers.