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的代码没有区别。
If yes, than for smaller programs Java
Threads wont be a big advantage ?
在这里定义很小对于短暂的流程来说,是的,线程没有太大的区别,因为你的顺序执行足够快。请注意,这又取决于解决的问题。对于UI工具包,无论应用程序多么小,需要某种线程/异步执行来保持UI响应。
当你有可以并行运行的东西时,线程也是有道理的。一个典型的例子是在线程上执行大量IO,在另一个中进行计算。你真的不想阻止你的处理,因为你的主线程被阻止做IO。
Once the Hotspot compiles both these
execution paths both can be as good as
native Threads ? Am I right ?
看我的第一点
线程真的不是一个银弹,特别是当涉及到“使用线程使此代码更快”的常见误解。有一点阅读和经验将是你最好的选择。我可以建议获得this awesome book的副本吗? 🙂
@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完全不同。我们来试试看简单的程序的执行:
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 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指令必须在某个时间点被映射到其原始对等体。是否存储本地代码进一步重新使用或丢弃如果不同的东西(优化,记住?)。
因此,为了回答你的问题,每当你编写线程代码时,它被翻译成本机代码并由操作系统运行。这个翻译是在飞行还是在这个时间点上查找是一个完全不同的问题。