ArticleslgStudy

computer science

Virtual thread

Virtual thread is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Virtual thread rather than just read about it. In short: In computer programming, a virtual thread is a thread that is managed by a runtime library or virtual machine (VM) and made to resemble a kernel 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, compared to low thousands of operating system threads.

Key takeaways

  • Virtual thread belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Virtual thread to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Virtual thread from memory before moving on to harder problems.

Reference excerpt

In computer programming, a virtual thread is a thread that is managed by a runtime library or virtual machine (VM) and made to resemble a kernel 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, compared to low thousands of operating system threads. Preemptive execution 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.

Definition

Virtual threads are preemptive

Important for response performance, a virtual thread can react to events without programmer intervention or before concluding a current task. Preemption requires knowledge of multi-threaded programming to avoid torn writes, data races, and invisible writes by other threads. Virtual threads can hop over the execution units of all processors and cores

This allows better utilisation of available hardware. Go (since version 1.18) uses 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. 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 number in the tens of millions by featuring small often managed stacks

This allows for several magnitudes more threads than it would be possible using operating system threads. Go 1.18 can launch 15 million virtual threads on a 2021 consumer-grade computer, i.e. 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

Because allocation of a virtual thread has little overhead on top of allocating memory, they can be allocated very quickly. 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 like operating system threads

Like OS threads, virtual threads share 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 operating system 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 time-slicing makes each thread appear to be continuously executing. While concurrency is easier to implement and program, it does not offer any gains in performance.

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. 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. For increased performance through parallelism, the language runtime need to use all present hardware, 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. 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. 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 and to manage thread stacks. An example of a retrofit of an existing runtime with virtual threads is Java's Project Loom. An example of a new language designed for virtual threads is Go.

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 a 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 and available to developers extending the browser. When a virtual thread is executing, it can hop on a different OS thread.

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

Java Java introduced mostly preemptive 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. The former limitation was fixed in Java 24.

Other uses of the term Intel in 2007 referred to an Intel compiler specific optimization technique as virtual threads.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Virtual thread

Start with the simplest possible case. Write down what Virtual thread claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Virtual thread before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Virtual thread ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Virtual thread

In research
Virtual thread appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Virtual thread in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Virtual thread is common in secondary-school and first-year university syllabi. It links to neighbouring topics Threads (computing), Virtualization, so understanding it makes those chapters shorter.
In everyday life
Look for Virtual thread outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Virtual thread” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Virtual thread in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Virtual thread means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Virtual thread out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Virtual thread in simple terms?

In computer programming, a virtual thread is a thread that is managed by a runtime library or virtual machine (VM) and made to resemble a kernel thread to code executing on it, while requiring substantially fewer resources than the latter. Virtual threads allows for tens of millions of preemptive t…

Why does Virtual thread matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Virtual thread?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Virtual thread.

Tags

  • Threads (computing)
  • Virtualization

Keep exploring