0

With an example code shown to create a thread by invoking start() method of java.lang.Thread class,

public class MyThread extends Thread{
    public void run(){
         //code to run goes here
    }
}

MyThread myThread = new MyThread();
myThread.start();

I understand that a second thread gets created in addition to main thread. This second thread performs the work given in run() method.

So, the JVM process is running this code with two user level threads.

Sun J2SE 5.0 is the environment running on Windows 2008/2012 boxes.

My question:

How many kernel level threads does OS dedicate for this JVM process? Does each user level thread map to a separate kernel thread? It is important to know this info, before running the enterprise application with configurable number of threads on destination OS platform.

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • That is OS-specific. Java has no notion of *kernel threads* (or, for that matter, of a *kernel* in the OS sense). – NPE Oct 22 '14 at 10:34
  • @NPE - But shouldn't there be a *Kernel* level Thread mapped to *user level / Java* thread?. Since, the JVM specifies that there should be different stacks for native methods and java methods . Also, only *java methods* PC is maintained in the JVM. So, shouldn't the OS fork its own thread to *link* to this Java thread? – TheLostMind Oct 22 '14 at 10:37
  • 3
    @TheLostMind - It doesn't specify that. It says that there *may* be separate Java and native stacks. – Stephen C Oct 22 '14 at 10:49
  • @StephenC With the given answer below from Dima, I would like to know, How many kernel threads that a process(say JVM) can work with? Does this depend on the process that requests for kernel threads or Does this depend on OS to decide that any process will be assigned 1-1 or N-1 (User level thread - Kernel level thread) mapping? – overexchange Oct 22 '14 at 10:50
  • 3
    @overexchange - The answer is that it is JVM specific. For some JVMs it is 1-to-1. For others (e.g. some Hotspot on Solaris JVMs), the ratio can be controlled by command line options. (This is backed up by a document I have seen that lists all known JVM options ...) – Stephen C Oct 22 '14 at 10:52
  • @overexchange: I would not give much weight to that answer until it is backed up by some authoritative up-to-date references. – NPE Oct 22 '14 at 10:52
  • @StephenC - Ok. But only one *Program Counter* (PC)?. Then the OS will have to maintain the PC for *internal Threads* right?. The instruction can be executed out of order in the processor and the PC for the *java Thread* cannot know how the processor is executing the instructions. [The JVM architecture](http://www.artima.com/insidejvm/ed2/jvmP.html) is here. – TheLostMind Oct 22 '14 at 10:53
  • 2
    @overexchange The answer from Dima makes unsubstantiated assertions and cites a non-normative reference that is 13 years out of date. Don't ask for answers based on it. – user207421 Oct 22 '14 at 10:53
  • 1
    @TheLostMind - Each thread has its own notional PC, no matter how they are implemented. If there is a many-to-one mapping between Java threads and native threads, the implementation *takes care of* the context switching such that the Java thread PCs are preserved. (Obviously ... or it just wouldn't work!) – Stephen C Oct 22 '14 at 10:59
  • @StephenC if it is JVM specific, I would ask, Does usage of `pthread` package in POSIX world using C/C++, will internally run as 1-1 threading model? If yes, Is 1-1 threading model here decided by OS or `pthread` package? – overexchange Oct 22 '14 at 11:04
  • According to [this article](http://www.artima.com/insidejvm/ed2/jvmP.html), the mapping between Java Threads and Native Threads is *implementation dependent* (as some people have already pointed out). – TheLostMind Oct 22 '14 at 11:05
  • @StephenC In POSIX world `pthread` lib creates a separate kernel thread using clone() syscall at kernel level for each pthread_create() at user level. So, the kind of Threading model to work with, it is upto the user process. am i correct? I would like to know, what happens in JVM 5.0, when we run start()? – overexchange Oct 22 '14 at 11:23
  • @overexchange It is implementation-dependent but you can download the source code of the Hotspot VM and have a look. http://openjdk.java.net/groups/hotspot/ – biziclop Oct 22 '14 at 12:15
  • 1
    FWIW, "Kernel thread" means something in the Linux world that has nothing to do with Java or, with any other application that runs in a Linux process. When you're talking about threads that a JVM creates by calling the operating system (possibly through a library like pthreads), You probably should call them "native threads." – Solomon Slow Oct 22 '14 at 12:53
  • @jameslarge Does Sun JVM create a user level thread using it's own user level library or kernel(native) level thread using system calls? – overexchange Oct 23 '14 at 07:16
  • Can somebody answer this query? – overexchange Oct 27 '14 at 10:18
  • @biziclop When you say implementation dependent, Does that mean, Windows platform JVM(Say) would enable kernel thread(using system call) for every user thread and Linux platform JVM(Say) would `not` enable kernel thread for every user level thread? Is that what you say? – overexchange Oct 28 '14 at 04:36
  • 1
    @biziclop In path, `..\openjdk-7-fcs-src-b147-27_jun_2011\openjdk\hotspot\agent\src\os\..`, As per JVM code in `linux\..`, JVM is using, libpthread.so library. On `..\solaris\proc\saproc.cpp` platform, JVM is loading `libthread.so` library . On `..\win32\Reaper.cpp` JVM is using `kernel32.dll` thread functions. – overexchange Oct 28 '14 at 06:30
  • Option -XX:+UseBoundThreads Bind user level threads to kernel threads. (Relevant to Solaris only.) So, In windows/linux i guess, it is N-1 model because kernel32.dll & libpthread.so does not provide system call to launch more than one kernel thread. Solaris provides system call `thr_setconcurrency();` to lauch multiple kernel threads for a process. – overexchange Nov 15 '14 at 08:00

0 Answers0