简体   繁体   中英

How Virtual Threads are mapped to Actual thread

As written, virtual threads are eventually mapped to actual threads. Let's say there are 1 million virtual threads that are created. In a 8 core processor, I assume there will be 8 kernel threads. So, my questions are

  1. how 1 million virtual threads are mapped to 8 kernel threads? What is the algorithm behind this?
  2. Why blocking is cheap in the virtual threads? As I understood this is because it does not block Carrier (Kernel) thread. But kernel thread uses context switch, so why it is still cheap?
  3. Is the virtual thread a good fit for use cases where code needs to call native method ( public native String getSystemTime();)
  1. Mapping of virtual threads and kernel threads is done by the thread model. There are 3 kinds of thread models available

    1. many to one

在此处输入图像描述

  1. one to one

在此处输入图像描述

  1. Many to Many

在此处输入图像描述

Essentially, the thread library keeps a mapping between the user threads/ virtual threads to kernel threads.

  1. The blocking of user thread is easy because all the kernel threads need to do is save the state of the user thread inside the kernel thread or process itself, and pick up the other user thread. On the other hand, if the kernel thread needs to be context switched a large set of registers require to evicted and stored in memory. This process is expensive.

Additionally, creating a kernel thread requires creating a complete thread control block so that kernel can manage those threads. This process is again slow.

  1. You always work at the user thread level. So any call you make needs to be on the user thread itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM