Green thread

Last updated

In computer programming, a green thread is a thread that is scheduled by a runtime library or virtual machine (VM) instead of natively by the underlying operating system (OS). Green threads emulate multithreaded environments without relying on any native OS abilities, 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]

Contents

Etymology

Green threads refers to the name of the original thread library for Java programming language (that was released in version 1.1 and then Green threads were abandoned in version 1.3 to native threads). It was designed by The Green Team at Sun Microsystems. [2]

History

Green threads were briefly available in Java between 1997 and 2000.

Green threads share a single operating system thread through co-operative concurrency and can therefore not achieve parallelism performance gains like operating system threads. The main benefit of coroutines and green threads is ease of implementation.

Performance

On a multi-core processor, native thread implementations can automatically assign work to multiple processors, whereas green thread implementations normally cannot. [1] [3] 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 version 2.2 (released in 1999) have shown that: [4]

When a green thread executes a blocking system call, not only is that thread blocked, but all of the threads within the process are blocked. [5] To avoid that problem, green threads must use non-blocking I/O or asynchronous I/O operations, although the increased complexity on the user side can be reduced if the virtual machine implementing the green threads spawns specific I/O processes (hidden to the user) for each I/O operation.[ citation needed ]

There are also mechanisms which allow use of native threads and reduce the overhead of thread activation and synchronization:

Green threads in the Java Virtual Machine

In Java 1.1, green threads were the only threading model used by the Java virtual machine (JVM), [8] at least on Solaris. As green threads have some limitations compared to native threads, subsequent Java versions dropped them in favor of native threads. [9] [10]

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 to minimize the use of native code, and to support migrating its isolates.

Kilim [11] [12] and Quasar [13] [14] are open-source projects which implement green threads on later versions of the JVM by modifying the Java bytecode produced by the Java compiler (Quasar also supports Kotlin and Clojure).

Green threads in other languages

There are some other programming languages that implement equivalents of green threads instead of native threads. Examples:

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 termed green threads, but have significant differences[ clarification needed ] from standard green threads.[ citation needed ]

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 needed to run on the available number of cores.[ citation needed ]

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:

 [     [(DelayforMilliseconds:50) wait] repeat  ] forkAt:ProcessorhighIOPriority

Other implementations, e.g., QKS Smalltalk, are always time-sharing. Unlike most green thread implementations, QKS also supports preventing priority inversion.

Differences to virtual threads in the Java Virtual Machine

Virtual threads were introduced as a preview feature in Java 19 [29] and stabilized in Java 21. [30] Important differences between virtual threads and green threads are:

See also

Related Research Articles

<span class="mw-page-title-main">Java (programming language)</span> Object-oriented programming language

Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let programmers write once, run anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without the need to recompile. Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of the underlying computer architecture. The syntax of Java is similar to C and C++, but has fewer low-level facilities than either of them. The Java runtime provides dynamic capabilities that are typically not available in traditional compiled languages.

<span class="mw-page-title-main">Java virtual machine</span> Virtual machine that runs Java programs

A Java virtual machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode. The JVM is detailed by a specification that formally describes what is required in a JVM implementation. Having a specification ensures interoperability of Java programs across different implementations so that program authors using the Java Development Kit (JDK) need not worry about idiosyncrasies of the underlying hardware platform.

<span class="mw-page-title-main">Virtual machine</span> Software that emulates an entire computer

In computing, a virtual machine (VM) is the virtualization or emulation of a computer system. Virtual machines are based on computer architectures and provide the functionality of a physical computer. Their implementations may involve specialized hardware, software, or a combination of the two. Virtual machines differ and are organized by their function, shown here:

<span class="mw-page-title-main">Thread (computing)</span> Smallest sequence of programmed instructions that can be managed independently by a scheduler

In computer science, a thread of execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler, which is typically a part of the operating system. In many cases, a thread is a component of a process.

Bytecode is a form of instruction set designed for efficient execution by a software interpreter. Unlike human-readable source code, bytecodes are compact numeric codes, constants, and references that encode the result of compiler parsing and performing semantic analysis of things like type, scope, and nesting depths of program objects.

