Java 线程与操作系统线程

Java Threads vs OS Threads(Java 线程与操作系统线程)
本文介绍了Java 线程与操作系统线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

看起来我搞砸了 Java 线程/操作系统线程和解释语言.

Looks like I have messed up with Java Threads/OS Threads and Interpreted language.

在开始之前,我确实了解绿色线程是 Java 线程,其中线程由 JVM 负责,整个 Java 进程仅作为单个 OS 线程运行.因此在多处理器系统上它是无用的.

Before I begin, I do understand that Green Threads are Java Threads where the threading is taken care of by the JVM and the entire Java process runs only as a single OS Thread. Thereby on a multi processor system it is useless.

现在我的问题是.我有两个线程 A 和 B.每个线程都有 10 万行独立代码.我在多处理器系统上的 Java 程序中运行这些线程.每个线程将被赋予一个本机操作系统线程来运行,它可以在不同的 CPU 上运行,但由于 Java 被解释,这些线程将需要一次又一次地与 JVM 交互以将字节码转换为机器指令?我对吗 ?如果是,那么对于较小的程序,Java 线程不会是一个很大的优势?

Now my questions is. I have two Threads A and B. Each with 100 thousand lines of independent code. I run these threads in my Java Program on a multiprocessor system. Each Thread will be given a native OS Thread to RUN which can run on a different CPU but since Java is interpreted these threads will require to interact with the JVM again and again to convert the byte code to machine instructions ? Am I right ? If yes, than for smaller programs Java Threads wont be a big advantage ?

一旦 Hotspot 编译了这两个执行路径,它们都可以和原生线程一样好?我说的对吗?

Once the Hotspot compiles both these execution paths both can be as good as native Threads ? Am I right ?

:另一个问题是,假设您有一个 Java 线程,其代码不是 JIT 编译的,您创建该线程并 start() 吗?操作系统线程和 JVM 如何交互来运行该字节码?

: An alternate question can be, assume you have a single Java Thread whose code is not JIT compiled, you create that Thread and start() it ? How does the OS Thread and JVM interact to run that Bytecode ?

谢谢

推荐答案

每个线程都会被赋予一个原生操作系统线程到 RUN 可以运行在不同的 CPU,但由于 Java 是解释这些线程将需要再次与 JVM 交互再次将字节码转换为机器指令 ?我说的对吗?

Each Thread will be given a native OS Thread to RUN which can run on a different CPU but since Java is interpreted these threads will require to interact with the JVM again and again to convert the byte code to machine instructions ? Am I right ?

你混合了两种不同的东西;由 VM 完成的 JIT 和 VM 提供的线程支持.在内心深处,你所做的一切都会转化为某种本机代码.使用线程的字节码指令与访问线程的 JIT 代码没有什么不同.

You are mixing two different things; JIT done by the VM and the threading support offered by the VM. Deep down inside, everything you do translates to some sort of native code. A byte-code instruction which uses thread is no different than a JIT'ed code which accesses threads.

如果是,那么对于较小的程序 Java线程不会是一个很大的优势?

If yes, than for smaller programs Java Threads wont be a big advantage ?

在这里定义小.对于短暂的进程,是的,线程不会产生太大的影响,因为您的顺序执行速度足够快.请注意,这又取决于要解决的问题.对于 UI 工具包,无论应用程序多么小,都需要某种线程/异步执行来保持 UI 响应.

Define small here. For short lived processes, yes, threading doesn't make that big a difference since your sequential execution is fast enough. Note that this again depends on the problem being solved. For UI toolkits, no matter how small the application, some sort of threading/asynchronous execution is required to keep the UI responsive.

当您拥有可以并行运行的东西时,线程也很有意义.一个典型的例子是在线程中进行大量 IO 并在另一个中进行计算.你真的不想仅仅因为你的主线程被阻塞做 IO 而阻塞你的处理.

Threading also makes sense when you have things which can be run in parallel. A typical example would be doing heavy IO in on thread and computation in another. You really wouldn't want to block your processing just because your main thread is blocked doing IO.

一旦 Hotspot 编译了这两个执行路径都可以和本机线程?我说的对吗?

Once the Hotspot compiles both these execution paths both can be as good as native Threads ? Am I right ?

请参阅我的第一点.

线程确实不是灵丹妙药,尤其是当涉及到使用线程使代码运行得更快"这一常见误解时.一点阅读和经验将是你最好的选择.我可以推荐一份这本很棒的书吗?:-)

Threading really isn't a silver bullet, esp when it comes to the common misconception of "use threads to make this code go faster". A bit of reading and experience will be your best bet. Can I recommend getting a copy of this awesome book? :-)

@Sanjay:事实上,我现在可以重新构建我的问题.如果我有一个线程代码尚未经过 JIT 处理操作系统线程执行它?

@Sanjay: Infact now I can reframe my question. If I have a Thread whose code has not been JIT'd how does the OS Thread execute it ?

我再说一遍,线程是与 JIT 完全不同的概念.让我们试着简单地看一下程序的执行:

Again I'll say it, threading is a completely different concept from JIT. Let's try to look at the execution of a program in simple terms:

java pkg.MyClass -> VM 定位方法要运行 -> 开始执行逐行方法的字节码->将每个字节码指令转换为它的原生对应物 -> 指令由操作系统执行 -> 执行的指令通过机器

java pkg.MyClass -> VM locates method to be run -> Start executing the byte-code for method line by line -> convert each byte-code instruction to its native counterpart -> instruction executed by OS -> instruction executed by machine

当 JIT 启动时:

java pkg.MyClass -> VM 定位方法运行 已经过 JIT 的 ->找到相关的 native 代码对于那个方法->指令由操作系统执行 -> 执行的指令通过机器

java pkg.MyClass -> VM locates method to be run which has been JIT'ed -> locate the associated native code for that method -> instruction executed by OS -> instruction executed by machine

如您所见,无论您遵循何种路线,VM 指令都必须在某个时间点映射到其本地对应项.是否存储该本机代码以供进一步重用或在其他情况下丢弃(优化,记得吗?).

As you can see, irrespective of the route you follow, the VM instruction has to be mapped to its native counterpart at some point in time. Whether that native code is stored for further re-use or thrown away if a different thing (optimization, remember?).

因此回答您的问题,每当您编写线程代码时,它 被翻译为本机代码并由操作系统运行.翻译是即时完成还是在那个时间点查找是完全不同的问题.

Hence to answer your question, whenever you write threading code, it is translated to native code and run by the OS. Whether that translation is done on the fly or looked up at that point in time is a completely different issue.

这篇关于Java 线程与操作系统线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to implement RecyclerView with section header depending on category?(如何根据类别实现带有节标题的 RecyclerView?)
How to generate JNI header file in Eclipse(如何在 Eclipse 中生成 JNI 头文件)
Setting a custom HTTP header dynamically with Spring-WS client(使用 Spring-WS 客户端动态设置自定义 HTTP 标头)
Could you technically call the string[] anything in the main method?(从技术上讲,您可以在 main 方法中调用 string[] 吗?)
What is the proper way of setting headers in a URLConnection?(在 URLConnection 中设置标头的正确方法是什么?)
How to overwrite http-header quot;Hostquot; in a HttpURLConnection?(如何覆盖 http-header “主机在 HttpURLConnection 中?)