Project Loom and Virtual Threads in Java

Arpit Jindal
Level Up Coding
Published in
3 min readApr 23, 2023

--

Project Loom is an ongoing OpenJDK project that aims to introduce lightweight, user-level threads in the Java Virtual Machine (JVM). These lightweight threads are called virtual threads, and they are designed to provide a more efficient and scalable alternative to the traditional operating system threads used by the JVM.

Using Virtual Threads in Java

To use virtual threads in Java, developers can use the new java.util.concurrent.Executors.newVirtualThreadExecutor() method to create a new Executor that uses virtual threads instead of traditional threads. They can then submit tasks to this Executor in the same way they would with a traditional Executor.

For example, the following code creates a new virtual Executor and submits a task to it:

Traditional Threads vs. Virtual Threads

Traditional operating system threads are heavyweight, which means they require a significant amount of system resources (such as memory and CPU time) to create and manage. This overhead can limit the number of threads that can be created and can lead to performance issues in highly concurrent applications.

Virtual threads, on the other hand, are lightweight and can be created and managed entirely within the JVM. They are not bound to an operating system thread and do not require the same amount of system resources as traditional threads. This means that virtual threads can be created and destroyed more efficiently and can scale to support a higher number of concurrent requests.

To compare the performance between traditional and virtual threads, I created 10,000 threads using both and captured the result in VisualVM:

As we can see, using traditional threads created 10,000 system threads on my machine, while the same program using virtual threads created only 24 system threads.

For detailed understanding, checkout this article:

Benefits of Virtual Threads

One of the primary benefits of virtual threads is that they enable developers to write concurrent code in a more natural way. Using virtual threads provides several other benefits:

  • Resource utilization: Traditional threads are created with a fixed amount of system resources, which can lead to inefficient use of resources in applications with varying levels of concurrency. Virtual threads, on the other hand, can dynamically adjust their resource usage based on the demands of the application, leading to more efficient resource utilization.
  • Improved Performance: Virtual threads are lightweight, which means they can be created and destroyed more quickly than traditional threads. This can improve the performance of applications that require a large number of concurrent threads.
  • Reduced Memory Overhead: Because virtual threads are lightweight, they require less memory than traditional threads. This can be beneficial for applications that need to create a large number of threads.
  • Simplified Synchronization: Because virtual threads voluntarily give up control to other threads, they can simplify synchronization compared to traditional threads. With virtual threads, there is no need for explicit locking or synchronization mechanisms to prevent race conditions.

Conclusion

Project Loom and virtual threads represent a significant shift in how developers can write concurrent code in Java. By providing a more efficient and scalable alternative to traditional operating system threads, virtual threads enable developers to write more responsive and scalable applications. While Project Loom is still in development, it is an exciting development for the Java ecosystem and one that is worth following closely.

For a deep dive into how virtual threads work, check out this article:

References

Level Up Coding

Thanks for being a part of our community! Before you go:

🚀👉 Join the Level Up talent collective and find an amazing job

--

--

Consultant Architect @Sopra Banking Software | Designing Systems At Scale | Java Developer | Quick Learner | Technophile