java virtual threads vs kotlin coroutines

Abstract

Virtual thread suspension:

  • blocking methods in java.* package
    • e.g. I/O calls, reading/writing streams
  • except
    • synchronized blocks
    • native methods / foreign functions

Virtual threads use cases:

  • thread-per-request style server applications
  • highly concurrent, I/O bound code
    • not CPU-bound

Java virtual threads vs Kotlin coroutines

  • virtual threads vs suspending functions
  • structured concurrency vs structured concurrency
  • scoped values vs coroutine context

The differences lay in the fundamentals: implicit vs explicit

  • virtual threads:
    • transparently implemented, minimal code changes needed
    • implicitly suspending methods
  • kotlin coroutines:
    • different coding style
    • explicitly suspending functions

Use cases:

  • kotlin coroutines
    • async computations
    • async UI
    • futures
    • (sequence) generators
    • channel-based concurrency
    • actor-based concurrency
    • etc
  • java virtual threads
    • I/O bound JVM applications