理解java的本地线程和jvm
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2653458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Understanding java's native threads and the jvm
提问by Traker
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 本身就是一个将 java 可执行文件的字节码转换为本地机器代码的应用程序,但是在使用本地线程时,我有一些我似乎无法回答的问题。
- Does every thread create their own instance of the jvm to handle their particular execution?
- If not then does the jvm have to have some way to schedule which thread it will handle next, if so wouldn't this render the multi-threaded nature of java useless since only one thread can be ran at a time?
- 每个线程是否创建自己的 jvm 实例来处理它们的特定执行?
- 如果不是,那么 jvm 是否必须有某种方法来安排它接下来将处理哪个线程,如果是这样,这是否会使 java 的多线程性质变得无用,因为一次只能运行一个线程?
采纳答案by Stephen C
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
如果不是,那么 JVM 是否必须有某种方式来安排它接下来要处理的线程
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.
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)
第二种线是“绿线”。这些由 JVM 自身实现和管理,由 JVM 实现线程调度。自 Java 1.2 以来,Sun/Oracle JVM 不再支持 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.
我们现在谈论的是绿色线程,从 Java 的角度来看,这具有历史意义(仅)。
Green threads have the advantage that scheduling and context switching are faster in the non-I/O case. (Based on measurements made with Java on Linux 2.2; http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.8.9238)
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.
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 情况下调度和上下文切换更快。(基于在 Linux 2.2 上使用 Java 进行的测量;http://citeseerx.ist.psu.edu/viewdoc/summary?doi= 10.1.1.8.9238)
使用纯绿色线程,N 个编程语言线程映射到单个本地线程。正如您所指出的,在此模型中,您不会获得真正的并行执行。
在混合线程实现中,N 个编程语言线程映射到 M 个本地线程(其中 N > M)。在这个模型中,进程内线程调度器负责绿色线程到本地线程的调度,并且你会得到真正的并行执行(如果 M > 1);见https://stackoverflow.com/a/16965741/139985。
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
但即使使用纯绿色线程,您仍然可以获得并发性。控制被切换到另一个线程,一个线程阻塞 I/O 操作,获取锁,等等。此外,JVM 的运行时可以实现周期性线程抢占,以便 CPU 密集型线程不会独占(单个)内核以排除其他线程
回答by Bozhidar Batsov
Java threads are mapped to native OS threads. They have little to do with the JVM itself.
Java 线程映射到本机操作系统线程。它们与 JVM 本身关系不大。
回答by Bill the Lizard
Does every thread create their own instance of the jvm to handle their particular execution?
每个线程是否创建自己的 jvm 实例来处理它们的特定执行?
No, your application running in the JVM can have many threads that all exist within that instance of the JVM.
不,您在 JVM 中运行的应用程序可以有许多线程,这些线程都存在于 JVM 的该实例中。
If not then does the jvm have to have some way to schedule which thread it will handle next...
如果不是,那么 jvm 是否必须有某种方式来安排它接下来将处理哪个线程......
Yes, the JVM has a thread scheduler. There are many different algorithms for thread scheduling, and which one is used is JVM-vendor dependent. (Schedulingin general is an interesting topic.)
是的,JVM 有一个线程调度程序。线程调度有许多不同的算法,使用哪一种取决于 JVM 供应商。(一般来说,调度是一个有趣的话题。)
...if so wouldn't this render the multi-threaded nature of java useless since only one thread can be ran at a time?
...如果是这样的话,这会不会使 java 的多线程特性变得无用,因为一次只能运行一个线程?
I'm not sure I understand this part of your question. This is kind of the point of threading. You typically have more threads than CPUs, and you want to run more than one thing at a time. Threading allows you to take full(er) advantage of your CPU by making sure it's busy processing one thread while another is waiting on I/O, or is for some other reason not busy.
我不确定我是否理解您问题的这一部分。这就是线程的重点。您通常拥有比 CPU 多的线程,并且您希望一次运行不止一件事。线程允许你充分利用你的 CPU,确保它忙于处理一个线程而另一个正在等待 I/O,或者由于某些其他原因不忙。
回答by muh
A Java thread may be mapped one-to-one to a kernel thread. But this must not be so. There could be n kernel threads running m java threads, where m may be much larger than n, and n should be larger than the number of processors. The JVM itself starts the n kernel threads, and each one of them picks a java thread and runs it for a while, then switches to some other java thread. The operating system picks kernel threads and assigns them to a cpu. So there may be thread scheduling on several levels. You may be interested to look at the GO programming language, where thousands of so called "Goroutines" are run by dozens of threads.
Java 线程可以一对一地映射到内核线程。但这一定不是这样。可能有 n 个内核线程运行 m 个 java 线程,其中 m 可能远大于 n,并且 n 应该大于处理器的数量。JVM 本身启动 n 个内核线程,每个线程选择一个 java 线程并运行一段时间,然后切换到其他一些 java 线程。操作系统选择内核线程并将它们分配给 CPU。所以可能会有几个层次的线程调度。您可能有兴趣查看 GO 编程语言,其中成千上万个所谓的“Goroutines”由数十个线程运行。