Compare

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.

JavaThe same tasks on 2 platform threads

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.

0123456789executor.close()main: continuespool-1-thread-1: task 1 computepool-1-thread-1: task 1 sleep(200)sleeppool-1-thread-1: task 1 computepool-1-thread-1: task 3 computepool-1-thread-1: task 3 sleep(200)sleeppool-1-thread-1: task 3 computepool-1-thread-2: task 2 computepool-1-thread-2: task 2 sleep(200)sleeppool-1-thread-2: task 2 compute
Play it step by step

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.

JavaVirtual threads

Three virtual threads share two real (carrier) threads. When one blocks, it steps off its carrier so another can run.

0123456executor.close()main: continuescarrier-1: vt-1 computevt-1carrier-1: vt-3 computevt-3carrier-1: vt-2 computevt-2carrier-1: vt-3 computevt-3carrier-2: vt-2 computevt-2carrier-2: vt-1 computevt-1
Play it step by step

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.

GoThousands of goroutines: waiting on the network vs computing

5,000 goroutines each wait on a TCP connection. They park in the netpoller: a handful of OS threads, both Ps idle.

012345678910P0: main: dial 5,000 connectionsdialP1: readers: Read(), parkparkP0: readers: Read(), parkpark5,000 readers parkedP0: main: print the countsprint
Play it step by step

Kotlin: coroutines

Two coroutines share the main thread. Each delay() suspends one and frees the thread for the other, so they take turns.

KotlinKotlin coroutines take turns on one thread

Two coroutines share the main thread. Each delay() suspends one and frees the thread for the other.

012345678runBlocking: launch a, bwaits for its childrencoroutine a: step 1coroutine a: delay: suspendeddelaycoroutine a: step 2coroutine a: delay: suspendeddelaycoroutine a: step 3delaycoroutine b: ready, waits for mainreadycoroutine b: step 1coroutine b: delay: suspendeddelaycoroutine b: step 2coroutine b: delay: suspendeddelaycoroutine b: step 3delay
Play it step by step

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:

JavaA computing virtual thread holds 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.

012345678910111213141516executor.close()main: continuesendA1: spinA2: spinA3: spincarrier-1: A doneAcarrier-1: C done: moved hereCC1B1B2C2B3C3carrier-2: B doneB
Play it step by step

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:

GoBlocking syscalls each hold an OS thread

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.

012345678910P0: main: start 1,000 readersstartP1: readers: read() enters the kernelread()1,000 threads, one per blocked readP0: main: print the countsprint
Play it step by step

Thread.sleep() doesn’t suspend a coroutine, it blocks the thread, so the other coroutine waits:

KotlinThread.sleep blocks every coroutine on the thread

Thread.sleep doesn't suspend, it blocks main. Coroutine a keeps the thread for all three steps, and b waits.

01234567891011121314runBlocking: launch a, bwaits for its childrencoroutine a: step 1coroutine a: Thread.sleep: blocks mainsleepcoroutine a: step 2coroutine a: Thread.sleep: blocks mainsleepcoroutine a: step 3coroutine a: Thread.sleepsleepready, but main is blockedcoroutine b: step 1coroutine b: Thread.sleepsleepcoroutine b: step 2coroutine b: Thread.sleepsleepcoroutine b: step 3coroutine b: Thread.sleepsleep
Play it step by step

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