Coroutines are computer program components that allow execution to be suspended and resumed, generalizing subroutines for cooperative multitasking. Coroutines are well-suited for implementing familiar program components such as cooperative tasks, exceptions, event loops, iterators, infinite lists and pipes.

Write once, run anywhere (WORA), or sometimes Write once, run everywhere (WORE), was a 1995 slogan created by Sun Microsystems to illustrate the cross-platform benefits of the Java language. Ideally, this meant that a Java program could be developed on any device, compiled into standard bytecode, and be expected to run on any device equipped with a Java virtual machine (JVM). The installation of a JVM or Java interpreter on chips, devices, or software packages became an industry standard practice.

HotSpot, released as Java HotSpot Performance Engine, is a Java virtual machine for desktop and server computers, developed by Sun Microsystems which was purchased by and became a division of Oracle Corporation in 2010. Its features improved performance via methods such as just-in-time compilation and adaptive optimization. It is the de facto Java Virtual Machine, serving as the reference implementation of the Java programming language.

CPython is the reference implementation of the Python programming language. Written in C and Python, CPython is the default and most widely used implementation of the Python language.

Concurrent computing is a form of computing in which several computations are executed concurrently—during overlapping time periods—instead of sequentially—with one completing before the next starts.

<span class="mw-page-title-main">Java (software platform)</span> Set of computer software and specifications

Java is a set of computer software and specifications that provides a software platform for developing application software and deploying it in a cross-platform computing environment. Java is used in a wide variety of computing platforms from embedded devices and mobile phones to enterprise servers and supercomputers. Java applets, which are less common than standalone Java applications, were commonly run in secure, sandboxed environments to provide many features of native applications through being embedded in HTML pages.

In computer science, a fiber is a particularly lightweight thread of execution.

Eclipse OpenJ9 is a high performance, scalable, Java virtual machine (JVM) implementation that is fully compliant with the Java Virtual Machine Specification.

The Java Development Kit (JDK) is a distribution of Java technology by Oracle Corporation. It implements the Java Language Specification (JLS) and the Java Virtual Machine Specification (JVMS) and provides the Standard Edition (SE) of the Java Application Programming Interface (API). It is derivative of the community driven OpenJDK which Oracle stewards. It provides software for working with Java applications. Examples of included software are the Java virtual machine, a compiler, performance monitoring tools, a debugger, and other utilities that Oracle considers useful for Java programmers.

<span class="mw-page-title-main">GraalVM</span> Virtual machine software

GraalVM is a Java Development Kit (JDK) written in Java. The open-source distribution of GraalVM is based on OpenJDK, and the enterprise distribution is based on Oracle JDK. As well as just-in-time (JIT) compilation, GraalVM can compile a Java application ahead of time. This allows for faster initialization, greater runtime performance, and decreased resource consumption, but the resulting executable can only run on the platform it was compiled for.

The Computer Language Benchmarks Game is a free software project for comparing how a given subset of simple algorithms can be implemented in various popular programming languages.

<span class="mw-page-title-main">Tokio (software)</span> Library for Rust programming language

Tokio is a software library for the Rust programming language. It provides a runtime and functions that enable the use of asynchronous I/O, allowing for concurrency in regards to task completion.

In computer programming, a virtual thread is a thread that is managed by a runtime library or virtual machine (VM) and made to resemble "real" operating system thread to code executing on it, while requiring substantially fewer resources than the latter.

