#Java 21 — Article 2: Mastering About Virtual Threads.
1)What Are Virtual Threads?
#Java 21 — Article 2: Mastering About Virtual Threads.
1)What Are Virtual Threads?
Virtual Threads are a new type of thread in Java, introduced in Java 21 (via Project Loom), that are:
- Lightweight: You can create thousands or even millions of them with very little memory overhead.
- Managed by the JVM: The Java Virtual Machine schedules and manages them for you, so they don’t use one operating system thread per Java thread (unlike “platform threads”).
2)Why Use Virtual Threads?
- 1)Lightweight: You can create thousands or even millions of them with very little memory overhead.
- 2)Easier Concurrency: You can write concurrent code (like servers or processing lots of tasks at once) using ordinary Java code, without needing to learn complex async patterns.
- 3)Highly Scalable: Because they’re so lightweight, you can run many more concurrent tasks than you could with traditional (platform) threads.
- 4)Same API : You still use familiar Java APIs like
Runnable,Thread,ExecutorService, etc.
3)Creating a Virtual Thread
Java
Thread.startVirtualThread(() ->
{
System.out.println("Hello from a virtual thread!");
});
4)Using ExecutorService to create Virtual Threads
Java
try (var executor = Executors.newVirtualThreadPerTaskExecutor())
{
executor.submit(() ->
{
// Code runs in a virtual thread
System.out.println("Virtual thread says hi!");
});
}
- With
Executors.newVirtualThreadPerTaskExecutor(), each task submitted runs in its own virtual thread. - You can run tens of thousands (or more) of these in parallel!
Example Usage
Java
try (var executor = Executors.newVirtualThreadPerTaskExecutor())
{ for (int i = 0; i < 100_000; i++)
{ executor.submit(() -> {
// Simulate work Thread.sleep(1000);
});
}
}
You can start 100,000 (or more!) tasks, and Java manages them all for you with ease.
5) How Are Virtual Threads Different from Platform Threads?

6)When Should I Use Virtual Threads?
- Server applications (e.g., handling thousands of client requests)
- Concurrent data processing
- Any program that needs to run lots of tasks at once
- Virtual Threads let you write simple, readable, blocking code yet scale to handle huge numbers of tasks.
- You don’t need to re-architect for async/non-blocking if you want scalability!
- They work seamlessly with traditional Java concurrency APIs.
7) Other documentation for Virtual Threads
Summary sentence: Virtual threads are easy-to-use, super-light threads built into Java, letting you run huge numbers of tasks at once, simply and scalably, without changing how you write your code.
What are Virtual Threads?
- Virtual threads are a new kind of lightweight thread in Java.
- You can create thousands or millions of them easily — they use much less memory and resources than traditional threads.
Why Did Java Add Virtual Threads?
- In the past, each thread in Java used a “real” operating system thread, which are limited and expensive. This limited how many tasks could run at once.
- If you wanted high performance for lots of tasks, you had to write complex, tricky asynchronous code — harder to understand, debug, and maintain.
How Do Virtual Threads Help?
- With virtual threads, you can go back to writing simple, one-thread-per-task code (often called “thread-per-request”), even for huge numbers of tasks.
- Virtual threads are fully managed by the Java runtime.
- The JVM schedules lots of virtual threads onto fewer operating system threads automatically and efficiently.
Example Usage
Java
try (var executor = Executors.newVirtualThreadPerTaskExecutor())
{
for (int i = 0; i < 100_000; i++)
{
executor.submit(() ->
{
// Simulate work
Thread.sleep(1000);
});
}
}
You can start 100,000 (or more!) tasks, and Java manages them all for you with ease.
Key Benefits
- Simple, old-style code: You can use familiar blocking calls and simple loops — no need for tricky async code.
- Highly scalable: You can easily handle tens/hundreds of thousands of concurrent tasks.
- Easy debugging: Standard Java tools (profilers, debuggers, stack traces) work with virtual threads.
- No pooling needed: Virtual threads are cheap; just create as many as you need and throw them away when done.
What Doesn’t Change
- Virtual threads don’t run faster than platform threads — they’re just lighter and more scalable.
- If you are doing heavy CPU work, you still need to think about how many “real” threads are available.
- Not all old tricks (like pooling threads or using thread-local storage for shared resources) work the same.
In Short
- Java virtual threads make it super easy to write highly concurrent programs without learning new programming styles.
- Most older Java libraries just work, and your server or app can handle much more work with less overhead!
- Create a new virtual thread wherever you’d naturally use a thread — for example, to handle each user request on a busy server.
Happy Learning! 🚀 Stay ahead in your coding journey — follow Byte Coders for the latest tutorials, updates, and tech insights. Don’t miss out — join our community today!
메타데이터
- post_id
- a93f186d7983
- slug
- java-21-article-2-mastering-about-virtual-threads-a93f186d7983
- url
- https://medium.com/@bytecoders/java-21-article-2-mastering-about-virtual-threads-a93f186d7983
- canonical_url
- https://medium.com/@bytecoders/java-21-article-2-mastering-about-virtual-threads-a93f186d7983
- author_url
- https://medium.com/@bytecoders
- status
- ok
- fetched_at
- 2026-06-26 03:39:16