Green thread

On this page 7

A green thread is a thread of execution scheduled by a language runtime or virtual machine in user space instead of by the operating system kernel. In concurrency and programming, green threads let a program create thousands or millions of lightweight tasks that the runtime multiplexes onto a small number of kernel threads, so that switching between tasks avoids a system call and costs far less memory than a native thread.

The name comes from the Green Team at Sun Microsystems, which built the original threading library for early Java. Modern descendants include Go goroutines, Erlang processes and Java virtual threads, all of which follow the same principle: the runtime, not the kernel, decides which task runs next.

How a runtime schedules green threads

A green thread scheduler keeps a run queue of ready tasks and assigns them to carrier threads that the operating system already manages. Each green thread owns a small private stack, often starting at a few kilobytes, plus a saved register context. When a task blocks or yields, the runtime saves its context and resumes another task on the same carrier thread without entering the kernel.

The mapping between green threads and kernel threads defines the threading model. Early implementations used many-to-one (M:1) mapping, where every green thread shared a single kernel thread. Modern runtimes use many-to-many (M:N) mapping, where M green threads run on N kernel threads, usually one per CPU core, which restores true parallelism on multicore hardware.

The following table compares the three classic threading models and their practical consequences.

ModelMappingParallelismTypical example
One-to-one (1:1)Each user thread is one kernel threadFull, limited by thread countClassic POSIX threads
Many-to-one (M:1)All green threads share one kernel threadNoneEarly Java green threads
Many-to-many (M:N)M green threads on N kernel threadsFull, up to N coresGo goroutines, Java virtual threads
Threading models and their mapping to kernel threads

Preemptive and cooperative switching in green threads

A green thread runtime switches tasks either cooperatively or preemptively. In cooperative scheduling, a task gives up control only at defined yield points, such as a blocking I/O call, a channel operation or an explicit yield. In preemptive scheduling, the runtime can interrupt a running task after a time slice, typically by inserting safepoint checks or by sending a signal to the carrier thread.

Blocking operations are the central engineering problem. If a green thread calls a blocking system call, the whole carrier thread stops, and every other task assigned to it stalls. Runtimes therefore redirect I/O to non-blocking interfaces such as epoll, kqueue or io_uring, park the task, and wake it when the event loop reports readiness.

The table below summarizes how common switching triggers behave.

TriggerSwitching styleRuntime action
Network read or writeCooperativePark task, register with event loop
Channel or queue waitCooperativeMove task to the waiting list
Long CPU loopPreemptiveInterrupt at safepoint after time slice
Blocking system callHandoffDetach carrier thread and start a new one
Events that cause a green thread switch

Green threads compared with native threads and coroutines

Green threads sit between native threads and stackless coroutines. Like native threads, they have their own stacks and support ordinary sequential code without special syntax. Like coroutines, they switch in user space, which makes a context switch roughly an order of magnitude cheaper than a kernel thread switch, although exact costs depend on the runtime and hardware.

The key distinction from asynchronous programming with async and await is the absence of function coloring: a green thread can call a blocking-style API, and the runtime suspends it transparently. Stackless coroutines, by contrast, require every suspending function to be marked, which spreads through the call graph.

PropertyNative threadGreen threadStackless coroutine
SchedulerOperating system kernelLanguage runtimeEvent loop or executor
Initial stack sizeAbout 1 to 8 MB reservedA few KB, grows on demandNo separate stack
Switch costKernel transitionUser-space context swapState machine resume
Code styleSequentialSequentialMarked with async and await
Practical countThousandsHundreds of thousands to millionsMillions
Comparison of concurrency units

When to choose green threads

Green threads deliver the most value for I/O-bound workloads with high concurrency, such as network servers handling tens of thousands of simultaneous connections. Each connection can have its own task written in straightforward blocking style, while the runtime keeps the number of kernel threads small and the memory footprint predictable.

They offer little benefit for CPU-bound work, because the number of cores, not the number of tasks, limits throughput. For heavy computation, a thread pool sized to the core count or a data-parallel framework is usually the better fit.

WorkloadRecommended approachReason
Many concurrent network connectionsGreen threadsCheap tasks, simple blocking-style code
Heavy numeric computationNative thread poolThroughput limited by core count
Calls into native libraries that blockNative threads or dedicated poolBlocking calls pin carrier threads
Mixed I/O and moderate CPUGreen threads with preemptionFair scheduling keeps latency stable
Decision guide for green threads

Limitations and common pitfalls

Green threads do not remove the classic hazards of shared state. A race condition or deadlock between green threads is just as possible as between native threads, so a mutex, atomic operations or message passing are still required. Some failure modes are specific to user-space scheduling:

  • Carrier thread pinning: holding a monitor lock or calling native code can tie a green thread to its carrier and reduce available parallelism.
  • Starvation under cooperative scheduling: a tight loop without yield points can block every other task on the same carrier.
  • Thread-local storage misuse: caching expensive objects per thread multiplies memory use when millions of tasks exist.
  • Debugging and profiling gaps: operating system tools see only carrier threads, so runtime-aware tooling is needed to inspect individual tasks.

A frequent misconception is that green threads are always faster. They are cheaper to create and switch, but a single task does not run faster, and pooling green threads is usually counterproductive because creating a new one is already inexpensive.

Put simply

A green thread is a very small worker managed by the program itself rather than by the computer’s operating system. The operating system can handle only a limited number of heavy workers, but a program can juggle an enormous number of light ones.

Think of a restaurant with a few cooks and hundreds of orders. The cooks are the operating system threads. The orders are green threads. When one order waits for the oven, the cook simply picks up another order instead of standing idle. The kitchen serves far more guests with the same staff.

In practice

A team runs a chat backend that must hold about 200,000 open WebSocket connections on a single server with 16 CPU cores. With one native thread per connection, the server would reserve hundreds of gigabytes of stack address space and spend much of its time switching contexts in the kernel.

The team rewrites the connection handler so that each client runs in its own green thread, using virtual threads on a modern Java runtime. The code still reads messages with ordinary blocking calls, but the runtime parks each idle connection and runs active ones on 16 carrier threads. Memory per connection drops to a few kilobytes, and the service handles the full load on one machine with stable latency. The team also replaces a synchronized block around a database client to avoid carrier thread pinning during peak traffic.