Content deleted Content added
Rchard2scout (talk | contribs) m Fix lint errors |
improve wording, drop duplicate sentences |
||
(20 intermediate revisions by 11 users not shown) | |||
Line 1:
{{TOC limit|2}}
{{Short description|Computational threads scheduled by a run-time library}}
{{Distinguish|green thread}}
In [[computer programming]], a '''virtual thread''' is a [[Thread (computing)|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.
Earlier constructs that are not or not always preemptive, such as [[ == Definition ==
{{More citations needed|section|date=April 2022}}
* Virtual threads can hop over the execution units of all processors and cores▼
** In the go1.18 implementation, there are virtual thread queues per execution unit. There are additional virtual threads not allocated to an execution unit and an execution unit can steal virtual threads from another execution unit<ref>{{Cite web |last=Lu |first=Genchi |date=2021-07-22 |title=Java’s Thread Model and Golang Goroutine |url=https://medium.com/@genchilu/javas-thread-model-and-golang-goroutine-f1325ca2df0c |access-date=2022-04-05 |website=Medium |language=en}}</ref>▼
* Virtual threads require no yield or similar interventions by the programmer▼
** Virtual threads appear to execute continuously until they return or stop at a synchronization lock ▼
** Unlike coroutines, if a virtual thread is in an infinite loop, it does not block the program. Execution continues at a higher cpu load, even if there are more looping threads than available execution units▼
* Virtual threads can exists in tens of millions by featuring small often managed stacks▼
** This allows for many magnitudes more threads than from using OS threads▼
** Go 1.18 can launch 15 million virtual threads on a 2021 consumer-grade computer, ie. about 350,000 per gigabyte of main memory. This is enabled by goroutines having a resizable, less than 3 KiB stack▼
* Virtual threads can be allocated quickly, similar to the rate of memory allocations▼
** The quicker ramp-up lessens the need for thread-pools of pre-launched threads to cater for sudden increases in traffic▼
* Virtual threads share memory map like OS threads▼
** Like OS threads, virtual threads share the memory across the process and can therefore freely share and access memory objects subject to synchronization▼
** Some single-threaded architectures, such as the V8 ECMAScript engine used by Node.js, do not readily accept data that the particular thread did not allocate, requiring special zero-copy data types to be used when sharing data between threads ▼
* Virtual threads offer parallelism like OS threads▼
** Parallelism means that multiple instructions are executed truly at the same time which typically leads to a magnitude of faster performance▼
** This is different from the simpler concurrency, in which a single execution unit executes multiple threads shared in small time increments. The sharing makes each thread appear to be continuously executing. While concurrency is easier to implement and program, it do not offer any gains in performance▼
* This allows better utilisation of available hardware.
Java servers have featured extensive and memory consuming software constructs allowing dozens of pooled operating system threads to preemptively execute thousands of requests per second without the use of virtual threads. Key to performance here is to reduce the initial latency in thread processing and minimize the time operating system threads are blocked<ref>{{Cite web |title=Principles to Handle Thousands of Connections in Java Using Netty - DZone Performance |url=https://dzone.com/articles/thousands-of-socket-connections-in-java-practical |access-date=2022-03-30 |website=dzone.com |language=en}}</ref>▼
▲
Virtual threads increase possible concurrency by many orders of magnitudes while the actual [[Parallel computing|parallelism]] achieved is limited by available execution units and pipelining offered by present processors and processor cores. In 2021, a consumer grade computers typically offer a parallelism of tens of concurrent execution units.<ref>{{Cite web |title=MacBook Pro 14-inch and MacBook Pro 16-inch |url=https://www.apple.com/macbook-pro-14-and-16/ |access-date=2022-03-30 |website=Apple |language=en-US}}</ref> For increased performance through parallelism, the language runtime need to use all present hardware,<ref>{{Cite web |title=Frequently Asked Questions (FAQ) - The Go Programming Language |url=https://go.dev/doc/faq#number_cpus |access-date=2022-03-30 |website=go.dev}}</ref> not be single-threaded or feature global synchronization such as [[global interpreter lock]]▼
▲
▲
▲
The many magnitudes of increase in possible preemptive items offered by virtual threads is achieved by the language runtime managing resizable thread stacks.<ref>{{Cite web |title=JEP draft: Virtual Threads (Preview) |url=https://openjdk.java.net/jeps/8277131#Memory-use-and-interaction-with-garbage-collection |access-date=2022-03-30 |website=openjdk.java.net}}</ref> Those stacks are smaller in size than those of operating system threads. The maximum number of threads possible without swapping is proportional to the amount of main memory<ref>{{Cite web |last=Rudell |first=Harald |date=2022-03-22 |title=Maximum number of virtual threads in Go |url=https://www.reddit.com/r/golang/comments/tifbow/comment/i1owqo9}}</ref>▼
▲
▲
In order to support virtual threads efficiently, the language runtime has to be largely rewritten to prevent blocking calls from holding up an operating system thread assigned to execute a virtual thread<ref>{{Cite web |last=Szczukocki |first=Denis |date=2020-03-18 |title=Difference Between Thread and Virtual Thread in Java {{!}} Baeldung |url=https://www.baeldung.com/java-virtual-thread-vs-thread |access-date=2022-03-30 |website=www.baeldung.com |language=en-US}}</ref> and to manage thread stacks.<ref>{{Cite web |date=2018-04-12 |title=Why you can have millions of Goroutines but only thousands of Java Threads |url=https://rcoh.me/posts/why-you-can-have-a-million-go-routines-but-only-1000-java-threads/ |access-date=2022-03-30 |website=rcoh.me |language=en-us}}</ref> An example of a retrofit of virtual threads is Java Loom.<ref>{{Cite web |title=Main - Main - OpenJDK Wiki |url=https://wiki.openjdk.java.net/display/loom/Main |access-date=2022-03-30 |website=wiki.openjdk.java.net}}</ref> An example of a new language designed for virtual threads is Go<ref>{{Cite web |last= |first= |date=2022-03-22 |title=The Go Programming Language |url=https://go.dev/ |access-date=2022-03-30 |website=go.dev}}</ref>▼
* Because allocation of a virtual thread has little overhead on top of allocating memory, they can be allocated very quickly.
▲
▲
▲
▲
▲
== Motivation ==
▲Java servers have featured extensive and memory consuming software constructs allowing dozens of pooled operating system threads to preemptively execute thousands of requests per second without the use of virtual threads. Key to performance here is to reduce the initial latency in thread processing and minimize the time operating system threads are blocked.<ref>{{Cite web |title=Principles to Handle Thousands of Connections in Java Using Netty - DZone Performance |url=https://dzone.com/articles/thousands-of-socket-connections-in-java-practical |access-date=2022-03-30 |website=dzone.com |language=en}}</ref>
▲Virtual threads increase possible concurrency by many orders of magnitudes while the actual [[Parallel computing|parallelism]] achieved is limited by available execution units and pipelining offered by present processors and processor cores. In 2021, a consumer grade computers typically offer a parallelism of tens of concurrent execution units.<ref>{{Cite web |title=MacBook Pro 14-inch and MacBook Pro 16-inch |url=https://www.apple.com/macbook-pro-14-and-16/ |access-date=2022-03-30 |website=Apple |language=en-US}}</ref> For increased performance through parallelism, the language runtime need to use all present hardware,<ref>{{Cite web |title=Frequently Asked Questions (FAQ) - The Go Programming Language |url=https://go.dev/doc/faq#number_cpus |access-date=2022-03-30 |website=go.dev}}</ref> not be single-threaded or feature global synchronization such as [[global interpreter lock]].
▲The many magnitudes of increase in possible preemptive items offered by virtual threads is achieved by the language runtime managing resizable thread stacks.<ref>{{Cite web |title=JEP draft: Virtual Threads (Preview) |url=https://openjdk.java.net/jeps/8277131#Memory-use-and-interaction-with-garbage-collection |access-date=2022-03-30 |website=openjdk.java.net}}</ref> Those stacks are smaller in size than those of operating system threads. The maximum number of threads possible without swapping is proportional to the amount of main memory.<ref>{{Cite web |last=Rudell |first=Harald |date=2022-03-22 |title=Maximum number of virtual threads in Go |url=https://www.reddit.com/r/golang/comments/tifbow/comment/i1owqo9}}</ref>
▲In order to support virtual threads efficiently, the language runtime has to be largely rewritten to prevent blocking calls from holding up an operating system thread assigned to execute a virtual thread<ref>{{Cite web |last=Szczukocki |first=Denis |date=2020-03-18 |title=Difference Between Thread and Virtual Thread in Java {{!}} Baeldung |url=https://www.baeldung.com/java-virtual-thread-vs-thread |access-date=2022-03-30 |website=www.baeldung.com |language=en-US}}</ref> and to manage thread stacks.<ref>{{Cite web |date=2018-04-12 |title=Why you can have millions of Goroutines but only thousands of Java Threads |url=https://rcoh.me/posts/why-you-can-have-a-million-go-routines-but-only-1000-java-threads/ |access-date=2022-03-30 |website=rcoh.me |language=en-us}}</ref> An example of a retrofit of an existing runtime with virtual threads is Java's ''Project Loom''.<ref>{{Cite web |title=Main - Main - OpenJDK Wiki |url=https://wiki.openjdk.java.net/display/loom/Main |access-date=2022-03-30 |website=wiki.openjdk.java.net}}</ref> An example of a new language designed for virtual threads is Go.<ref>{{Cite web |last= |first= |date=2022-03-22 |title=The Go Programming Language |url=https://go.dev/ |access-date=2022-03-30 |website=go.dev}}</ref>
== Complexity ==
Because virtual threads offer parallelism, the programmer needs to be skilled in multi-threaded programming and synchronization.
Because a blocked virtual thread would block the OS thread it occupies at the moment, much effort must be taken in the runtime to handle blocking system calls. Typically, a thread from
Management of the virtual thread stack requires care in the linker and short predictions of additional stack space requirements.
== Implementations ==
=== Google Chrome Browser ===
Virtual threads are used to serialize singleton input/output activities and available to developers extending the browser. When a virtual thread is executing, it can hop on a different OS thread.<ref>{{Cite
=== Go ===
Go's ''goroutines'' became preemptive with
Java introduced virtual threads in 2023 with Java 21, with the limitation that any code running on a virtual thread which uses ''synchronised'' blocks or native calls will become pinned to its carrier OS thread.<ref>{{Cite web |title=Virtual Threads |url=https://docs.oracle.com/en/java/javase/21/core/virtual-threads.html |access-date=2024-09-10 |website=Oracle Help Center |language=en-US}}</ref> The former limitation was fixed in Java 24.<ref>{{Cite web |title=JEP 491: Synchronize Virtual Threads without Pinning |url=https://openjdk.org/jeps/491 |access-date=2025-03-30 |website=OpenJDK |language=en-US}}</ref>
== Other uses of the term ==
Intel<ref>{{Cite web |title=Intel Technology Journal |url=https://www.intel.com/content/dam/www/public/us/en/documents/research/2007-vol11-iss-4-intel-technology-journal.pdf}}</ref> in 2007 referred to an Intel compiler specific optimization technique as virtual threads.
▲=== Java Project Loom ===
== See also ==
Line 74 ⟶ 87:
* [https://codeberg.org/haraldrudell/massivevirtualparallelism/src/branch/main/README.md massivevirtualparallelism Go program testing limits on virtual threads]
[[Category:Threads (computing)]]
[[Category:Virtualization]]
|