了解java的本机线程和jvm
我知道 jvm 本身就是一个将 java 可执行文件的字节码转换为本机机器码的应用程序,但是当使用本机线程时,我有一些我似乎无法回答的问题.
I understand that the jvm is itself an application that turns the bytecode of the java executable into native machine code, but when using native threads I have some questions that I just cannot seem to answer.
- 是否每个线程都创建自己的线程jvm 的实例来处理它们的具体执行?
- 如果不是,那么 jvm 是否必须有某种方法来安排它接下来将处理哪个线程,如果是这样,由于一次只能运行一个线程,这不会使 java 的多线程特性变得无用吗?
推荐答案
是否每个线程都创建自己的 JVM 实例来处理它们的特定执行?
Does every thread create their own instance of the JVM to handle their particular execution?
没有.它们在同一个 JVM 中执行,以便(例如)它们可以共享对象和类属性.
No. They execute in the same JVM so that (for example) they can share objects and class attributes.
如果没有,那么 JVM 是否必须有某种方法来安排它接下来将处理哪个线程
If not then does the JVM have to have some way to schedule which thread it will handle next
Java 中有两种线程实现.本机线程被映射到由主机操作系统实现的线程抽象.操作系统负责本地线程调度和时间片.
There are two kinds of thread implementation in Java. Native threads are mapped onto a thread abstraction which is implemented by the host OS. The OS takes care of native thread scheduling, and time slicing.
第二种线程是绿色线程".这些由 JVM 本身实现和管理,JVM 实现线程调度.自 Java 1.2 以来,Sun/Oracle JVM 不再支持 Java 绿色线程实现.(参见绿色线程与非绿色线程)
The second kind of thread is "green threads". These are implemented and managed by the JVM itself, with the JVM implementing thread scheduling. Java green thread implementations have not been supported by Sun / Oracle JVMs since Java 1.2. (See Green Threads vs Non Green Threads)
如果是这样,由于一次只能运行一个线程,这是否会使 Java 的多线程特性变得毫无用处?
If so wouldn't this render the multi-threaded nature of Java useless since only one thread can be ran at a time?
我们现在谈论的是绿色线程,从 Java 的角度来看,这(仅)具有历史意义.
We are talking about green threads now, and this is of historic interest (only) from the Java perspective.
绿色线程的优点是在非 I/O 情况下调度和上下文切换更快.(基于在 Linux 2.2 上使用 Java 进行的测量;http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.8.9238)
使用纯绿色线程,N 个编程语言线程被映射到一个本地线程.正如您所指出的,在此模型中,您不会获得真正的并行执行.
With pure green threads, N programming language threads are mapped to a single native thread. In this model you don't get true parallel execution, as you noted.
在混合线程实现中,N 个编程语言线程被映射到 M 个本机线程(其中 N > M).在此模型中,进程内线程调度程序负责将绿色线程调度到本地线程,并且您可以获得真正的并行执行(如果 M > 1);请参阅 https://stackoverflow.com/a/16965741/139985.
In a hybrid thread implementation, N programming language threads are mapped onto M native threads (where N > M). In this model, the in-process thread scheduler is responsible for the green thread to native thread scheduling AND you get true parallel execution (if M > 1); see https://stackoverflow.com/a/16965741/139985.
但即使使用纯绿色线程,您仍然可以获得并发性.控制被切换到另一个线程,一个线程在 I/O 操作上阻塞,获取锁等等.此外,JVM 的运行时可以实现周期性线程抢占,这样 CPU 密集型线程就不会独占(单个)内核而排斥其他线程
But even with the pure green threads, you still get concurrency. Control is switched to another threads a thread blocks on an I/O operation, whick acquiring a lock, and so on. Furthermore, the JVM's runtime could implement periodic thread preemption so that a CPU intensive thread doesn't monopolize the (single) core to the exclusion of other threads
相关文章