Difference between platform thread, carrier thread and virtual thread in context of Java 21 and later
Asked Answered
V

2

4

The introduction of virtual threads in Java 21 has created a lot of buzz around the community. While I still try to wrap my head around actual benefit I will be getting by this feature, I stumble upto three terminologies

  • virtual thread
  • carrier thread
  • platform thread

While I understood that the platform thread is an actual OS thread and the virtual thread Virtual threads are a lightweight implementation of threads that is provided by the JDK rather than the OS, and somehow these two threads are tied via a carrier thread. But I am still unclear about their exact anotomy and how they work in conjection

Vice answered 3/3, 2024 at 3:6 Comment(1)
Not sure what you are looking for apart from this documentation. A platform thread is JVM's wrapper over an OS thread, consider what you used to call simply threads before introducing the concept of virtual threads. Additionally, carrier thread is nothing but a term for platform threads that are carrying a virtual thread operation.Ascetic
C
8

I assume that you are referring to the terminology in the Oracle documentation for Virtual Threads. This document says this:

The operating system schedules when a platform thread is run. However, the Java runtime schedules when a virtual thread is run. When the Java runtime schedules a virtual thread, it assigns or mounts the virtual thread on a platform thread, then the operating system schedules that platform thread as usual. This platform thread is called a carrier. After running some code, the virtual thread can unmount from its carrier. This usually happens when the virtual thread performs a blocking I/O operation. After a virtual thread unmounts from its carrier, the carrier is free, which means that the Java runtime scheduler can mount a different virtual thread on it.

Further on, the document does use the term "carrier thread" rather than just "carrier".

What the document is saying is that some of the time a virtual thread is assigned to a platform thread. While it is assigned, the platform thread is the carrier (or carrier thread) for the virtual thread.


[...] and somehow these two threads are tied via a carrier thread.

That is not correct. The carrier thread is actually a platform thread. It is just called a carrier thread because its purpose is to "carry" a virtual thread. There are really just two threads here, not three.

Cheloid answered 3/3, 2024 at 3:36 Comment(1)
If my assumption is incorrect, please provide the URL for the pages that you are looking at ...Cheloid
P
4

The three terms are defined as:

  • Platform thread: The Java wrapper for an Operating System (OS) thread that is scheduled by the thread scheduler of the OS.
  • Virtual Thread: A lightweight abstraction of a task that can be bound (called "mounting" in Java 21) to a platform thread and is scheduled by the Java virtual thread scheduler.
  • Carrier thread: The platform thread on which a virtual thread is mounted.

Virtual threads are not always mounted and therefore may not have a carrier thread at any given time. This usually occurs when a virtual thread performs a blocking operation, and is unmounted from its carrier thread (allowing another virtual thread to be mounted to the platform thread).

Note: A virtual thread may be mounted to different carrier threads over its lifetime of execution, except in certain cases where it is "pinned" and must be mounted on the same carrier thread when a blocking operation unblocks. The underlying carrier thread is also blocked during pinning.

For more information, see:

Plight answered 3/3, 2024 at 12:28 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.