Cheap threads: virtual threads, goroutines and coroutines
An OS thread is a real thread the operating system schedules, and a program can only have so many. Java, Go and Kotlin each let you start far more tasks than that: when a task waits, it steps off its thread, so another task can use it. What differs is where a task steps off, and what still holds a thread.
The baseline: a pool of OS threads
Three tasks on a pool of two ordinary threads. A sleeping task keeps its thread, so task 3 waits for a whole task to finish.
Three tasks on a pool of 2 normal threads. A sleeping task keeps its thread, so task 3 waits for a whole task to finish.
Java: virtual threads
The same three tasks as virtual threads on two carrier threads. When one sleeps, it steps off its carrier, and can come back on another.
Three virtual threads share two real (carrier) threads. When one blocks, it steps off its carrier so another can run.
Go: goroutines
5,000 goroutines each wait on a network read. They park in the runtime’s netpoller, and the whole program needs fewer than 20 OS threads.
5,000 goroutines each wait on a TCP connection. They park in the netpoller: a handful of OS threads, both Ps idle.
Kotlin: coroutines
Two coroutines share the main thread. Each delay() suspends one and frees the thread for the other, so they take turns.
Two coroutines share the main thread. Each delay() suspends one and frees the thread for the other.
What still holds a thread
Each runtime can only switch at the waits it knows about.
A virtual thread that computes never steps off its carrier:
Two carriers. A computes, so it never unmounts; B and C sleep, so they share the other carrier and can move once A is done.
A goroutine in a raw blocking syscall takes its OS thread into the kernel. 1,000 of them needed over 1,000 threads, whatever GOMAXPROCS was:
1,000 goroutines block in a raw read() syscall instead. Each one takes its OS thread into the kernel, so the program needs over 1,000 threads, whatever GOMAXPROCS is.
Thread.sleep() doesn’t suspend a coroutine, it blocks the thread, so the other coroutine waits:
Thread.sleep doesn't suspend, it blocks main. Coroutine a keeps the thread for all three steps, and b waits.
Side by side
| Steps off at | Still holds it | |
|---|---|---|
| Java virtual threads | sleep() |
computing |
| Go goroutines | a network read | a raw read() |
| Kotlin coroutines | delay() |
Thread.sleep() |
Common questions
What's the difference between virtual threads, goroutines and coroutines?
All three let many tasks share a few OS threads: a task that waits steps off its thread. Java virtual threads and Go goroutines do it at ordinary blocking calls like a sleep or a network read; Kotlin coroutines do it at suspending calls like delay().
Do they run in parallel?
Up to a limit. Go runs at most GOMAXPROCS goroutines at once, and here 1,000 sleeping virtual threads shared at most 2 carrier threads; the rest wait their turn.
What still blocks the OS thread?
Work that doesn't wait the way the runtime expects: a long computation on a virtual thread, a raw blocking syscall in Go (1,000 of them needed over 1,000 OS threads), or Thread.sleep() inside a Kotlin coroutine.
Every timeline here is checked against a real program in CI. How we verify