Search code examples
javamultithreadingvirtual-threads

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


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


Solution

  • 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.