Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding java's native threads and the jvm

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?
like image 564
Traker Avatar asked Apr 16 '10 14:04

Traker


People also ask

What are native threads in Java?

"Native threads" refers to a in which the Java virtual machine creates and manages Java threads using the operating system threads library - named libthread on UnixWare - and each Java thread is mapped to one threads library thread.

Are Java threads native threads?

The standard threading model in Java, covering all JVM languages, uses native threads. This has been the case since Java 1.2 and is the case regardless of the underlying system that the JVM is running on. This means that any time we use any of the standard threading mechanisms in Java, then we're using native threads.

Which thread type keeps the JVM running?

The Java thread lifecycle consists of six thread states: New: A new Thread() has been instantiated. Runnable: The Thread 's start() method has been invoked. Running: The start() method has been invoked and the thread is running.

What does native thread ID mean?

Native thread ID. This ID is platform dependent: On Windows, it's simply the OS-level thread ID within a process. On Linux and Solaris, it's the pid of the thread (which in turn is a light-weight process). On Mac OS X, it is said to be the native pthread_t value.


2 Answers

Does every thread create their own instance of the JVM to handle their particular execution?

No. They execute in the same JVM so that (for example) they can share objects and class attributes.


If not then does the JVM have to have some way to schedule which thread it will handle next

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.

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)


If so wouldn't this render the multi-threaded nature of Java useless since only one thread can be ran at a time?

We are talking about green threads now, and this is of historic interest (only) from the Java perspective.

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

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

like image 194
Stephen C Avatar answered Sep 20 '22 16:09

Stephen C


Does every thread create their own instance of the jvm to handle their particular execution?

No, your application running in the JVM can have many threads that all exist within that instance of the JVM.

If not then does the jvm have to have some way to schedule which thread it will handle next...

Yes, 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.)

...if so wouldn't this render the multi-threaded nature of java useless since only one thread can be ran at a time?

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.

like image 23
Bill the Lizard Avatar answered Sep 23 '22 16:09

Bill the Lizard