Enhancements in Java 21 for Concurrency — A Leap Toward Simplicity with Virtual Threads

๐Ÿงต Enhancements in Java 21 for Concurrency — A Leap Toward Simplicity with Virtual Threads

๐Ÿ”— Related Reads:
Enhancements in Java 5 for Concurrency
Enhancements in Java 8 for Concurrency

Java has evolved significantly over the last two decades—from manual thread management in Java 5, to asynchronous computation via CompletableFuture in Java 8, and now, in Java 21, a major shift with Virtual Threads under Project Loom.

Let’s explore how Java 21 revolutionizes concurrency by making it simpler and more scalable without requiring complicated constructs.

๐Ÿ“Œ What Are Virtual Threads?

Virtual Threads are lightweight, OS-independent threads managed by the JVM rather than the operating system. Unlike traditional (platform) threads, millions of virtual threads can run concurrently with minimal memory and scheduling overhead.

๐Ÿ”„ Evolution at a Glance

FeatureJava 5Java 8Java 21
Task Management Executors, Thread pools CompletableFuture Virtual Threads (Thread.ofVirtual)
Thread Creation new Thread() ExecutorService + lambda Thread.ofVirtual().start(...)
Async Composition Manual with Future thenApply() chains Structured Concurrency (Incubator)
Memory Overhead High (~1MB/thread) Moderate Low (~few KB/thread)
Scalable Concurrency Difficult Moderate Simple and Highly Scalable

⚙️ How to Create Virtual Threads (Java 21)

✅ Using Thread.ofVirtual().start(...)

public class VirtualThreadDemo {
    public static void main(String[] args) {
        Runnable task = () -> System.out.println("Running in: " + Thread.currentThread());
        Thread vThread = Thread.ofVirtual().start(task);
    }
}

✅ Using ExecutorService with Virtual Threads

ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();
executor.submit(() -> {
    Thread.sleep(1000);
    System.out.println("Executed by: " + Thread.currentThread());
});
executor.shutdown();

☝️ This behaves like a cached thread pool — but with millions of threads.

๐Ÿ“š Example: Parallel HTTP Calls Using Virtual Threads

ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();

List<Callable<String>> tasks = List.of(
    () -> fetchDataFromService("http://service1.com"),
    () -> fetchDataFromService("http://service2.com"),
    () -> fetchDataFromService("http://service3.com")
);

List<Future<String>> results = executor.invokeAll(tasks);

for (Future<String> result : results) {
    System.out.println(result.get());
}

executor.shutdown();

This mimics parallel I/O-bound tasks (like REST API calls) without needing CompletableFuture or reactive frameworks like WebFlux.

๐Ÿ” When to Prefer Virtual Threads?

ScenarioUse Virtual Threads?Alternative
I/O-bound (REST, DB)✅ YesCompletableFuture, WebFlux
CPU-bound parallel tasks❌ Not IdealForkJoinPool, parallel streams
UI/main thread tasks❌ AvoidUse Platform thread

๐Ÿง  Structured Concurrency (Preview Feature)

try (var scope = StructuredTaskScope.ShutdownOnFailure()) {
    Future<String> task1 = scope.fork(() -> callServiceA());
    Future<String> task2 = scope.fork(() -> callServiceB());

    scope.join();               // Wait for all
    scope.throwIfFailed();      // Rethrow exceptions if any

    String result = task1.result() + task2.result();
    System.out.println("Result = " + result);
}

๐Ÿ’ฌ Comparison: Java 5 vs 8 vs 21

TaskJava 5Java 8Java 21
Thread Creation new Thread() or Executor CompletableFuture.supplyAsync() Thread.ofVirtual().start()
Composition of Tasks Manual with Future .thenApply() chains StructuredTaskScope, invokeAll()
Waiting for Completion future.get() .join() or .get() Blocking (lightweight)
Massive Concurrency OS thread limit Complex with tuning Trivial with millions of threads

๐Ÿš€ Final Thoughts

Virtual Threads in Java 21 eliminate the complexity of building scalable concurrent applications by:

  • Maintaining a synchronous coding style
  • Providing non-blocking scalability
  • Making thread-per-task practical again

You can now write code that is simple, clean, and scalable, without adopting complex reactive paradigms.

๐Ÿ”— Don’t miss our previous concurrency articles:

Comments