简体   繁体   English

虚拟线程如何映射到实际线程

[英]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.假设创建了 100 万个虚拟线程。 In a 8 core processor, I assume there will be 8 kernel threads.在 8 核处理器中,我假设将有 8 个 kernel 线程。 So, my questions are所以,我的问题是

  1. how 1 million virtual threads are mapped to 8 kernel threads? 100 万个虚拟线程如何映射到 8 个 kernel 线程? 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?但是 kernel 线程使用上下文切换,为什么它仍然便宜?
  3. Is the virtual thread a good fit for use cases where code needs to call native method ( public native String getSystemTime();)虚拟线程是否适合代码需要调用本机方法的用例(public native String getSystemTime();)
  1. Mapping of virtual threads and kernel threads is done by the thread model.虚拟线程和 kernel 线程的映射由线程 model 完成。 There are 3 kinds of thread models available有 3 种螺纹型号可供选择

    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.本质上,线程库保持用户线程/虚拟线程到 kernel 线程之间的映射。

  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.用户线程的阻塞很容易,因为所有kernel线程需要做的就是将用户线程的state保存在kernel线程或进程本身中,然后拾取其他用户线程。 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.另一方面,如果 kernel 线程需要进行上下文切换,则需要驱逐大量寄存器并将其存储在 memory 中。 This process is expensive.这个过程很昂贵。

Additionally, creating a kernel thread requires creating a complete thread control block so that kernel can manage those threads.此外,创建 kernel 线程需要创建完整的线程控制块,以便 kernel 可以管理这些线程。 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.因此,您进行的任何调用都需要在用户线程本身上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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