Game of Loom 2: life and dead(lock) of a virtual thread by Mario Fusco

Discover the pros and cons of using virtual threads in Java, including their impact on performance, scalability, and limitations, and learn how to optimize their use in your applications.

Key takeaways
  • Virtual threads are a way to improve performance and scalability in Java applications, but they also introduce new challenges and limitations.
  • One-to-one relationship between Java threads and operating system threads is dropped, allowing for more efficient use of resources.
  • Virtual threads are lightweight, with metadata typically being 1/10 of a native thread.
  • They are not preemptive, which can lead to unfairness in CPU-bound tasks.
  • Switching between virtual threads and native threads can be expensive.
  • Thread locals can be problematic in a virtual thread context, as they are not thread-safe.
  • Some libraries, such as Jackson, are not yet optimized for virtual threads.
  • They do not have the same guarantees as native threads in terms of fairness and preemption.
  • Creating resources can be problematic in a virtual thread context, as they may be created and destroyed repeatedly.
  • Some aspects of virtual threads are not pluggable, such as the carrier thread pool.
  • Optimizing virtual threads for CPU-bound tasks requires careful consideration of the trade-offs involved.
  • There are pros and cons to using virtual threads, and it is essential to understand their limitations and use them appropriately.
  • Java 22 may include further improvements to virtual threads.