Home:ALL Converter>Java Threads vs OS Threads

Java Threads vs OS Threads

Ask Time:2010-12-13T17:29:09         Author:Geek

Json Formatter

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

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.

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 ?

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

[EDIT] : 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 ?

thanks

Author:Geek,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/4427398/java-threads-vs-os-threads
Sanjay T. Sharma :

\n Each Thread will be given a native OS\n Thread to RUN which can run on a\n different CPU but since Java is\n interpreted these threads will require\n to interact with the JVM again and\n again to convert the byte code to\n machine instructions ? Am I right ?\n\n\nYou 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.\n\n\n If yes, than for smaller programs Java\n Threads wont be a big advantage ?\n\n\nDefine 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.\n\nThreading 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.\n\n\n Once the Hotspot compiles both these\n execution paths both can be as good as\n native Threads ? Am I right ?\n\n\nSee my first point.\n\nThreading 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? :-)\n\n\n @Sanjay: Infact now I can reframe my\n question. If I have a Thread whose\n code has not been JIT'd how does the\n OS Thread execute it ?\n\n\nAgain 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:\n\n\n java pkg.MyClass -> VM locates method\n to be run -> Start executing the\n byte-code for method line by line ->\n convert each byte-code instruction to\n its native counterpart -> instruction\n executed by OS -> instruction executed\n by machine\n\n\nWhen JIT has kicked in:\n\n\n java pkg.MyClass -> VM locates method\n to be run which has been JIT'ed ->\n locate the associated native code\n for that method -> instruction\n executed by OS -> instruction executed\n by machine\n\n\nAs 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?).\n\nHence 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.",
2010-12-13T09:51:45
Andreas Dolk :

\nand the entire Java process runs only as a single OS Thread\n\nThis is not true. Thus not specified, we often see, that Java threads are in fact native OS threads and that multithreaded Java applications really make use of multi-core processors or multi-processor platforms.\nA common recommendation is using a thread pool where the number of threads is proportional to the number of cores (factor 1-1.5). This is another hint, that the JVM is not restricted to a single OS thread / process.\n\nFrom wkipedia:\n\nIn Java 1.1, green threads were the only threading model used by the JVM,[4] at least on Solaris. As green threads have some limitations compared to native threads, subsequent Java versions dropped them in favor of native threads.\n\nNow, back in 2010 with Java 7 under development and Java 8 planned - are we really still interested in historic "green threads"??",
2010-12-13T09:39:04
AlexR :

Threading and running a byte code are separate issues. Green threads are used by JVM on platforms that do not have native support of threads. (IMHO I do not know which platform does not support threads). \n\nByte code is interpreted in real time and executed on native platform by JVM. JVM decides what are the most popular code fragments and performs so called Just in time compiling of these fragments, so it does not have to compile them again and again. This is independent on threading. If for example you have one thread that executes the same code fragment in loop you this fragment will be cached by just in time compiler. \n\nBottom line: do not worry about performance and threads. Java is strong enough to run everything you are coding. ",
2010-12-13T09:41:35
yy