butcher java virtual threads like a pro

Abstract

  • Thread.sleep not a real world CPU bound work, so cannot be used to benchmark virtual threads
  • corner stone: Virtual threads
    • not reinvent the wheel, keep threads concept
    • cheap to start
    • carried by platform / OS threads under the hood
    • unmounting carrier threads when waiting, e.g. IO
    • always daemons, with normal prio
  • car metaphor
    • your own car is a platform thread
    • a taxi is a virtual thread
  • virtual threads will not magically squeeze more juice from your CPU!
    • but your application will scale better
  • what not to do with virtual threads
    • reuse
    • pool
    • pin
      • -Djdk.tracePinnedThreads=full and JFR events to detect pinning issues
  • logging is IO operation, so it can play on the virtual thread performance
    • when monitoring virtual threads, do not use IO operation like logs
  • scoped values
    • one-way immutable ThreadLocals + bounded lifetime
    • simplified reasoning and improved performance
    • work nicely with structured concurrenvy
    • don’t use mutable variables
  • structured concurrency
    • subtasks should be interruptible