问题
I've been reading Head First for multithreading. What I know about multithreading is:
When we call start() with an object of Thread class that thread goes to the Runnable state. So all the threads go to Runnable state after calling start() by object of those threads. It is JVM thread scheduler, who picks thread randomly from Runnable state to give it in Running state. After going to Running state, the determined call stack for that specific thread becomes executed.
Again, JVM thread scheduler can stop executing of a thread by picking that thread from Running state to Runnable state. This time, code execution is paused in the call stack of that thread.
Now my question is, for multiprocessor machine, how JVM thread scheduler picks thread from the Runnable state? Does it pick only one thread and give it to a processor? Or, does it pick more than one thread and give those threads to Running state of different processors?
I have written below code:
// Class of main thread
public class ThreadMain {
public static void main(String[] args) {
Runnable threadJob=new MyRunnable();
Thread t=new Thread(threadJob);
t.start();
System.out.println("Back in the Main");
}
}
// Class of another thread
public class MyRunnable implements Runnable{
public void run()
{
System.out.println("I'm Thread");
}
}
Here, there are two threads. Main thread, and the thread I've created. If my machine has multiprocessor, how it will behave? Will the JVM thread scheduler pick two threads at a time, and give those to two multiprocessors?
回答1:
The term “JVM thread scheduler” makes only sense, if we consider operating system, JVM and class library as an execution environment as a whole. Then, it’s guaranteed that this environment has a scheduler, regardless of how it is implemented.
In most of today’s implementations the JVM will create an operating system level thread for each Java thread and does no active scheduling activity itself. But a particular JVM implementation may contain a scheduler for operating systems that don’t have one.
E.g., for Sun’s JVM that was the case back in the last millennium. At this time, there was the option to use green threads, as opposed to native threads. Note that these threads implemented without the aid of the operating system aren’t capable of using multiple CPUs/cores.
So in practice, when you run your example program, the operating system’s scheduler may indeed assign the second thread to a different core. However, since this is a tiny program, it’s also possible that the first thread terminates before the second even starts its actual work, and in that case, it will likely run on the same core as the first, but there is no guaranty about any particular scheduling behavior at all.
While there is no guaranty regarding a particular scheduling behavior, most SMP libraries and tools are built on the (founded) assumption, that if there are enough runnable threads with sufficient workload, the underlying system will assign these threads to available CPU cores.
回答2:
JVM use underlying OS(Unix, Windows etc) threading mechanism to schedule java thread on multiprocessor system.
来源:https://stackoverflow.com/questions/41759261/how-jvm-thread-scheduler-control-threads-for-multiprocessors