Definition
Green Thread
A green thread is a thread scheduled and managed by a runtime environment or virtual machine in user space, not by the kernel. The OS sees only a single standard thread containing all green threads.
Named after the “Green Team” at Sun Microsystems, who designed the original threading library for Java.
Cooperative Scheduling
Explicit Yielding
The code calls a function like
yield()to pass control to the runtime’s scheduler.
Implicit Yielding
The thread performs a potentially slow operation (e.g., network I/O). The runtime intercepts this, starts the non-blocking operation in the background, and schedules another green thread.
Context Switch
Lightweight Switching
A context switch between green threads is extremely lightweight. The runtime saves and restores CPU state (instruction pointer, registers) within the same process and OS thread, avoiding costly kernel mode transitions required for native thread switches.