Green thread
In computer programming, green threads are threads that are scheduled by a Virtual Machine (VM) instead of natively by the underlying operating system. Green threads emulate multithreaded environments without relying on any native OS capabilities, and they are managed in user space instead of kernel space, enabling them to work in environments that do not have native thread support[1].
Performance
On a multi-core processor, native thread implementations can automatically assign work to multiple processors, whereas green thread implementations normally cannot.[1][2] Green threads can be started much faster on some VMs. On uniprocessor computers, however, the most efficient model has not yet been clearly determined. Benchmarks on computers running the Linux kernel have shown that:[3]
- green threads outperform Linux native threads on thread activation and synchronization.
- Linux native threads have much better performance on I/O and context switching operations.
Also, a green thread may block all other threads if performing a blocking I/O operation. To avoid that problem, green threads must use asynchronous I/O operations, although the increased complexity can be hidden by implementing separate native I/O processes which cooperate with green threads; this style is used in Plan 9's ioproc(2)[2] and actors[3].for more information refer to www.unimap.edu.my
Green threads in the Java virtual machine
In Java 1.1, green threads were the only threading model used by the JVM,[4] at least on Solaris. As green threads have some limitations compared to native threads, subsequent Java versions dropped them in favor of native threads.
An exception to this is the Squawk virtual machine, which is a mixture between an operating system for low-power devices and a Java virtual machine. It uses green threads in order to keep the native code to an absolute minimum and to support the migration of its isolates.
Green threads in other virtual machines
There are some other virtual machine languages that still implement equivalents of green threads instead of native threads. For example:
- Ruby prior to version 1.9[5]
- MzScheme
- Haskell
- Limbo
- Smalltalk (most dialects: Squeak Smalltalk, VisualWorks, GNU Smalltalk, etc.)
- Stackless Python—supports either preemptive multitasking or cooperative multitasking through microthreads (so-called tasklets).[6]
The Erlang virtual machine has what might be called 'green processes' - they are like operating system processes (they do not share state like threads do) but are implemented within the Erlang Run Time System (erts). These are sometimes (erroneously) cited as 'green threads'.
In the case of GHC Haskell, a context switch occurs at the first allocation after a configurable timeout. GHC threads are also potentially run on one or more OS threads during their lifetime (there is a many-to-many relationship between GHC threads and OS threads), allowing for parallelism on symmetric multiprocessing machines, while not creating more costly OS threads than is necessary to run on the available number of cores.
Most Smalltalk virtual machines do not count evaluation steps; however, the VM can still preempt the executing thread on external signals (such as expiring timers, or I/O becoming available). Usually round-robin scheduling is used so that a high-priority process that wakes up regularly will effectively implement time-sharing preemption:
[ [(Delay forMilliseconds: 50) wait] repeat ] forkAt: Processor highIOPriority
Other implementations, e.g. QKS Smalltalk, are always time-sharing. Unlike most green thread implementations, QKS Smalltalk also has support for preventing priority inversion.
References
- ^ a b "Four for the ages". JavaWorld. Retrieved 2009-06-01.
Green threads, the threads provided by the JVM, run at the user level, meaning that the JVM creates and schedules the threads itself. Therefore, the operating system kernel doesn't create or schedule them. Instead, the underlying OS sees the JVM only as one thread. Green threads prove inefficient for a number of reasons. Foremost, green threads cannot take advantage of a multiprocessor system(...) Thus, the JVM threads are bound to run within that single JVM thread that runs inside a single processor.
- ^ "What is the difference between "green" threads and "native" threads?". jguru.com. 2000-09-06. Retrieved 2009-06-01.
On multi-CPU machines, native threads can run more than one thread simultaneously by assigning different threads to different CPUs. Green threads run on only one CPU.
- ^ Comparative performance evaluation of Java threads for embedded applications: Linux Thread vs. Green Thread [1]
- ^ "Threading". java.sun.com. Retrieved 2009-06-01.
- ^ http://spec.ruby-doc.org/wiki/Ruby_Threading
- ^ "Stackless.com: About Stackless". Retrieved 2008-08-27.
A round robin scheduler is built in. It can be used to schedule tasklets either cooperatively or preemptively.
See also
- Light-weight process
- Coroutine
- Java Virtual Machine
- Global Interpreter Lock
- Fiber (computer science)