Today I learned that Java has virtual threads introduced in Project Loom.

When I read about virtual threads, I instantly thought about go routines, which (in most basic terms) are “lightweight threads” that utilize less of the CPU’s resources than a traditional thread.

A traditional thread in Java:

  • is managed/scheduled by the OS
  • is costly to create, so things like thread pools are introduced for realloc/dealloc of threads as needed (ie. scaling your app)

A virtual thread, on the other hand:

  • is managed by the JVM
  • alloc doesn’t require a system call + free of the OS’s context switch
  • run on the “carrier thread” and DOES NOT block the carrier thread

1 caveat is

  • If the virtual threads somehow call native methods that cause blocking I/O at the OS level, that could pin the carrier thread and lead to some bad news that I need to understand more of…