References

  1. 1 2 Sintes, Tony (April 13, 2001). "Four for the ages". JavaWorld . Archived from the original on 2020-07-15. Retrieved 2020-07-14. 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.{{cite web}}: CS1 maint: bot: original URL status unknown (link)
  2. "Java Technology: The Early Years". java.sun.com. 2014-12-22. Archived from the original on 2008-05-30.
  3. "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.
  4. "Comparative performance evaluation of Java threads for embedded applications: Linux Thread vs. Green Thread". CiteSeerX   10.1.1.8.9238 .
  5. Stallings, William (2008). Operating Systems, Internal and Design Principles. New Jersey: Prentice Hall. p. 171. ISBN   9780136006329.
  6. Sieger, Nick (2011-07-22). "Concurrency in JRuby". Engine Yard. Archived from the original on 2014-01-30. Retrieved 2013-01-26. For systems with large volumes of email, this naive approach may not work well. Native threads carry a bigger initialization cost and memory overhead than green threads, so JRuby normally cannot support more than about 10,000 threads. To work around this, we can use a thread pool.
  7. Goetz, Brian (2005-10-18). "Java theory and practice: Synchronization optimizations in Mustang". IBM . Retrieved 2013-01-26.
  8. "Java Threads in the Solaris Environment – Earlier Releases". Oracle Corporation . Retrieved 2013-01-26. As a result, several problems arose: Java applications could not interoperate with existing MT applications in the Solaris environment, Java threads could not run in parallel on multiprocessors, An MT Java application could not harness true OS concurrency for faster applications on either uniprocessors or multiprocessors. To substantially increase application performance, the green threads library was replaced with native Solaris threads for Java on the Solaris 2.6 platform; this is carried forward on the Solaris 7 and Solaris 8 platforms.
  9. "Threads: Green or Native". SCO Group . Retrieved 2013-01-26. The performance benefit from using native threads on an MP machine can be dramatic. For example, using an artificial benchmark where Java threads are doing processing independent of each other, there can be a three-fold overall speed improvement on a 4-CPU MP machine.
  10. "Threads: Green or Native". codestyle.org. Archived from the original on 2013-01-16. Retrieved 2013-01-26. There is a significant processing overhead for the JVM to keep track of thread states and swap between them, so green thread mode has been deprecated and removed from more recent Java implementations.
  11. "kilim". GitHub. Retrieved 2016-06-09.
  12. "Kilim". www.malhar.net. Retrieved 2016-06-09.
  13. "Quasar Code on GitHub". GitHub .
  14. "Parallel Universe". Archived from the original on 22 December 2015. Retrieved 6 December 2015.
  15. "Chicken Scheme" . Retrieved 5 November 2017.
  16. "thezerobit/green-threads". GitHub. Retrieved 2016-04-08.
  17. "Application-level Stackless features – PyPy 4.0.0 documentation" . Retrieved 6 December 2015.
  18. "Concurrency: GitBook". crystal-lang.org. Retrieved 2018-04-03.
  19. "Fibers - Dlang Tour". tour.dlang.org. Retrieved 2022-05-02.
  20. "Threads: Overview". Dyalog APL 17.0 Help. Retrieved 2018-12-14. A thread is a strand of execution in the APL workspace.
  21. @joeerl (23 June 2018). "Erlang processes are emulated in the Erlang VM, like Green threads - we like them since this simplifies many proble…" (Tweet) via Twitter.
  22. 1 2 "Go and Dogma". research!rsc. Retrieved 2017-01-14. for example both Go and Haskell need some kind of "green threads", so there are more shared runtime challenges than you might expect.
  23. "The Limbo Programming Language". www.vitanuova.com. Retrieved 2019-04-01.
  24. "Multithreading in the MRI Ruby Interpreter | BugFactory" . Retrieved 2024-06-18.
  25. "Racket Places" . Retrieved 2011-10-13. Places enable the development of parallel programs that take advantage of machines with multiple processors, cores, or hardware threads. A place is a parallel task that is effectively a separate instance of the Racket virtual machine.
  26. "Using Threads to Run Code Simultaneously - The Rust Programming Language". doc.rust-lang.org. Retrieved 2021-09-24.
  27. "Stackless.com: About Stackless". Archived from the original on 2012-02-27. Retrieved 2008-08-27. A round robin scheduler is built in. It can be used to schedule tasklets either cooperatively or preemptively.
  28. "Tcl event loop" . Retrieved 6 December 2015.
  29. "JEP 425: Virtual Threads (Preview)" . Retrieved 2024-01-25.
  30. "JEP 444: Virtual Threads" . Retrieved 2024-01-25.
  31. "JEP 464: Scoped Values (Second Preview)" . Retrieved 2024-01-25.