Virtual thread explained

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.

Virtual threads allows for tens of millions of preemptive tasks and events on a 2021 consumer-grade computer,[1] compared to low thousands of operating system threads.[2] Preemptive execution[3] is important to performance gains through parallelism and fast preemptive response times for tens of millions of events.

Earlier constructs that are not or not always preemptive, such as coroutines, green threads or the largely single-threaded Node.js, introduce delays in responding to asynchronous events such as every incoming request in a server application.[4]

Definition

Virtual threads were commercialized with Google's Chrome browser in 2008[5] where virtual threads may hop physical threads. Virtual threads are truly virtual, created in user-space software.

Underlying reasons

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.[7]

Virtual threads increase possible concurrency by many orders of magnitudes while the actual 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.[8] For increased performance through parallelism, the language runtime need to use all present hardware,[9] 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.[10] 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.[11]

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[12] and to manage thread stacks.[13] An example of a retrofit of virtual threads is Java Loom.[14] An example of a new language designed for virtual threads is Go.[15]

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 an pool of spare OS threads is used to execute the blocking call for the virtual thread so that the initially executing OS thread is not blocked.

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. When a virtual thread is executing, it can hop on different OS thread. The Chrome browser first appeared in 2008. Chrome's virtual threads are available to developers extending the browser.

Go

Go's goroutines became preemptive with Go 1.4 in 2014 and are a prominent application of virtual threads.

Other uses of the term

Intel[16] in 2007 referred to an Intel compiler specific optimization technique as virtual threads.

See also

References

  1. Web site: Rudell . Harald . 2022-03-19 . massivevirtualparallelism .
  2. Web site: baeldung . 2022-01-02 . Maximum Number of Threads Per Process in Linux Baeldung on Linux . 2022-03-30 . www.baeldung.com . en-US.
  3. Web site: Go 1.14 Release Notes - The Go Programming Language . 2022-03-30 . go.dev.
  4. Web site: Node.js . Don't Block the Event Loop (or the Worker Pool) . 2022-03-30 . Node.js . en.
  5. Web site: Threading and Tasks in Chrome . 2022-04-05 . chromium.googlesource.com.
  6. Web site: Lu . Genchi . 2021-07-22 . Java’s Thread Model and Golang Goroutine . 2022-04-05 . Medium . en.
  7. Web site: Principles to Handle Thousands of Connections in Java Using Netty - DZone Performance . 2022-03-30 . dzone.com . en.
  8. Web site: MacBook Pro 14-inch and MacBook Pro 16-inch . 2022-03-30 . Apple . en-US.
  9. Web site: Frequently Asked Questions (FAQ) - The Go Programming Language . 2022-03-30 . go.dev.
  10. Web site: JEP draft: Virtual Threads (Preview) . 2022-03-30 . openjdk.java.net.
  11. Web site: Rudell . Harald . 2022-03-22 . Maximum number of virtual threads in Go .
  12. Web site: Szczukocki . Denis . 2020-03-18 . Difference Between Thread and Virtual Thread in Java Baeldung . 2022-03-30 . www.baeldung.com . en-US.
  13. Web site: 2018-04-12 . Why you can have millions of Goroutines but only thousands of Java Threads . 2022-03-30 . rcoh.me . en-us.
  14. Web site: Main - Main - OpenJDK Wiki . 2022-03-30 . wiki.openjdk.java.net.
  15. Web site: 2022-03-22 . The Go Programming Language . 2022-03-30 . go.dev.
  16. Web site: Intel Technology Journal .

External links

__FORCETOC__