Home:ALL Converter>Understanding java's native threads and the jvm

Understanding java's native threads and the jvm

Ask Time:2010-04-16T22:01:40         Author:Traker

Json Formatter

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.

  • 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?

Author:Traker,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/2653458/understanding-javas-native-threads-and-the-jvm
Stephen C :

\nDoes every thread create their own instance of the JVM to handle their particular execution?\n\nNo. They execute in the same JVM so that (for example) they can share objects and values of static fields.\n\n\nIf not then does the JVM have to have some way to schedule which thread it will handle next\n\nThere 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.\nThe 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)\n\n\nIf so wouldn't this render the multi-threaded nature of Java useless since only one thread can be ran at a time?\n\nWe are talking about green threads now, and this is of historic interest (only) from the Java perspective.\n\nGreen 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)\n\nWith 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\nIn 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.\n\n\nBut 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",
2010-04-16T14:21:44
Bill the Lizard :

\n Does every thread create their own instance of the jvm to handle their particular execution?\n\n\nNo, your application running in the JVM can have many threads that all exist within that instance of the JVM.\n\n\n If not then does the jvm have to have some way to schedule which thread it will handle next...\n\n\nYes, the JVM has a thread scheduler. There are many different algorithms for thread scheduling, and which one is used is JVM-vendor dependent. (Scheduling in general is an interesting topic.)\n\n\n ...if so wouldn't this render the multi-threaded nature of java useless since only one thread can be ran at a time?\n\n\nI'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.",
2010-04-16T14:17:06
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.\nYou may be interested to look at the GO programming language, where thousands of so called \"Goroutines\" are run by dozens of threads.",
2011-05-25T09:30:14
yy