Project Loom - java virtual threads


Abstract

An upgrade to Java’s concurrency model.

Virtual threads:

  • not backed by OS thread
  • managed by the Java runtime
  • 200-300 bytes of metadata
  • pay-as-you go stack in the Java heap
  • M:N threading Lots of mis-usage of virtual threads:
  • thinking that virtual threads are faster threads
  • replacing platform threads with virtual threads rather than tasks with virtual threads
  • changing the ThreadFactory for a thread pool, thus pooling virtual threads
  • pinning issues and assuming that all uses of monitors must be replaced
  • using warmup issues
  • doing over complicated stuff
  • misunderstanding as to where the performance benefits come from Migration guide:
  • move to simpler blocking / synchronous code
  • migrate tasks to virtual threads, not platform threads to virtual threads
  • use semaphore or similar to limit concurrency
  • don’t cache expensive objects in thread locals
  • avoid lengthy and frequent pinning (for now anyways)
  • Adoption of virtual threads Try to move away from thread locals. Prefer ScopedValue over Thread locals for “one-way” data transmission.