What is Virtual Thread in Java?
The virtual thread is introduced in JDK 21 that helps to improve the application availability, throughput, and code quality on top of reducing the memory consumption. The virtual thread concept has gained a lot of attention in recent times, and much more programming languages are trying to update their thread libraries to support the virtual threads.
Different types of threads
Before the virtual thread introduced in Java, we have two types of thread as following:
- Kernel Thread
- Kernel threads, also known as OS threads, are managed and scheduled by the operating system.
- Platform Thread
- Java Platform threads are associated with 1-to-1 with the OS thread in order to perform their execution.
When we create a thread in Java, it gets maps to the platform thread, which is further linked to an OS or kernel thread.
Why Virtual thread is introduced in Java?
In order to understand why Java developers introduce the virtual threads, let first understand the problem with a normal multi-thread environment with the help of the following scenario:
- First, a request is coming to the application server where we have deployed our application.
- The application server has its own pool of platform threads; say one of the pool threads started executing the request and took it to the application.
- The application is performing some business operations and maybe making some calls to the backed database, and it may take some time to complete its execution.
- Till the time the application is performing the backed operation, the pool thread has to wait, and it is linked to the platform thread, and the platform thread also has to wait, and similarly, the platform thread is linked to the OS thread, which means the OS thread will be waiting too.
- We can analyze that the thread is working only when it is processing the request; apart from this, it is sitting ideal and waiting.
- Because of linking of platform thread to the OS thread, the OS thread is also waiting and will not be able to perform any other operation.
How does a virtual thread work in the above scenario?
Let’s try to understand the above scenario with the help of a virtual thread.
- As earlier, a request goes to the application server.
- Once the request hits the application server, it is not directly picked by the platform thread to present the thread pool; instead, a virtual thread starts processing that request.
- After the request goes to the backup for business operations, instead of waiting, the virtual thread will get detached from the platform thread and go back to the heap memory.
- Once it gets detached from the platform thread, the platform thread also gets detached from the OS thread, and this way the OS thread becomes free to perform any other operation.
Why we should use Virtual thread?
The virtual threads are lightweight and have high throughput and are easy to use in multi threaded environment.
Why virtual threads are lightweight?
A virtual thread maps to a platform thread only when it is actually working or executing any task; otherwise, it will sit back into the heap memory. And the OS thread is not allocated to the platform thread until any task needs to be executed; this is because the virtual threads are considered to be lightweight. We can create a million virtual threads, but we cannot create a million platform threads.