Virtual Threads
A virtual thread is an instance of java.lang.Thread, independent of any OS thread, is used to run programs. The Java runtime suspends the virtual thread until it resumes when the code calls a blocked I/O operation. Virtual threads have a limited call stack and can only execute one HTTP client call or JDBC query. They are suitable for delayed operations, but not for extended CPU-intensive tasks.
Syntax of Virtual Threads:
Thread virtualThread = Thread.ofVirtual().start(() -> {
// Code to be executed by the virtual thread
});
Virtual Threads in Java
In Java, Virtual threads are now supported by the Java Platform. Virtual threads are lightweight threads that greatly minimize the effort required to create, operate, and manage high volumes systems that are concurrent. As a result, they are more efficient and scalable than standard platform threads.
A thread is the smallest processing unit that can be scheduled. It operates concurrently with, and mostly independently of other units of this type. It’s an instance of java.lang.Thread.
There are two kinds of threads, platform threads and virtual threads:
- Platform Threads
- Virtual Threads