I read Head First for multithreading. What I know about multithreading:
When we call start () with an object of class Thread , this thread goes into Runnable state . Thus, all threads go into Runnable state after calling start () by the object of these threads. This scheduler JVM thread that selects randomly from the flow state the Runnable , to indicate it is able to work . Going into the Run state , a certain call stack will be executed for this particular thread.
Again, the JVM thread scheduler can stop the thread from executing by selecting that thread from Running to Runnable. This time, code execution is paused in the call stack of this thread.
Now my question is: for a multiprocessor machine, how does a JVM thread scheduler select a thread from Runnable state? Does it select only one thread and pass it to the processor? Or does he select more than one thread and give these threads the startup state of different processors?
I wrote the code below:
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");
}
}
public class MyRunnable implements Runnable{
public void run()
{
System.out.println("I'm Thread");
}
}
There are two threads here. The main thread and the thread that I created. If my machine has a multiprocessor, how will it behave? Will the JVM thread scheduler select two threads at a time and pass them to two multiprocessors?
source
share