Sitemap
Javarevisited

A humble place to learn Java and Programming better.

New Java 21 Feature : A Guide to Virtual Threads in Java!

4 min readJun 19, 2025

--

Let us be honest. Learning about threads and multithreading in Java can be confusing. Terms like “context switching” and “concurrent programming” sound difficult, especially for beginners.

But good news. Java has introduced something called Virtual Threads. This is a new feature that helps us run many tasks at the same time in a simpler and more efficient way.

In this article, I will explain everything step by step using easy words and real-life examples. Let us begin.

Recommended Courses:

If you are new to Java : Java Programming Masterclass!

If you want to become a Full Stack Developer : Full Stack Java Developer Program!

1. How Does a Computer Run Many Things Together?

Even if your laptop or computer has only one processor (CPU), you can still open many apps like Chrome, Excel, and Music Player at the same time. How is that possible?

This is done using a trick called time slicing. The CPU gives a small amount of time to each app. First Chrome, then Excel, then Music Player, and it keeps switching again and again.

This switching happens very fast, so it looks like everything is running together.

If your computer has 4 cores, then it can truly run 4 different things at the same time. Each of those 4 can also do time slicing. This is how multitasking works in computers.

2. What Are Threads in Java?

Sometimes, our program also needs to do multiple things at the same time. For example:

  • Showing a progress bar while downloading a file
  • Playing music while loading an image
  • Handling 500 users on a website at the same time

To do this, Java uses threads. A thread is like a helper worker who does one job. Your program can create many threads for different tasks.

Example: In a browser like Chrome

  • One thread downloads a file
  • One thread loads a webpage
  • One thread plays a YouTube video

This is called multithreading.

3. Old Method: Platform Threads

In normal Java, when you create a thread, the JVM asks the Operating System to create it. These threads are called platform threads.

The OS handles the thread’s memory, scheduling, and execution. But there are some problems:

  • Each thread takes around 1 MB memory for its stack
  • If you create 10,000 threads, you need about 10 GB RAM
  • Switching between threads is slow and uses extra CPU

So if your application needs to handle thousands of users or requests, platform threads are not a good idea. They waste memory and CPU even when they are just waiting.

4. The Problem with Blocking

Let us take an example. You have a Java web application that handles 10,000 requests. Each request does the following:

  • Calls a database (takes 200ms)
  • Waits for a payment API
  • Sends the response to the user

While waiting for the database or API, the thread is doing nothing useful. But still, it is using memory and CPU resources. This is a waste.

Most of these tasks are not heavy. They are just waiting for a reply. So why should we use heavy platform threads to simply wait?

5. What Are Virtual Threads?

This is where Virtual Threads come in. This is a new feature in Java under Project Loom.

Virtual threads are lightweight and created by the JVM. They are not managed by the OS. You can create thousands or even millions of virtual threads without worrying about memory.

How do they work?

  • You start a virtual thread to handle a task
  • If it needs to wait (for DB or API), the JVM parks it
  • The real OS thread is freed and can be used for other work
  • When the response comes, the JVM resumes the virtual thread

This way, virtual threads do not block OS threads. They use less memory and CPU.

6. Do I Need to Change My Code?

Good news. You do not have to change your entire code. You can write normal blocking code. No need to learn complex async or reactive programming.

To create a virtual thread, just do this:

Thread.startVirtualThread(() -> {

// Your logic here

});

That’s it. The rest is handled by the JVM.

7. Benefits of Virtual Threads

Here are the main advantages:

  • You can create 100,000+ threads easily
  • You write simple code (just like before)
  • Less memory is used (no 1 MB per thread)
  • Less CPU is wasted on switching or waiting

Your program stays clean and readable, but still handles a lot of work.

8. When to Use Virtual Threads

Virtual threads are perfect for I/O-heavy tasks like:

  • Web servers
  • Microservices
  • Apps that call many APIs or databases

If your program is doing CPU-heavy work (like image processing, video rendering, data analysis), then virtual threads may not give much benefit.

But for most modern backend applications, they are a great choice.

Final Thoughts

Virtual threads are a big improvement in Java. They make it easy to write simple and clean code that can handle thousands of tasks.

You no longer need to worry about complex thread management or async code.

So if you are building a web app, backend service, or anything that handles many users or requests, you should start using virtual threads.

Suggested Courses:

--

--

Javarevisited
Javarevisited

Published in Javarevisited

A humble place to learn Java and Programming better.

javinpaul
javinpaul

Written by javinpaul

I am Java programmer, blogger, working on Java, J2EE, UNIX, FIX Protocol. I share Java tips on http://javarevisited.blogspot.com and http://java67.com

No responses yet