Threads can be your savior or can become your biggest headache, They are the smallest processing unit that can be used in Java. There are two main types of threads, Platform threads and Virtual threads. Since JDK 19 in most of the JVM implementation threads was a thin wrapper of the Operating System thread or Platform thread.
Platform Thread is an operating system (OS) thread encapsulated in a wrapper class. On its underlying OS thread, a platform thread executes Java code, and during the platform thread’s lifetime, it captures its OS thread.
Virtual Threads is like a platform thread, it is also an instance of java.lang.Thread
but it is not tied to a specific OS thread. It was introduced with JDK 19 and makes it easier to create, manage, and troubleshoot high-throughput concurrent applications with their lightweight footprint.
What Does Virtual Thread Offer?
Memory Management
One of the challenges that can be faced using the platform threads is memory management. The memory allocated for the platform thread stack is in monolithic blocks. This will give the developer a hard and risky choice to make, under-allocating the memory and the possibility of StackOverflowException
or over-allocating and using a lot of memory. Alternatively, in Virtual threads, the stack…