Member-only story
Beginner’s Guide to Java Virtual Threads with Spring Boot
Virtual threads are lightweight threads that reduce the effort of maintenance and improve the throughput of concurrent applications. This was proposed as a preview feature in JDK 19 and it is planned to be included in JDK 21 release.
The goal of Java’s concurrent programming is to distribute operations to several threads running at the same time and thus improve performance. But these threads are often a bottleneck. Formerly, Java threads always corresponded to operating system threads. These are very resource-intensive and therefore very limited in number. Starting a few thousand threads simultaneously may quickly result in an OutOfMemory error. Alternatively, if the number of threads is limited via a thread pool, the application may no longer respond as desired due to the large number of blocking operations.
A platform thread is managed by the operating system, while a virtual thread is managed by a virtual machine. Virtual threads allow the implementation of concurrent processing of tasks executed on a very large number of threads. This can help improve the efficiency of concurrent tasks.
Let’s create a simple Spring Boot application and explore virtual threads.